Files
portfolio-photo/scripts/bake-seo.mjs
T
Leon TonhäuserandClaude 7476659b14 feat: statische Canonical-, Open-Graph- und JSON-LD-Tags in alle Seiten backen
Bisher wurden canonical, Open Graph (og:title/description/url/type/site_name/
image), Twitter-Card und JSON-LD (Person/Business, BreadcrumbList, Service)
nur per seo.js zur Laufzeit injiziert → ohne JS keine Vorschau beim Teilen
(Social/Messenger) und kein statisches Canonical gegen Duplicate Content.

Jetzt als echte <link>/<meta>/<script>-Tags im <head> gebacken (Marker-
geschützter, idempotenter Block), Werte aus seo-config.js, Logik äquivalent
zu seo.js. seo.js bleibt als idempotenter Fallback (statische JSON-LD-Skripte
tragen dieselben IDs → kein Duplikat).

Zusätzlich repariert:
- DEFAULT_OG_IMAGE zeigt auf echtes Foto (vorher nicht existierende PNG).
- 8 fehlende Portfolio-Seiten in seo-config ergänzt (fc-st-pauli,
  beach-soccer-cup, deutschland-u21, hannover-96, nfv-pokalfinale-2026,
  omr-yes-wecancer, sv-meppen, yes-con-2026) → keine Title-Überschreibung
  mit generischem SITE_NAME mehr, einheitliche SEO-Titel.
- Breadcrumb-Labels sauber (FC St. Pauli, YES!CON 2026 …).

Wartbarkeit: scripts/bake-seo.mjs neu ausgeführt, wenn sich Config/Seiten
ändern. Nicht Teil des Pages-Artifacts (liegt außerhalb public/).

Co-Authored-By: Claude <noreply@anthropic.com>
2026-06-25 16:56:55 +02:00

110 lines
6.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Backt statische SEO-Tags (canonical, Open Graph, Twitter, JSON-LD) in den
// <head> jeder Seite unter public/. Werte aus public/custom/seo-config.js,
// Logik äquivalent zu public/custom/seo.js. Idempotent (Marker-geschützter Block).
//
// Aufruf: node scripts/bake-seo.mjs
// Werte-Quelle bleibt seo-config.js; bei Config-Änderung neu ausführen.
import { readFileSync, writeFileSync, readdirSync } from 'node:fs';
import vm from 'node:vm';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const PUB = path.join(ROOT, 'public');
// seo-config.js laden (IIFE weist window.SEO_CONFIG zu)
const ctx = { window: {} };
vm.createContext(ctx);
vm.runInContext(readFileSync(path.join(PUB, 'custom/seo-config.js'), 'utf8'), ctx);
const cfg = ctx.window.SEO_CONFIG;
const SITE_URL = String(cfg.SITE_URL).replace(/\/$/, '');
const SITE_NAME = cfg.SITE_NAME;
const OG_IMAGE = cfg.DEFAULT_OG_IMAGE ? SITE_URL + cfg.DEFAULT_OG_IMAGE : '';
const biz = cfg.BUSINESS;
const esc = (s) => String(s).replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
const jsonEsc = (s) => String(s).replace(/</g, '\\u003c');
const reEsc = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
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 }
]};
}
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-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',
'portfolio-omr-yes-wecancer': 'OMR x yes!wecancer', 'portfolio-beach-soccer-cup': 'Beach Soccer Cup',
'portfolio-aufstiegsfeier': 'Aufstiegsfeier', 'portfolio-abiball': 'Abiball', 'portfolio-svg-lueneburg': 'SVG Lüneburg',
'portfolio-rhine-ruhr-3x3': 'Rhine-Ruhr 2025 – 3×3 Basketball', 'portfolio-rhine-ruhr-basketball': 'Rhine-Ruhr 2025 – Basketball',
'portfolio-rhine-ruhr-beachvolleyball': 'Rhine-Ruhr 2025 – Beachvolleyball', 'portfolio-rhine-ruhr-sport': 'Rhine-Ruhr 2025 – Sportarten'
};
const items = [{ '@type': 'ListItem', position: 1, name: 'Startseite', item: SITE_URL + '/' }];
let acc = '';
for (let i = 0; i < segs.length; i++) {
acc += '/' + segs[i];
const label = labels[segs[i]] || segs[i].replace(/^portfolio-/, '').replace(/-/g, ' ').replace(/\b\w/g, (c) => c.toUpperCase());
items.push({ '@type': 'ListItem', position: i + 2, name: label, item: SITE_URL + acc });
}
return { '@context': 'https://schema.org', '@type': 'BreadcrumbList', itemListElement: items };
}
function serviceGraph() {
return { '@context': 'https://schema.org', '@type': 'Service', serviceType: biz.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 } })) } };
}
const BEGIN = '<!-- STATIC-SEO: canonical, Open Graph, Twitter, JSON-LD (aus seo-config.js gebacken) -->';
const END = '<!-- /STATIC-SEO -->';
function buildBlock(p) {
const page = cfg.PAGES[p] || {};
const title = page.title || cfg.SITE_NAME;
const desc = page.desc || cfg.DEFAULT_DESCRIPTION;
const canonical = SITE_URL + (p === '/' ? '/' : p);
const ogType = p === '/' ? 'website' : 'article';
const L = [BEGIN,
`<link rel="canonical" href="${esc(canonical)}">`,
`<meta property="og:title" content="${esc(title)}">`,
`<meta property="og:description" content="${esc(desc)}">`,
`<meta property="og:url" content="${esc(canonical)}">`,
`<meta property="og:type" content="${ogType}">`,
`<meta property="og:site_name" content="${esc(SITE_NAME)}">`];
if (OG_IMAGE) L.push(`<meta property="og:image" content="${esc(OG_IMAGE)}">`);
L.push(`<meta name="twitter:card" content="summary_large_image">`,
`<meta name="twitter:title" content="${esc(title)}">`,
`<meta name="twitter:description" content="${esc(desc)}">`);
if (OG_IMAGE) L.push(`<meta name="twitter:image" content="${esc(OG_IMAGE)}">`);
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>`);
L.push(END);
return '\t' + L.join('\n\t');
}
let n = 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$/, '');
let html = readFileSync(fp, 'utf8');
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; }
html = html.replace('</head>', buildBlock(p) + '\n</head>');
writeFileSync(fp, html);
n++;
}
console.log(`Gebacken: ${n} Seiten`);