diff --git a/server/routes-seo.js b/server/routes-seo.js index 6ae0fbc..02022c8 100644 --- a/server/routes-seo.js +++ b/server/routes-seo.js @@ -1,5 +1,5 @@ import { readFileSync, existsSync } from 'fs'; -import { join } from 'path'; +import { join, resolve } from 'path'; // SEO-Meta-Daten für verschiedene Routen const seoData = { @@ -29,31 +29,39 @@ const seoData = { function generateHTML(route, meta, __dirname) { // Versuche, die gebaute index.html zu lesen const distIndexPath = join(__dirname, '../docroot/dist/index.html'); - let baseHTML; - if (existsSync(distIndexPath)) { - // Verwende die gebaute index.html (mit korrekten Asset-Pfaden von Vite) - baseHTML = readFileSync(distIndexPath, 'utf-8'); - - // Ersetze Meta-Tags in der gebauten HTML - baseHTML = baseHTML.replace(/.*?<\/title>/, `<title>${meta.title}`); - - // Ersetze oder füge description hinzu - if (baseHTML.includes(']*>/, ``); - } else { - baseHTML = baseHTML.replace('', ` \n`); - } - - // Ersetze oder füge keywords hinzu - if (baseHTML.includes(']*>/, ``); - } else { - baseHTML = baseHTML.replace('', ` \n`); - } - - // Ersetze oder füge Open Graph Tags hinzu - const ogTags = ` + console.log('[SEO] Prüfe gebaute index.html:', distIndexPath); + console.log('[SEO] Datei existiert:', existsSync(distIndexPath)); + + if (!existsSync(distIndexPath)) { + // Fallback: Gebaute index.html nicht gefunden + console.error('WARNUNG: Gebaute index.html nicht gefunden:', distIndexPath); + return null; + } + + // Verwende die gebaute index.html (mit korrekten Asset-Pfaden von Vite) + let baseHTML = readFileSync(distIndexPath, 'utf-8'); + console.log('[SEO] Gebaute HTML geladen, Länge:', baseHTML.length); + + // Ersetze Meta-Tags in der gebauten HTML + baseHTML = baseHTML.replace(/.*?<\/title>/, `<title>${meta.title}`); + + // Ersetze oder füge description hinzu + if (baseHTML.includes(']*>/, ``); + } else { + baseHTML = baseHTML.replace('', ` \n`); + } + + // Ersetze oder füge keywords hinzu + if (baseHTML.includes(']*>/, ``); + } else { + baseHTML = baseHTML.replace('', ` \n`); + } + + // Ersetze oder füge Open Graph Tags hinzu + const ogTags = ` @@ -64,23 +72,18 @@ function generateHTML(route, meta, __dirname) { `; - - // Entferne alte OG/Twitter/Canonical Tags falls vorhanden - baseHTML = baseHTML.replace(/]*>/g, ''); - baseHTML = baseHTML.replace(/]*>/g, ''); - - // Füge neue Tags vor ein - baseHTML = baseHTML.replace('', `${ogTags}\n`); - - // Füge robots meta hinzu falls nicht vorhanden - if (!baseHTML.includes('', ` \n`); - } - } else { - // Fallback: Gebaute index.html nicht gefunden - verwende SPA-Fallback - console.error('WARNUNG: Gebaute index.html nicht gefunden:', distIndexPath); - return null; + + // Entferne alte OG/Twitter/Canonical Tags falls vorhanden + baseHTML = baseHTML.replace(/]*>/g, ''); + baseHTML = baseHTML.replace(/]*>/g, ''); + + // Füge neue Tags vor ein + baseHTML = baseHTML.replace('', `${ogTags}\n`); + + // Füge robots meta hinzu falls nicht vorhanden + if (!baseHTML.includes('', ` \n`); } return baseHTML; @@ -92,6 +95,8 @@ export function setupSEORoutes(app, __dirname) { const IS_PRODUCTION = process.env.NODE_ENV === 'production'; if (IS_PRODUCTION) { + const distIndexPath = resolve(__dirname, '../docroot/dist/index.html'); + // Pre-Rendering für Hauptseite app.get('/', (req, res) => { const meta = seoData['/']; @@ -100,10 +105,10 @@ export function setupSEORoutes(app, __dirname) { res.send(html); } else { // Fallback: Verwende die gebaute index.html direkt (ohne Meta-Tag-Anpassung) - const distIndexPath = join(__dirname, '../docroot/dist/index.html'); if (existsSync(distIndexPath)) { res.sendFile(distIndexPath); } else { + console.error('FEHLER: Gebaute index.html nicht gefunden:', distIndexPath); res.status(500).send('Gebaute index.html nicht gefunden. Bitte führe "npm run build" aus.'); } } @@ -117,10 +122,10 @@ export function setupSEORoutes(app, __dirname) { res.send(html); } else { // Fallback: Verwende die gebaute index.html direkt (ohne Meta-Tag-Anpassung) - const distIndexPath = join(__dirname, '../docroot/dist/index.html'); if (existsSync(distIndexPath)) { res.sendFile(distIndexPath); } else { + console.error('FEHLER: Gebaute index.html nicht gefunden:', distIndexPath); res.status(500).send('Gebaute index.html nicht gefunden. Bitte führe "npm run build" aus.'); } }