/** * Writes public/sitemap.xml and public/robots.txt from the content that will ship * in the build. The server also serves a live sitemap at /sitemap.xml (which picks * up articles published after the build); this static copy is the fallback for * static hosting and for crawlers hitting the file directly. * * node scripts/generate-sitemap.mjs [siteUrl] */ import fs from 'node:fs' import path from 'node:path' const ROOT = path.resolve(import.meta.dirname, '..') const SITE_URL = (process.argv[2] || process.env.SITE_URL || 'https://www.blackdice.ai').replace(/\/+$/, '') const PATHS = [ ['/', '1.0'], ['/mobile-sdk', '0.9'], ['/dns-protect', '0.9'], ['/halo-cpe', '0.9'], ['/for-operators', '0.8'], ['/financial-services', '0.8'], ['/why-blackdice', '0.7'], ['/news', '0.8'], ['/investors', '0.6'], ['/contact', '0.7'], ] const seed = JSON.parse(fs.readFileSync(path.join(ROOT, 'src/cms/generated/seedContent.json'), 'utf8')) const live = (() => { const file = path.join(ROOT, 'content/site-content.json') return fs.existsSync(file) ? JSON.parse(fs.readFileSync(file, 'utf8')) : null })() const posts = (live?.posts || seed.posts || []).filter((p) => p.status !== 'draft') const today = new Date().toISOString().slice(0, 10) const urls = [ ...PATHS.map(([p, pri]) => ({ loc: SITE_URL + p, lastmod: today, pri })), ...posts.map((p) => ({ loc: `${SITE_URL}/blog/${p.slug}`, lastmod: (p.updatedAt || p.date || today).slice(0, 10), pri: '0.6', })), ] const xml = '\n\n' + urls .map((u) => ` \n ${u.loc}\n ${u.lastmod}\n ${u.pri}\n `) .join('\n') + '\n\n' fs.writeFileSync(path.join(ROOT, 'public/sitemap.xml'), xml) fs.writeFileSync(path.join(ROOT, 'public/robots.txt'), `User-agent: *\nAllow: /\n\nSitemap: ${SITE_URL}/sitemap.xml\n`) console.log(`sitemap.xml: ${urls.length} URLs (${posts.length} articles) → ${SITE_URL}`)