feat: regionale SEO-Daten zentralisieren
This commit is contained in:
1 parent
e5b4903492
commit
1131ca5f81
31 files changed
+300
-131
No files matched your search
+37
-9
@@ -27,19 +27,24 @@ const esc = (s) => String(s).replace(/&/g, '&').replace(/"/g, '"').repl
|
||||
const jsonEsc = (s) => String(s).replace(/</g, '\\u003c');
|
||||
const reEsc = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
|
||||
function placeNode(place) {
|
||||
if (typeof place === 'string') return { '@type': 'City', name: place };
|
||||
return { '@type': place.type, name: place.name };
|
||||
}
|
||||
|
||||
function personBusinessGraph() {
|
||||
return { '@context': 'https://schema.org', '@graph': [
|
||||
{ '@type': 'Person', name: biz.personName, jobTitle: biz.personJobTitle, url: SITE_URL, email: biz.email, telephone: biz.telephone },
|
||||
{ '@type': 'ProfessionalService', name: biz.name, url: SITE_URL, telephone: biz.telephone, email: biz.email,
|
||||
address: { '@type': 'PostalAddress', addressLocality: biz.addressLocality, addressRegion: biz.addressRegion, addressCountry: biz.addressCountry },
|
||||
areaServed: biz.areaServed.map((n) => ({ '@type': 'City', name: n })), priceRange: biz.priceRange }
|
||||
areaServed: biz.areaServed.map(placeNode), priceRange: biz.priceRange }
|
||||
]};
|
||||
}
|
||||
|
||||
function breadcrumbListGraph(p) {
|
||||
const segs = p.split('/').filter(Boolean);
|
||||
const labels = {
|
||||
portfolio: 'Portfolio', leistungen: 'Leistungen', 'über-mich': 'Über mich', kontakt: 'Kontakt', blog: 'Blog', archiv: 'Archiv',
|
||||
portfolio: 'Portfolio', leistungen: 'Leistungen', sportfotografie: 'Sportfotografie', eventfotografie: 'Eventfotografie', 'über-mich': 'Über mich', kontakt: 'Kontakt', blog: 'Blog', archiv: 'Archiv',
|
||||
'portfolio-fc-st-pauli': 'FC St. Pauli', 'portfolio-hamburger-sv': 'Hamburger SV', 'portfolio-frauen-bundesliga': 'Frauen-Bundesliga',
|
||||
'portfolio-hannover-96': 'Hannover 96', 'portfolio-sv-meppen': 'SV Meppen', 'portfolio-deutschland-u21': 'Deutschland U21',
|
||||
'portfolio-nfv-pokalfinale-2026': 'NFV-Pokalfinale 2026', 'portfolio-yes-con-2026': 'YES!CON 2026',
|
||||
@@ -58,12 +63,16 @@ function breadcrumbListGraph(p) {
|
||||
return { '@context': 'https://schema.org', '@type': 'BreadcrumbList', itemListElement: items };
|
||||
}
|
||||
|
||||
function serviceGraph() {
|
||||
return { '@context': 'https://schema.org', '@type': 'Service', serviceType: biz.serviceType,
|
||||
function serviceGraph(page) {
|
||||
const data = page.service || {};
|
||||
const serviceType = data.serviceType || biz.serviceType;
|
||||
const areas = data.areaServed || biz.areaServed;
|
||||
const services = data.services || biz.services;
|
||||
return { '@context': 'https://schema.org', '@type': 'Service', serviceType,
|
||||
provider: { '@type': 'ProfessionalService', name: biz.name, url: SITE_URL },
|
||||
areaServed: biz.areaServed.map((n) => ({ '@type': 'City', name: n })),
|
||||
hasOfferCatalog: { '@type': 'OfferCatalog', name: biz.serviceType + '-Leistungen',
|
||||
itemListElement: biz.services.map((s) => ({ '@type': 'Offer', itemOffered: { '@type': 'Service', name: s } })) } };
|
||||
areaServed: areas.map(placeNode),
|
||||
hasOfferCatalog: { '@type': 'OfferCatalog', name: serviceType + '-Leistungen',
|
||||
itemListElement: services.map((name) => ({ '@type': 'Offer', itemOffered: { '@type': 'Service', name } })) } };
|
||||
}
|
||||
|
||||
const BEGIN = '<!-- STATIC-SEO: canonical, Open Graph, Twitter, JSON-LD (aus seo-config.js gebacken) -->';
|
||||
@@ -94,7 +103,7 @@ function buildBlock(p) {
|
||||
const s = page.jsonld || [];
|
||||
if (s.includes('person-business')) L.push(`<script type="application/ld+json" id="seo-person-business">${jsonEsc(JSON.stringify(personBusinessGraph()))}</script>`);
|
||||
if (s.includes('breadcrumb')) L.push(`<script type="application/ld+json" id="seo-breadcrumb">${jsonEsc(JSON.stringify(breadcrumbListGraph(p)))}</script>`);
|
||||
if (s.includes('service')) L.push(`<script type="application/ld+json" id="seo-service">${jsonEsc(JSON.stringify(serviceGraph()))}</script>`);
|
||||
if (s.includes('service')) L.push(`<script type="application/ld+json" id="seo-service">${jsonEsc(JSON.stringify(serviceGraph(page)))}</script>`);
|
||||
L.push(END);
|
||||
return '\t' + L.join('\n\t');
|
||||
}
|
||||
@@ -104,6 +113,25 @@ 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$/, '');
|
||||
let html = readFileSync(fp, 'utf8');
|
||||
const page = cfg.PAGES[p] || {};
|
||||
if (page.title) {
|
||||
if (!/<title>[\s\S]*?<\/title>/i.test(html)) {
|
||||
console.warn('kein <title> in', f);
|
||||
continue;
|
||||
}
|
||||
html = html.replace(
|
||||
/<title>[\s\S]*?<\/title>/i,
|
||||
`<title>${esc(page.title)}</title>`
|
||||
);
|
||||
}
|
||||
if (page.desc) {
|
||||
const descriptionTag = `<meta name="description" content="${esc(page.desc)}">`;
|
||||
if (/<meta\s+name=["']description["'][^>]*>/i.test(html)) {
|
||||
html = html.replace(/<meta\s+name=["']description["'][^>]*>/i, descriptionTag);
|
||||
} else {
|
||||
html = html.replace('</title>', `</title>\n\t${descriptionTag}`);
|
||||
}
|
||||
}
|
||||
const re = new RegExp(reEsc(BEGIN) + '[\\s\\S]*?' + reEsc(END) + '\\n?');
|
||||
html = html.replace(re, '');
|
||||
if (!html.includes('</head>')) { console.warn('kein </head> in', f); continue; }
|
||||
@@ -111,4 +139,4 @@ for (const f of readdirSync(PUB).filter((x) => x.endsWith('.html'))) {
|
||||
writeFileSync(fp, html);
|
||||
n++;
|
||||
}
|
||||
console.log(`Gebacken: ${n} Seiten`);
|
||||
console.log(`Gebacken: ${n} Seiten`);
|
||||
@@ -0,0 +1,97 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { existsSync, readFileSync, readdirSync } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import vm from 'node:vm';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
||||
const PUBLIC = path.join(ROOT, 'public');
|
||||
const requested = new Set(process.argv.slice(2));
|
||||
const wants = (suite) => requested.size === 0 || requested.has('all') || requested.has(suite);
|
||||
const failures = [];
|
||||
|
||||
function read(relativePath) {
|
||||
return readFileSync(path.join(ROOT, relativePath), 'utf8');
|
||||
}
|
||||
|
||||
function loadConfig() {
|
||||
const context = { window: {} };
|
||||
vm.createContext(context);
|
||||
vm.runInContext(read('public/custom/seo-config.js'), context);
|
||||
return context.window.SEO_CONFIG;
|
||||
}
|
||||
|
||||
function count(text, pattern) {
|
||||
return (text.match(pattern) || []).length;
|
||||
}
|
||||
|
||||
function htmlEscape(value) {
|
||||
return String(value)
|
||||
.replace(/&/g, '&')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>');
|
||||
}
|
||||
|
||||
function check(name, fn) {
|
||||
try {
|
||||
fn();
|
||||
console.log(`PASS ${name}`);
|
||||
} catch (error) {
|
||||
failures.push(`${name}: ${error.message}`);
|
||||
console.error(`FAIL ${name}: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (wants('core')) {
|
||||
const config = loadConfig();
|
||||
const cities = ['Hamburg', 'Hannover', 'Wolfsburg', 'Uelzen', 'Celle', 'Lüneburg'];
|
||||
|
||||
check('Geschäftsort ist Ebstorf', () => {
|
||||
assert.equal(config.BUSINESS.addressLocality, 'Ebstorf');
|
||||
});
|
||||
|
||||
check('alle sechs Sportregionen sind zentral gepflegt', () => {
|
||||
assert.deepEqual(Array.from(config.BUSINESS.areaServed), cities);
|
||||
});
|
||||
|
||||
check('Sportseite hat seitenspezifische Service-Daten', () => {
|
||||
const page = config.PAGES['/sportfotografie'];
|
||||
assert.equal(page.service.serviceType, 'Sportfotografie');
|
||||
assert.deepEqual(
|
||||
Array.from(page.service.areaServed, (place) => place.name),
|
||||
cities
|
||||
);
|
||||
assert.ok(page.jsonld.includes('service'));
|
||||
});
|
||||
|
||||
check('Eventseite hat Hamburg und Deutschland als Einsatzgebiet', () => {
|
||||
const page = config.PAGES['/eventfotografie'];
|
||||
assert.deepEqual(
|
||||
JSON.parse(JSON.stringify(page.service.areaServed)),
|
||||
[
|
||||
{ type: 'City', name: 'Hamburg' },
|
||||
{ type: 'Country', name: 'Deutschland' }
|
||||
]
|
||||
);
|
||||
assert.ok(page.jsonld.includes('service'));
|
||||
});
|
||||
|
||||
check('gebackener Event-Head entspricht der Konfiguration', () => {
|
||||
const page = config.PAGES['/eventfotografie'];
|
||||
const html = read('public/eventfotografie.html');
|
||||
assert.equal(count(html, /<title>/g), 1);
|
||||
assert.equal(count(html, /<meta name="description"/g), 1);
|
||||
assert.ok(html.includes(`<title>${htmlEscape(page.title)}</title>`));
|
||||
assert.ok(html.includes(`content="${htmlEscape(page.desc)}"`));
|
||||
assert.ok(html.includes('"serviceType":"Eventfotografie"'));
|
||||
assert.ok(html.includes('"@type":"Country","name":"Deutschland"'));
|
||||
});
|
||||
}
|
||||
|
||||
if (failures.length) {
|
||||
console.error(`\n${failures.length} Prüfung(en) fehlgeschlagen.`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log('\nAlle angeforderten SEO-Prüfungen sind grün.');
|
||||
Reference in new issue
Block a user