-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprerender.mjs
59 lines (48 loc) · 1.52 KB
/
prerender.mjs
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
55
56
57
58
59
import puppeteer from "puppeteer";
import blogIndex from "./public/blog/index.json" assert { type: "json" };
import fs from "fs";
import { build, preview } from "vite";
// Config
const staticRoutes = ["", "blog"];
const dynamicRoutes = blogIndex.map((entry) => `blog/${entry.id}`);
// Build
console.log("Building app");
await build();
// Startup
console.log("Starting preview server");
const previewServer = await preview();
const baseUrl = previewServer.resolvedUrls.local[0];
const outputDir = previewServer.config.build.outDir;
console.log("Starting browser");
const browser = await puppeteer.launch({
headless: true,
args: ["--no-sandbox"],
});
// Rendering
for (const route of [...staticRoutes, ...dynamicRoutes]) {
const pageUrl = `${baseUrl}${route}`;
const fileDir = `${outputDir}/${route}`;
const filePath = `${fileDir}/index.html`;
console.log(`Rendering page ${pageUrl}`);
const page = await browser.newPage();
await page.goto(pageUrl, { waitUntil: "networkidle0" });
const pageHtml = await page.content();
if (!fs.existsSync(fileDir)) {
console.log(`Creating directory ${fileDir}`);
fs.mkdirSync(fileDir, { recursive: true });
}
console.log(`Writing file ${filePath}`);
fs.writeFile(filePath, pageHtml, (err) => {
if (err) {
console.error(err);
return;
}
console.log(`Done writing file ${filePath}`);
});
await page.close();
}
// Teardown
console.log("Closing browser");
await browser.close();
console.log("Closing preview server");
previewServer.httpServer.close();