feat: prerender public site pages
This commit is contained in:
34
frontend/scripts/prerender-public.mjs
Normal file
34
frontend/scripts/prerender-public.mjs
Normal 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}`);
|
||||
}
|
||||
Reference in New Issue
Block a user