-
Notifications
You must be signed in to change notification settings - Fork 1
/
prerender.ts
36 lines (33 loc) · 1.06 KB
/
prerender.ts
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
import * as fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const toAbsolute = (p: string) => path.resolve(__dirname, p);
const ruTemplate = fs.readFileSync(
toAbsolute("dist/static/index.html"),
"utf-8",
);
const enTemplate = fs.readFileSync(
toAbsolute("dist/static/index-en.html"),
"utf-8",
);
(async () => {
async function preRenderRoute(
outFile: string,
template: string,
lang: string,
) {
const context = {};
const { render, CURRENT_VERSION } = await import(
"./dist/server/entry-server.js"
);
const appHtml = await render("/", lang);
const html1 = template.replace(`<!--app-html-->`, appHtml);
const html = html1.replace(`<!--version-->`, `v${CURRENT_VERSION}`);
const filePath = `dist/static/${outFile}.html`;
fs.writeFileSync(toAbsolute(filePath), html);
console.log("📦 pre-rendered:", filePath);
}
await preRenderRoute("index", ruTemplate, "ru");
await preRenderRoute("index-en", enTemplate, "en");
})();