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:
@@ -42,20 +42,21 @@ function generateHTML(route, meta, __dirname) {
|
||||
// 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);
|
||||
console.log('[SEO] Enthält Script-Tags:', baseHTML.includes('<script'));
|
||||
|
||||
// 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}">`);
|
||||
baseHTML = baseHTML.replace(/<meta name="description"[^>]*>/g, `<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}">`);
|
||||
baseHTML = baseHTML.replace(/<meta name="keywords"[^>]*>/g, `<meta name="keywords" content="${meta.keywords}">`);
|
||||
} else {
|
||||
baseHTML = baseHTML.replace('</head>', ` <meta name="keywords" content="${meta.keywords}">\n</head>`);
|
||||
}
|
||||
@@ -73,18 +74,28 @@ function generateHTML(route, meta, __dirname) {
|
||||
<meta name="twitter:image" content="${meta.ogImage}">
|
||||
<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 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 neue Tags vor </head> ein (aber NACH den Script-Tags!)
|
||||
// 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
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user