Improve SEO handling in generateHTML function by adding detailed logging, enhancing error handling for missing index.html, and ensuring proper path resolution for production environments.
This commit is contained in:
@@ -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>/, `<title>${meta.title}</title>`);
|
||||
|
||||
// Ersetze oder füge description hinzu
|
||||
if (baseHTML.includes('<meta name="description"')) {
|
||||
baseHTML = baseHTML.replace(/<meta name="description"[^>]*>/, `<meta name="description" content="${meta.description}">`);
|
||||
} else {
|
||||
baseHTML = baseHTML.replace('</head>', ` <meta name="description" content="${meta.description}">\n</head>`);
|
||||
}
|
||||
|
||||
// Ersetze oder füge keywords hinzu
|
||||
if (baseHTML.includes('<meta name="keywords"')) {
|
||||
baseHTML = baseHTML.replace(/<meta name="keywords"[^>]*>/, `<meta name="keywords" content="${meta.keywords}">`);
|
||||
} else {
|
||||
baseHTML = baseHTML.replace('</head>', ` <meta name="keywords" content="${meta.keywords}">\n</head>`);
|
||||
}
|
||||
|
||||
// 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>/, `<title>${meta.title}</title>`);
|
||||
|
||||
// Ersetze oder füge description hinzu
|
||||
if (baseHTML.includes('<meta name="description"')) {
|
||||
baseHTML = baseHTML.replace(/<meta name="description"[^>]*>/, `<meta name="description" content="${meta.description}">`);
|
||||
} else {
|
||||
baseHTML = baseHTML.replace('</head>', ` <meta name="description" content="${meta.description}">\n</head>`);
|
||||
}
|
||||
|
||||
// Ersetze oder füge keywords hinzu
|
||||
if (baseHTML.includes('<meta name="keywords"')) {
|
||||
baseHTML = baseHTML.replace(/<meta name="keywords"[^>]*>/, `<meta name="keywords" content="${meta.keywords}">`);
|
||||
} else {
|
||||
baseHTML = baseHTML.replace('</head>', ` <meta name="keywords" content="${meta.keywords}">\n</head>`);
|
||||
}
|
||||
|
||||
// Ersetze oder füge Open Graph Tags hinzu
|
||||
const ogTags = `
|
||||
<meta property="og:title" content="${meta.ogTitle}">
|
||||
<meta property="og:description" content="${meta.ogDescription}">
|
||||
<meta property="og:type" content="${meta.ogType}">
|
||||
@@ -64,23 +72,18 @@ function generateHTML(route, meta, __dirname) {
|
||||
<meta name="twitter:description" content="${meta.ogDescription}">
|
||||
<meta name="twitter:image" content="${meta.ogImage}">
|
||||
<link rel="canonical" href="${meta.ogUrl}">`;
|
||||
|
||||
// Entferne alte OG/Twitter/Canonical Tags falls vorhanden
|
||||
baseHTML = baseHTML.replace(/<meta property="og:[^>]*>/g, '');
|
||||
baseHTML = baseHTML.replace(/<meta name="twitter:[^>]*>/g, '');
|
||||
baseHTML = baseHTML.replace(/<link rel="canonical"[^>]*>/g, '');
|
||||
|
||||
// Füge neue Tags vor </head> ein
|
||||
baseHTML = baseHTML.replace('</head>', `${ogTags}\n</head>`);
|
||||
|
||||
// Füge robots meta hinzu falls nicht vorhanden
|
||||
if (!baseHTML.includes('<meta name="robots"')) {
|
||||
baseHTML = baseHTML.replace('</head>', ` <meta name="robots" content="index, follow">\n</head>`);
|
||||
}
|
||||
} 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(/<meta property="og:[^>]*>/g, '');
|
||||
baseHTML = baseHTML.replace(/<meta name="twitter:[^>]*>/g, '');
|
||||
baseHTML = baseHTML.replace(/<link rel="canonical"[^>]*>/g, '');
|
||||
|
||||
// Füge neue Tags vor </head> ein
|
||||
baseHTML = baseHTML.replace('</head>', `${ogTags}\n</head>`);
|
||||
|
||||
// Füge robots meta hinzu falls nicht vorhanden
|
||||
if (!baseHTML.includes('<meta name="robots"')) {
|
||||
baseHTML = baseHTML.replace('</head>', ` <meta name="robots" content="index, follow">\n</head>`);
|
||||
}
|
||||
|
||||
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.');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user