Aimpress_site/scripts/generate-sitemap.mjs
Vadym Samoilenko b6a6a55e05 Generate dynamic sitemap with blog posts and lastmod dates
New script reads public/blog/posts.json at build time and outputs
public/sitemap.xml with all static routes + /blog/:slug entries,
each with lastmod from post dates or current date.

Build pipeline: sync-blog → generate-sitemap → tsc → vite build

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-18 21:37:29 +00:00

50 lines
1.6 KiB
JavaScript

import { readFileSync, writeFileSync } from 'node:fs';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const root = resolve(__dirname, '..');
const today = new Date().toISOString().split('T')[0];
const staticRoutes = [
{ url: '/', priority: '1.0', changefreq: 'weekly' },
{ url: '/about', priority: '0.8', changefreq: 'monthly' },
{ url: '/services', priority: '0.8', changefreq: 'monthly' },
{ url: '/pricing', priority: '0.8', changefreq: 'weekly' },
{ url: '/blog', priority: '0.9', changefreq: 'daily' },
{ url: '/privacy-policy', priority: '0.3', changefreq: 'yearly' },
{ url: '/terms-of-use', priority: '0.3', changefreq: 'yearly' },
];
let blogRoutes = [];
try {
const posts = JSON.parse(readFileSync(resolve(root, 'public/blog/posts.json'), 'utf-8'));
blogRoutes = posts.map(p => ({
url: `/blog/${p.slug}`,
priority: '0.7',
changefreq: 'monthly',
lastmod: p.date || today,
}));
} catch {
console.warn('Could not read posts.json, skipping blog routes');
}
const allRoutes = [
...staticRoutes.map(r => ({ ...r, lastmod: today })),
...blogRoutes,
];
const xml = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${allRoutes.map(r => ` <url>
<loc>https://ai-impress.com${r.url}</loc>
<lastmod>${r.lastmod}</lastmod>
<changefreq>${r.changefreq}</changefreq>
<priority>${r.priority}</priority>
</url>`).join('\n')}
</urlset>
`;
writeFileSync(resolve(root, 'public/sitemap.xml'), xml, 'utf-8');
console.log(`Sitemap generated with ${allRoutes.length} URLs`);