|
| 1 | +/** |
| 2 | + * Pre-render all docs website routes to static HTML files. |
| 3 | + * |
| 4 | + * This script runs after `vite build` (client + server bundles) and uses |
| 5 | + * the SSR server entry to render each route into its own index.html file, |
| 6 | + * making the site compatible with static hosting (e.g., GitHub Pages). |
| 7 | + * |
| 8 | + * Usage: node scripts/prerender.mjs |
| 9 | + */ |
| 10 | +import fs from 'node:fs' |
| 11 | +import path from 'node:path' |
| 12 | +import { fileURLToPath } from 'node:url' |
| 13 | + |
| 14 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 15 | +const buildDir = path.join(__dirname, '..', 'build', 'client') |
| 16 | + |
| 17 | +// All routes to pre-render (app-relative paths, without base path) |
| 18 | +const routes = [ |
| 19 | + '/', |
| 20 | + '/getting-started', |
| 21 | + '/api/signal', |
| 22 | + '/api/computed', |
| 23 | + '/api/effect', |
| 24 | + '/examples', |
| 25 | + '/release-notes', |
| 26 | +] |
| 27 | + |
| 28 | +// Suppress expected SSR errors from client-only code |
| 29 | +process.on('uncaughtException', (err) => { |
| 30 | + if (err.message?.includes('document is not defined') || |
| 31 | + err.message?.includes('window is not defined')) { |
| 32 | + return |
| 33 | + } |
| 34 | + console.error('Uncaught exception:', err) |
| 35 | + process.exit(1) |
| 36 | +}) |
| 37 | + |
| 38 | +async function prerender() { |
| 39 | + // Read the built index.html template (produced by vite build) |
| 40 | + const template = fs.readFileSync(path.join(buildDir, 'index.html'), 'utf-8') |
| 41 | + |
| 42 | + // Load the SSR server entry (built by vite build --ssr) |
| 43 | + const { render } = await import('../build/server/EntryServer.res.js') |
| 44 | + |
| 45 | + console.log(`Pre-rendering ${routes.length} routes...\n`) |
| 46 | + |
| 47 | + for (const route of routes) { |
| 48 | + // Render the app HTML for this route |
| 49 | + const appHtml = render(route) |
| 50 | + |
| 51 | + // Inject into the template |
| 52 | + const html = template.replace('<!--ssr-outlet-->', appHtml) |
| 53 | + |
| 54 | + // Write to the correct directory structure |
| 55 | + // e.g., "/" → build/client/index.html (already exists, overwrite) |
| 56 | + // "/getting-started" → build/client/getting-started/index.html |
| 57 | + const filePath = route === '/' |
| 58 | + ? path.join(buildDir, 'index.html') |
| 59 | + : path.join(buildDir, route, 'index.html') |
| 60 | + |
| 61 | + // Ensure directory exists |
| 62 | + const dir = path.dirname(filePath) |
| 63 | + fs.mkdirSync(dir, { recursive: true }) |
| 64 | + |
| 65 | + fs.writeFileSync(filePath, html) |
| 66 | + console.log(` ${route} → ${path.relative(buildDir, filePath)}`) |
| 67 | + } |
| 68 | + |
| 69 | + console.log('\nPre-rendering complete!') |
| 70 | +} |
| 71 | + |
| 72 | +prerender() |
0 commit comments