From 6b0e905d27879c6f0e1bd4c8062ed376c8b6852c Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Fri, 5 Dec 2025 09:20:27 +0100 Subject: [PATCH] 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. --- server/routes-seo.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/server/routes-seo.js b/server/routes-seo.js index 02022c8..e38aa62 100644 --- a/server/routes-seo.js +++ b/server/routes-seo.js @@ -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('.*?<\/title>/, `${meta.title}`); // Ersetze oder füge description hinzu if (baseHTML.includes(']*>/, ``); + baseHTML = baseHTML.replace(/]*>/g, ``); } else { baseHTML = baseHTML.replace('', ` \n`); } // Ersetze oder füge keywords hinzu if (baseHTML.includes(']*>/, ``); + baseHTML = baseHTML.replace(/]*>/g, ``); } else { baseHTML = baseHTML.replace('', ` \n`); } @@ -73,19 +74,29 @@ function generateHTML(route, meta, __dirname) { `; - // 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(/]*>/g, ''); baseHTML = baseHTML.replace(/]*>/g, ''); - // Füge neue Tags vor ein - baseHTML = baseHTML.replace('', `${ogTags}\n`); + // Füge neue Tags vor ein (aber NACH den Script-Tags!) + // Finde die Position von und füge die Tags davor ein + const headEndIndex = baseHTML.indexOf(''); + if (headEndIndex !== -1) { + baseHTML = baseHTML.substring(0, headEndIndex) + ogTags + '\n' + baseHTML.substring(headEndIndex); + } // Füge robots meta hinzu falls nicht vorhanden if (!baseHTML.includes('', ` \n`); + const headEndIndex2 = baseHTML.indexOf(''); + if (headEndIndex2 !== -1) { + baseHTML = baseHTML.substring(0, headEndIndex2) + ` \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('