Enhance SEO meta tag handling in generateHTML function by improving regex patterns for description and keywords, ensuring proper insertion of new tags before closing head tag while preserving script tags, and adding detailed logging for HTML manipulation.

This commit is contained in:
Torsten Schulz (local)
2025-12-05 09:20:27 +01:00
parent 9ee288fd93
commit 6b0e905d27

View File

@@ -42,20 +42,21 @@ function generateHTML(route, meta, __dirname) {
// Verwende die gebaute index.html (mit korrekten Asset-Pfaden von Vite) // Verwende die gebaute index.html (mit korrekten Asset-Pfaden von Vite)
let baseHTML = readFileSync(distIndexPath, 'utf-8'); let baseHTML = readFileSync(distIndexPath, 'utf-8');
console.log('[SEO] Gebaute HTML geladen, Länge:', baseHTML.length); console.log('[SEO] Gebaute HTML geladen, Länge:', baseHTML.length);
console.log('[SEO] Enthält Script-Tags:', baseHTML.includes('<script'));
// Ersetze Meta-Tags in der gebauten HTML // Ersetze Meta-Tags in der gebauten HTML
baseHTML = baseHTML.replace(/<title>.*?<\/title>/, `<title>${meta.title}</title>`); baseHTML = baseHTML.replace(/<title>.*?<\/title>/, `<title>${meta.title}</title>`);
// Ersetze oder füge description hinzu // Ersetze oder füge description hinzu
if (baseHTML.includes('<meta name="description"')) { if (baseHTML.includes('<meta name="description"')) {
baseHTML = baseHTML.replace(/<meta name="description"[^>]*>/, `<meta name="description" content="${meta.description}">`); baseHTML = baseHTML.replace(/<meta name="description"[^>]*>/g, `<meta name="description" content="${meta.description}">`);
} else { } else {
baseHTML = baseHTML.replace('</head>', ` <meta name="description" content="${meta.description}">\n</head>`); baseHTML = baseHTML.replace('</head>', ` <meta name="description" content="${meta.description}">\n</head>`);
} }
// Ersetze oder füge keywords hinzu // Ersetze oder füge keywords hinzu
if (baseHTML.includes('<meta name="keywords"')) { if (baseHTML.includes('<meta name="keywords"')) {
baseHTML = baseHTML.replace(/<meta name="keywords"[^>]*>/, `<meta name="keywords" content="${meta.keywords}">`); baseHTML = baseHTML.replace(/<meta name="keywords"[^>]*>/g, `<meta name="keywords" content="${meta.keywords}">`);
} else { } else {
baseHTML = baseHTML.replace('</head>', ` <meta name="keywords" content="${meta.keywords}">\n</head>`); baseHTML = baseHTML.replace('</head>', ` <meta name="keywords" content="${meta.keywords}">\n</head>`);
} }
@@ -73,19 +74,29 @@ function generateHTML(route, meta, __dirname) {
<meta name="twitter:image" content="${meta.ogImage}"> <meta name="twitter:image" content="${meta.ogImage}">
<link rel="canonical" href="${meta.ogUrl}">`; <link rel="canonical" href="${meta.ogUrl}">`;
// Entferne alte OG/Twitter/Canonical Tags falls vorhanden // Entferne alte OG/Twitter/Canonical Tags falls vorhanden (nur Meta-Tags, keine Script-Tags!)
baseHTML = baseHTML.replace(/<meta property="og:[^>]*>/g, ''); baseHTML = baseHTML.replace(/<meta property="og:[^>]*>/g, '');
baseHTML = baseHTML.replace(/<meta name="twitter:[^>]*>/g, ''); baseHTML = baseHTML.replace(/<meta name="twitter:[^>]*>/g, '');
baseHTML = baseHTML.replace(/<link rel="canonical"[^>]*>/g, ''); baseHTML = baseHTML.replace(/<link rel="canonical"[^>]*>/g, '');
// Füge neue Tags vor </head> ein // Füge neue Tags vor </head> ein (aber NACH den Script-Tags!)
baseHTML = baseHTML.replace('</head>', `${ogTags}\n</head>`); // Finde die Position von </head> und füge die Tags davor ein
const headEndIndex = baseHTML.indexOf('</head>');
if (headEndIndex !== -1) {
baseHTML = baseHTML.substring(0, headEndIndex) + ogTags + '\n' + baseHTML.substring(headEndIndex);
}
// Füge robots meta hinzu falls nicht vorhanden // Füge robots meta hinzu falls nicht vorhanden
if (!baseHTML.includes('<meta name="robots"')) { if (!baseHTML.includes('<meta name="robots"')) {
baseHTML = baseHTML.replace('</head>', ` <meta name="robots" content="index, follow">\n</head>`); const headEndIndex2 = baseHTML.indexOf('</head>');
if (headEndIndex2 !== -1) {
baseHTML = baseHTML.substring(0, headEndIndex2) + ` <meta name="robots" content="index, follow">\n` + baseHTML.substring(headEndIndex2);
}
} }
console.log('[SEO] HTML nach Manipulation, Länge:', baseHTML.length);
console.log('[SEO] Enthält Script-Tags nach Manipulation:', baseHTML.includes('<script'));
return baseHTML; return baseHTML;
} }