-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprerender.js
30 lines (23 loc) · 918 Bytes
/
prerender.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const toAbsolute = (p) => path.resolve(__dirname, p);
const template = fs.readFileSync(toAbsolute('dist/static/index.html'), 'utf-8');
const render = (await import('./dist/server/entry-server.js')).render;
// determine routes to pre-render from src/pages
const routesToPrerender = fs
.readdirSync(toAbsolute('src/pages'))
.map((file) => {
const name = file.replace(/\.tsx$/, '').toLowerCase();
return name === 'about' ? `/` : `/${name}`;
});
(async () => {
// pre-render each route...
for (const url of routesToPrerender) {
const appHtml = render(url);
const html = template.replace(`App is loading...`, appHtml);
const filePath = `dist/static${url === '/' ? '/index' : url}.html`;
fs.writeFileSync(toAbsolute(filePath), html);
}
})();