-
Notifications
You must be signed in to change notification settings - Fork 0
/
prerender.js
39 lines (30 loc) · 1.12 KB
/
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
31
32
33
34
35
36
37
38
39
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { readdirSync, readFileSync } from "node:fs";
import { mkdirSync, writeFileSync } from "node:fs";
const __dirname = dirname(fileURLToPath(import.meta.url));
const template = readFileSync("dist/static/index.html", "utf-8");
const { render } = await import("./dist/server/entry-server.js");
/** @type {Array<string>} */
const routesToPrerender = readdirSync("src/pages", { recursive: true })
.filter((file) => file.endsWith(".tsx"))
.map((file) => {
const path = `/${file.slice(0, -4)}`;
if (path === "/index") {
return "/";
} else if (path.endsWith("/index")) {
return path.slice(0, -6);
} else {
return path;
}
});
async function prerender() {
routesToPrerender.forEach((route) => {
const filePath = join(__dirname, "dist", "static", `${route === "/" ? "index" : route}.html`);
mkdirSync(dirname(filePath), { recursive: true });
const appHtml = render(route);
const html = template.replace(`<!--app-html-->`, appHtml);
writeFileSync(filePath, html);
});
}
await prerender();