fix: vollständige SEO-Bake-Prüfung sichern
This commit is contained in:
1 parent
6dbf5c2fec
commit
5eed960772
2 files changed
+38
-6
No files matched your search
@@ -109,6 +109,7 @@ function buildBlock(p) {
|
||||
}
|
||||
|
||||
let n = 0;
|
||||
let skipped = 0;
|
||||
for (const f of readdirSync(PUB).filter((x) => x.endsWith('.html'))) {
|
||||
const fp = path.join(PUB, f);
|
||||
const p = f === 'index.html' ? '/' : '/' + f.replace(/\.html$/, '');
|
||||
@@ -117,6 +118,7 @@ for (const f of readdirSync(PUB).filter((x) => x.endsWith('.html'))) {
|
||||
if (page.title) {
|
||||
if (!/<title>[\s\S]*?<\/title>/i.test(html)) {
|
||||
console.warn('kein <title> in', f);
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
html = html.replace(
|
||||
@@ -134,9 +136,13 @@ for (const f of readdirSync(PUB).filter((x) => x.endsWith('.html'))) {
|
||||
}
|
||||
const re = new RegExp('[\\t ]*' + reEsc(BEGIN) + '[\\s\\S]*?' + reEsc(END) + '\\n?');
|
||||
html = html.replace(re, '');
|
||||
if (!html.includes('</head>')) { console.warn('kein </head> in', f); continue; }
|
||||
if (!html.includes('</head>')) { console.warn('kein </head> in', f); skipped++; continue; }
|
||||
html = html.replace('</head>', buildBlock(p) + '\n</head>');
|
||||
writeFileSync(fp, html);
|
||||
n++;
|
||||
}
|
||||
console.log(`Gebacken: ${n} Seiten`);
|
||||
if (skipped) {
|
||||
console.error(`${skipped} Seite(n) übersprungen.`);
|
||||
process.exitCode = 1;
|
||||
}
|
||||
+31
-5
@@ -1,6 +1,6 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { existsSync, readFileSync, readdirSync } from 'node:fs';
|
||||
import { readFileSync, readdirSync, writeFileSync } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import vm from 'node:vm';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
@@ -45,12 +45,20 @@ function check(name, fn) {
|
||||
}
|
||||
|
||||
function bakeSeo() {
|
||||
execFileSync(process.execPath, ['scripts/bake-seo.mjs'], {
|
||||
return execFileSync(process.execPath, ['scripts/bake-seo.mjs'], {
|
||||
cwd: ROOT,
|
||||
encoding: 'utf8',
|
||||
stdio: 'pipe'
|
||||
});
|
||||
}
|
||||
|
||||
function snapshotTopLevelHtml() {
|
||||
return readdirSync(PUBLIC)
|
||||
.filter((name) => name.endsWith('.html'))
|
||||
.sort()
|
||||
.map((name) => [name, readFileSync(path.join(PUBLIC, name), 'utf8')]);
|
||||
}
|
||||
|
||||
if (wants('core')) {
|
||||
const config = loadConfig();
|
||||
const cities = ['Hamburg', 'Hannover', 'Wolfsburg', 'Uelzen', 'Celle', 'Lüneburg'];
|
||||
@@ -96,10 +104,28 @@ if (wants('core')) {
|
||||
assert.ok(html.includes('"@type":"Country","name":"Deutschland"'));
|
||||
});
|
||||
|
||||
check('erneutes SEO-Backen verändert den Event-Head nicht', () => {
|
||||
const before = read('public/eventfotografie.html');
|
||||
check('erneutes SEO-Backen verändert keine Top-Level-HTML-Seite', () => {
|
||||
const before = snapshotTopLevelHtml();
|
||||
bakeSeo();
|
||||
assert.equal(read('public/eventfotografie.html'), before);
|
||||
assert.deepEqual(snapshotTopLevelHtml(), before);
|
||||
});
|
||||
|
||||
check('SEO-Bake meldet übersprungene HTML-Seiten als Fehler', () => {
|
||||
const file = path.join(PUBLIC, 'blog.html');
|
||||
const original = readFileSync(file, 'utf8');
|
||||
let failure;
|
||||
try {
|
||||
writeFileSync(file, original.replace('</head>', ''));
|
||||
try {
|
||||
bakeSeo();
|
||||
} catch (error) {
|
||||
failure = error;
|
||||
}
|
||||
} finally {
|
||||
writeFileSync(file, original);
|
||||
}
|
||||
assert.ok(failure, 'das Backen muss bei übersprungenen HTML-Seiten fehlschlagen');
|
||||
assert.match(String(failure.stderr), /kein <\/head> in blog\.html/);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user