feat: prerender public site pages

This commit is contained in:
2026-05-04 16:29:50 -04:00
parent 55d8acef4c
commit 4fba72e99c
14 changed files with 380 additions and 5 deletions

View File

@@ -0,0 +1,34 @@
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import { dirname, resolve } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
const publicRoutes = ['/', '/product', '/pricing', '/blogs', '/guides'];
const rootDir = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const distDir = resolve(rootDir, 'dist');
const ssrEntry = resolve(rootDir, 'dist-ssr/entry-public-ssr.js');
const templatePath = resolve(distDir, 'index.html');
const template = await readFile(templatePath, 'utf8');
const { render } = await import(pathToFileURL(ssrEntry));
const baseTemplate = template.replace('<title>Socialize</title>', '');
function outputPathForRoute(route) {
if (route === '/') {
return resolve(distDir, 'index.html');
}
return resolve(distDir, route.replace(/^\//, ''), 'index.html');
}
for (const route of publicRoutes) {
const { appHtml, headTags } = await render(route);
const html = baseTemplate
.replace('</head>', `${headTags.headTags}\n</head>`)
.replace('<div id="app"></div>', `<div id="app">${appHtml}</div>`);
const outputPath = outputPathForRoute(route);
await mkdir(dirname(outputPath), { recursive: true });
await writeFile(outputPath, html);
console.log(`Prerendered ${route}`);
}