import { mkdir, readFile, writeFile } from 'node:fs/promises'
import { dirname, join } from 'node:path'
import { NOT_FOUND_META, ROUTES, type RouteMeta } from '../src/lib/routes.ts'
const SSR_ENTRY = '../dist-ssr/entry-server.js'
const { render } = (await import(SSR_ENTRY)) as {
render: (location: string) => string
}
const DIST = join(import.meta.dirname, '..', 'dist')
const SITE_URL = 'https://re-invention.nl/soulseek-rs/'
const escapeAttr = (value: string): string =>
value.replace(/&/g, '&').replace(/"/g, '"').replace(/
html.replace(
new RegExp(`( {
const canonical = meta.path === '/' ? SITE_URL : `${SITE_URL}${meta.path.slice(1)}/`
let out = html.replace(
/
[\s\S]*?<\/title>/,
`${escapeAttr(meta.title)}`,
)
out = setMeta(out, 'name', 'description', meta.description)
out = setMeta(out, 'property', 'og:title', meta.title)
out = setMeta(out, 'property', 'og:description', meta.description)
out = setMeta(out, 'property', 'og:url', canonical)
return out.replace(
/( => {
const html = applyMeta(template, meta).replace(
'',
`${render(location)}
`,
)
const target = join(DIST, outPath)
await mkdir(dirname(target), { recursive: true })
await writeFile(target, html, 'utf8')
console.log(` ${outPath}`)
}
const template = await readFile(join(DIST, 'index.html'), 'utf8')
console.log('prerendering:')
for (const route of ROUTES) {
const outPath = route.path === '/' ? 'index.html' : `${route.path.slice(1)}/index.html`
await writePage(template, route, outPath, route.path)
}
await writePage(template, NOT_FOUND_META, '404.html', '/__not_found__')
console.log(`done — ${String(ROUTES.length + 1)} pages`)