fix: vollständige SEO-Bake-Prüfung sichern

This commit is contained in:
Leon Tonhäuser committed 2026-07-30 12:01:34 +02:00
1 parent 6dbf5c2fec
commit 5eed960772
2 files changed
+38 -6

No files matched your search

+7 -1
View File
@@ -109,6 +109,7 @@ function buildBlock(p) {
} }
let n = 0; let n = 0;
let skipped = 0;
for (const f of readdirSync(PUB).filter((x) => x.endsWith('.html'))) { for (const f of readdirSync(PUB).filter((x) => x.endsWith('.html'))) {
const fp = path.join(PUB, f); const fp = path.join(PUB, f);
const p = f === 'index.html' ? '/' : '/' + f.replace(/\.html$/, ''); 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 (page.title) {
if (!/<title>[\s\S]*?<\/title>/i.test(html)) { if (!/<title>[\s\S]*?<\/title>/i.test(html)) {
console.warn('kein <title> in', f); console.warn('kein <title> in', f);
skipped++;
continue; continue;
} }
html = html.replace( 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?'); const re = new RegExp('[\\t ]*' + reEsc(BEGIN) + '[\\s\\S]*?' + reEsc(END) + '\\n?');
html = html.replace(re, ''); 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>'); html = html.replace('</head>', buildBlock(p) + '\n</head>');
writeFileSync(fp, html); writeFileSync(fp, html);
n++; n++;
} }
console.log(`Gebacken: ${n} Seiten`); console.log(`Gebacken: ${n} Seiten`);
if (skipped) {
console.error(`${skipped} Seite(n) übersprungen.`);
process.exitCode = 1;
}
+31 -5
View File
@@ -1,6 +1,6 @@
import assert from 'node:assert/strict'; import assert from 'node:assert/strict';
import { execFileSync } from 'node:child_process'; 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 path from 'node:path';
import vm from 'node:vm'; import vm from 'node:vm';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
@@ -45,12 +45,20 @@ function check(name, fn) {
} }
function bakeSeo() { function bakeSeo() {
execFileSync(process.execPath, ['scripts/bake-seo.mjs'], { return execFileSync(process.execPath, ['scripts/bake-seo.mjs'], {
cwd: ROOT, cwd: ROOT,
encoding: 'utf8',
stdio: 'pipe' 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')) { if (wants('core')) {
const config = loadConfig(); const config = loadConfig();
const cities = ['Hamburg', 'Hannover', 'Wolfsburg', 'Uelzen', 'Celle', 'Lüneburg']; const cities = ['Hamburg', 'Hannover', 'Wolfsburg', 'Uelzen', 'Celle', 'Lüneburg'];
@@ -96,10 +104,28 @@ if (wants('core')) {
assert.ok(html.includes('"@type":"Country","name":"Deutschland"')); assert.ok(html.includes('"@type":"Country","name":"Deutschland"'));
}); });
check('erneutes SEO-Backen verändert den Event-Head nicht', () => { check('erneutes SEO-Backen verändert keine Top-Level-HTML-Seite', () => {
const before = read('public/eventfotografie.html'); const before = snapshotTopLevelHtml();
bakeSeo(); 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/);
}); });
} }