-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprerender.js
More file actions
54 lines (37 loc) · 1.54 KB
/
Copy pathprerender.js
File metadata and controls
54 lines (37 loc) · 1.54 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import fs from "node:fs";
import path from "node:path";
import {fileURLToPath} from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const toAbsolute = (p) => path.resolve(__dirname, p);
const template = fs.readFileSync(toAbsolute("dist/client/index.html"), "utf8");
const {render} = await import("./dist/server/EntryServer.js");
const routesToPrerender = ["/", "/aefaq", "/prfaq", "/psfaq", "/rules", "/links", "/404"];
(async () => {
for (const url of routesToPrerender) {
const context = {};
const {html} = await render(url, context);
const {helmet} = context;
let htmlWithMeta = template
.replace(/<title>.*<\/title>/, `${helmet.title.toString()}`)
.replace(/<\/head>/, `${helmet.meta.toString()}${helmet.link.toString()}</head>`)
.replace("<!--app-html-->", html);
if (url.endsWith("404")) {
htmlWithMeta = htmlWithMeta.replace(
'<meta name="prerender-status-code" content="200">',
'<meta name="prerender-status-code" content="404">'
);
}
if (["/aefaq", "/prfaq", "/psfaq"].includes(url)) {
htmlWithMeta = htmlWithMeta.replaceAll("<details", "<details open");
}
const filePath = toAbsolute(
url === "/" ? "dist/client/index.html" : `dist/client${url}.html`
);
const dirPath = path.dirname(filePath);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, {recursive: true});
}
fs.writeFileSync(filePath, htmlWithMeta);
console.log(`prerendered: ${url} to ${path.relative(__dirname, filePath)}`);
}
})();