-
Notifications
You must be signed in to change notification settings - Fork 538
/
prerender.ts
250 lines (218 loc) · 6.9 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import { pathToFileURL } from "node:url";
import { resolve, join } from "pathe";
import { joinURL, parseURL, withBase, withoutBase } from "ufo";
import chalk from "chalk";
import { createRouter as createRadixRouter, toRouteMatcher } from "radix3";
import { defu } from "defu";
import { createNitro } from "./nitro";
import { build } from "./build";
import type {
Nitro,
NitroRouteRules,
PrerenderGenerateRoute,
PrerenderRoute,
} from "./types";
import { writeFile } from "./utils";
import { compressPublicAssets } from "./compress";
const allowedExtensions = new Set(["", ".json"]);
export async function prerender(nitro: Nitro) {
if (nitro.options.noPublicDir) {
console.warn(
"[nitro] Skipping prerender since `noPublicDir` option is enabled."
);
return;
}
// Initial list of routes to prerender
const routes = new Set(nitro.options.prerender.routes);
// Extend with static prerender route rules
const prerenderRulePaths = Object.entries(nitro.options.routeRules)
.filter(([path, options]) => options.prerender && !path.includes("*"))
.map((e) => e[0]);
for (const route of prerenderRulePaths) {
routes.add(route);
}
// Crawl / at least if no routes are defined
if (nitro.options.prerender.crawlLinks && routes.size === 0) {
routes.add("/");
}
// Allow extending prereneder routes
await nitro.hooks.callHook("prerender:routes", routes);
// Skip if no prerender routes specified
if (routes.size === 0) {
return;
}
// Build with prerender preset
nitro.logger.info("Initializing prerenderer");
nitro._prerenderedRoutes = [];
const nitroRenderer = await createNitro({
...nitro.options._config,
rootDir: nitro.options.rootDir,
logLevel: 0,
preset: "nitro-prerender",
});
await build(nitroRenderer);
// Import renderer entry
const serverEntrypoint = resolve(
nitroRenderer.options.output.serverDir,
"index.mjs"
);
const { localFetch } = await import(pathToFileURL(serverEntrypoint).href);
// Create route rule matcher
const _routeRulesMatcher = toRouteMatcher(
createRadixRouter({ routes: nitro.options.routeRules })
);
const _getRouteRules = (path: string) =>
defu({}, ..._routeRulesMatcher.matchAll(path).reverse()) as NitroRouteRules;
// Start prerendering
const generatedRoutes = new Set();
const canPrerender = (route: string = "/") => {
if (generatedRoutes.has(route)) {
return false;
}
if (route.length > 250) {
return false;
}
for (const ignore of nitro.options.prerender.ignore) {
if (route.startsWith(ignore)) {
return false;
}
}
if (_getRouteRules(route).prerender === false) {
return false;
}
return true;
};
const generateRoute = async (route: string) => {
const start = Date.now();
// Check if we should render route
if (!canPrerender(route)) {
return;
}
generatedRoutes.add(route);
routes.delete(route);
// Create result object
const _route: PrerenderGenerateRoute = { route };
// Fetch the route
const res = await (localFetch(withBase(route, nitro.options.baseURL), {
headers: { "x-nitro-prerender": route },
}) as ReturnType<typeof fetch>);
_route.data = await res.arrayBuffer();
Object.defineProperty(_route, "contents", {
get: () => {
if (!(_route as any)._contents) {
(_route as any)._contents = new TextDecoder("utf8").decode(
new Uint8Array(_route.data)
);
}
return (_route as any)._contents;
},
set(value: string) {
(_route as any)._contents = value;
_route.data = new TextEncoder().encode(value);
},
});
if (res.status !== 200) {
_route.error = new Error(`[${res.status}] ${res.statusText}`) as any;
_route.error.statusCode = res.status;
_route.error.statusMessage = res.statusText;
}
// Write to the file
const isImplicitHTML =
!route.endsWith(".html") &&
(res.headers.get("content-type") || "").includes("html");
const routeWithIndex = route.endsWith("/") ? route + "index" : route;
_route.fileName = isImplicitHTML
? joinURL(route, "index.html")
: routeWithIndex;
_route.fileName = withoutBase(_route.fileName, nitro.options.baseURL);
await nitro.hooks.callHook("prerender:generate", _route, nitro);
// Check if route skipped or has errors
if (_route.skip || _route.error) {
return;
}
const filePath = join(nitro.options.output.publicDir, _route.fileName);
await writeFile(filePath, Buffer.from(_route.data));
nitro._prerenderedRoutes.push(_route);
// Crawl route links
if (!_route.error && isImplicitHTML) {
const extractedLinks = extractLinks(
_route.contents,
route,
res,
nitro.options.prerender.crawlLinks
);
for (const _link of extractedLinks) {
if (canPrerender(_link)) {
routes.add(_link);
}
}
}
_route.generateTimeMS = Date.now() - start;
return _route;
};
nitro.logger.info(
nitro.options.prerender.crawlLinks
? `Prerendering ${routes.size} initial routes with crawler`
: `Prerendering ${routes.size} routes`
);
for (let i = 0; i < 100 && routes.size > 0; i++) {
for (const route of routes) {
const _route = await generateRoute(route).catch(
(error) => ({ route, error } as PrerenderRoute)
);
// Skipped (not allowed or duplicate)
if (!_route) {
continue;
}
await nitro.hooks.callHook("prerender:route", _route);
nitro.logger.log(
chalk[_route.error ? "yellow" : "gray"](
` ├─ ${_route.route} (${_route.generateTimeMS}ms) ${
_route.error ? `(${_route.error})` : ""
}`
)
);
}
}
if (nitro.options.compressPublicAssets) {
await compressPublicAssets(nitro);
}
}
const LINK_REGEX = /href=["']?([^ "'>]+)/g;
function extractLinks(
html: string,
from: string,
res: Response,
crawlLinks: boolean
) {
const links: string[] = [];
const _links: string[] = [];
// Extract from any <TAG href=""> to crawl
if (crawlLinks) {
_links.push(
...[...html.matchAll(LINK_REGEX)]
.map((m) => m[1])
.filter((link) => allowedExtensions.has(getExtension(link)))
);
}
// Extract from x-nitro-prerender headers
const header = res.headers.get("x-nitro-prerender") || "";
_links.push(...header.split(",").map((i) => i.trim()));
for (const link of _links.filter(Boolean)) {
const parsed = parseURL(link);
if (parsed.protocol) {
continue;
}
let { pathname } = parsed;
if (!pathname.startsWith("/")) {
const fromURL = new URL(from, "http://localhost");
pathname = new URL(pathname, fromURL).pathname;
}
links.push(pathname);
}
return links;
}
const EXT_REGEX = /\.[\da-z]+$/;
function getExtension(path: string): string {
return (path.match(EXT_REGEX) || [])[0] || "";
}