Implement SEO improvements and enforce HTTPS redirection in production

- Added a middleware to enforce HTTPS and canonical host for known public hosts, enhancing security and SEO.
- Introduced a function to generate the sitemap.xml dynamically, improving the delivery of SEO data to crawlers.
- Updated the sitemap route to utilize the new function, ensuring stable delivery and error handling.

These changes collectively enhance the application's SEO capabilities and ensure secure access in production environments.
This commit is contained in:
Torsten Schulz (local)
2026-03-27 11:35:50 +01:00
parent 9b079e31a0
commit bb13779c72
3 changed files with 148 additions and 20 deletions

View File

@@ -75,6 +75,27 @@ const seoData = {
}
};
function buildSitemapXml() {
const currentDate = new Date().toISOString().split('T')[0];
const urls = Object.entries(seoData)
.map(([route, meta]) => {
const priority = route === '/' ? '1.0' : '0.8';
const changefreq = route === '/' ? 'daily' : 'weekly';
return ` <url>
<loc>${meta.ogUrl}</loc>
<lastmod>${currentDate}</lastmod>
<changefreq>${changefreq}</changefreq>
<priority>${priority}</priority>
</url>`;
})
.join('\n');
return `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${urls}
</urlset>`;
}
function escapeHtml(value = '') {
return String(value)
.replace(/&/g, '&amp;')
@@ -221,25 +242,22 @@ Sitemap: ${SITE_URL}/sitemap.xml
});
app.get('/sitemap.xml', (req, res) => {
const currentDate = new Date().toISOString().split('T')[0];
const urls = Object.entries(seoData)
.map(([route, meta]) => {
const priority = route === '/' ? '1.0' : '0.8';
const changefreq = route === '/' ? 'daily' : 'weekly';
return ` <url>
<loc>${meta.ogUrl}</loc>
<lastmod>${currentDate}</lastmod>
<changefreq>${changefreq}</changefreq>
<priority>${priority}</priority>
</url>`;
})
.join('\n');
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
try {
const sitemap = buildSitemapXml();
// Stabilere Auslieferung fuer Crawler und Reverse-Proxy-Caches.
res.set('Cache-Control', 'public, max-age=300, s-maxage=300, stale-while-revalidate=600');
res.type('application/xml');
res.status(200).send(sitemap);
} catch (error) {
console.error('[SEO] Fehler beim Generieren der sitemap.xml:', error);
// Fallback: niemals 500 fuer die Sitemap ausliefern.
const fallback = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${urls}
<url><loc>${SITE_URL}/</loc></url>
</urlset>`;
res.type('application/xml');
res.send(sitemap);
res.set('Cache-Control', 'no-store');
res.type('application/xml');
res.status(200).send(fallback);
}
});
}