#!/usr/bin/env node
/**
* prerender-seo.mjs — Generates route-specific HTML for SEO-important pages.
*
* How it works:
* 1. Starts a Vite dev server in SSR mode (no HTTP server, just the transform pipeline)
* 2. Loads src/entry-server.tsx through Vite — handles TSX, CSS, SVG, path aliases
* 3. For each route with seoMeta in seoRoutes.ts:
* a. Renders the React component to HTML via renderToString
* b. Replaces
, , OG/Twitter tags, canonical URL from seoMeta
* c. Injects the rendered HTML into #root-seo
* 4. Writes to dist/{route}/index.html
*
* nginx's try_files ($uri/) serves these automatically.
*
* Run after `vite build`: node scripts/prerender-seo.mjs
*/
import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { createServer } from 'vite';
const __dirname = dirname(fileURLToPath(import.meta.url));
const distDir = join(__dirname, '..', 'dist');
// Check that dist exists (vite build must have run first)
if (!existsSync(join(distDir, 'index.html'))) {
console.error('❌ dist/index.html not found. Run `vite build` first.');
process.exit(1);
}
const baseHtml = readFileSync(join(distDir, 'index.html'), 'utf-8');
const DOMAIN = 'https://velxio.dev';
// nginx serves each prerendered route as `/index.html` and
// 301-redirects the slash-less URL to add the trailing slash. Canonical +
// og:url must therefore use the slash form (== the served URL == the sitemap
// entry), or Google sees a canonical that points to a redirecting URL.
const withSlash = (u) => (u.endsWith('/') ? u : `${u}/`);
/**
* Swap the whole static #root-seo block (the hand-written homepage fallback
* in index.html) for this page's SSR body. Located by string search, not by
* a regex anchored on a following