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,11 +29,19 @@ 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)) {
|
||||
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)
|
||||
baseHTML = readFileSync(distIndexPath, 'utf-8');
|
||||
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>`);
|
||||
@@ -77,11 +85,6 @@ function generateHTML(route, meta, __dirname) {
|
||||
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;
|
||||
}
|
||||
|
||||
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