-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathprerender.ts
360 lines (310 loc) · 10.2 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import path from "node:path";
import { promises as fs } from "node:fs";
import MagicString from "magic-string";
import { parse as htmlParse } from "node-html-parser";
import type { Plugin, ResolvedConfig } from "vite";
// Vite re-exports Rollup's type defs in newer versions,
// merge into above type import when we bump the Vite devDep
import type { InputOption, OutputAsset, OutputChunk } from "rollup";
interface HeadElement {
type: string;
props: Record<string, string>;
children?: string;
}
interface Head {
lang: string;
title: string;
elements: Set<HeadElement>;
}
interface PrerenderedRoute {
url: string;
_discoveredBy?: PrerenderedRoute;
}
function enc(str: string) {
return str
.replace(/&/g, "&")
.replace(/"/g, """)
.replace(/</g, "<")
.replace(/>/g, ">");
}
function serializeElement(
element: HeadElement | HeadElement[] | string,
): string {
if (element == null) return "";
if (typeof element !== "object") return String(element);
if (Array.isArray(element)) return element.map(serializeElement).join("");
const type = element.type;
let s = `<${type}`;
const props = element.props || {};
let children = element.children;
for (const prop of Object.keys(props)) {
const value = props[prop];
// Filter out empty values:
if (value == null) continue;
if (prop === "children" || prop === "textContent") children = value;
else s += ` ${prop}="${enc(value)}"`;
}
s += ">";
if (!/link|meta|base/.test(type)) {
if (children) s += serializeElement(children);
s += `</${type}>`;
}
return s;
}
interface PrerenderPluginOptions {
prerenderScript?: string;
renderTarget?: string;
additionalPrerenderRoutes?: string[];
}
export function PrerenderPlugin({
prerenderScript,
renderTarget,
additionalPrerenderRoutes,
}: PrerenderPluginOptions = {}): Plugin {
const preloadHelperId = "vite/preload-helper";
let viteConfig = {} as ResolvedConfig;
renderTarget ||= "body";
additionalPrerenderRoutes ||= [];
/**
* From the non-external scripts in entry HTML document, find the one (if any)
* that provides a `prerender` export
*/
const getPrerenderScriptFromHTML = async (input: InputOption) => {
// prettier-ignore
const entryHtml =
typeof input === "string"
? input
: Array.isArray(input)
? input.find(i => /html$/.test(i))
: Object.values(input).find(i => /html$/.test(i));
if (!entryHtml) throw new Error("Unable to detect entry HTML");
const htmlDoc = htmlParse(await fs.readFile(entryHtml, "utf-8"));
const entryScriptTag = htmlDoc
.getElementsByTagName("script")
.find(s => s.hasAttribute("prerender"));
if (!entryScriptTag)
throw new Error("Unable to detect prerender entry script");
const entrySrc = entryScriptTag.getAttribute("src");
if (!entrySrc || /^https:/.test(entrySrc))
throw new Error(
"Prerender entry script must have a `src` attribute and be local",
);
return path.join(viteConfig.root, entrySrc);
};
return {
name: "preact:prerender",
apply: "build",
enforce: "post",
configResolved(config) {
viteConfig = config;
},
async options(opts) {
if (!opts.input) return;
if (!prerenderScript) {
prerenderScript = await getPrerenderScriptFromHTML(opts.input);
}
// prettier-ignore
opts.input =
typeof opts.input === "string"
? [opts.input, prerenderScript]
: Array.isArray(opts.input)
? [...opts.input, prerenderScript]
: { ...opts.input, prerenderEntry: prerenderScript };
opts.preserveEntrySignatures = "allow-extension";
},
// Injects a window check into Vite's preload helper, instantly resolving
// the module rather than attempting to add a <link> to the document.
transform(code, id) {
// Vite keeps changing up the ID, best we can do for cross-version
// compat is an `includes`
if (id.includes(preloadHelperId)) {
// Through v5.0.4
// https://github.com/vitejs/vite/blob/b93dfe3e08f56cafe2e549efd80285a12a3dc2f0/packages/vite/src/node/plugins/importAnalysisBuild.ts#L95-L98
const s = new MagicString(code);
s.replace(
`if (!__VITE_IS_MODERN__ || !deps || deps.length === 0) {`,
`if (!__VITE_IS_MODERN__ || !deps || deps.length === 0 || typeof window === 'undefined') {`,
);
// 5.0.5+
// https://github.com/vitejs/vite/blob/c902545476a4e7ba044c35b568e73683758178a3/packages/vite/src/node/plugins/importAnalysisBuild.ts#L93
s.replace(
`if (__VITE_IS_MODERN__ && deps && deps.length > 0) {`,
`if (__VITE_IS_MODERN__ && deps && deps.length > 0 && typeof window !== 'undefined') {`,
);
return {
code: s.toString(),
map: s.generateMap({ hires: true }),
};
}
},
async generateBundle(_opts, bundle) {
// @ts-ignore
globalThis.location = {};
// @ts-ignore
globalThis.self = globalThis;
// Local, fs-based fetch implementation for prerendering
const nodeFetch = globalThis.fetch;
// @ts-ignore
globalThis.fetch = async (url: string, opts: RequestInit | undefined) => {
if (/^\//.test(url)) {
const text = () =>
fs.readFile(
`${path.join(
viteConfig.root,
viteConfig.build.outDir,
)}/${url.replace(/^\//, "")}`,
"utf-8",
);
return { text, json: () => text().then(JSON.parse) };
}
return nodeFetch(url, opts);
};
// Grab the generated HTML file, which we'll use as a template:
const tpl = (bundle["index.html"] as OutputAsset).source as string;
let htmlDoc = htmlParse(tpl);
// Create a tmp dir to allow importing & consuming the built modules,
// before Rollup writes them to the disk
const tmpDir = path.join(
viteConfig.root,
"node_modules",
"@preact/preset-vite",
"headless-prerender",
);
try {
await fs.rm(tmpDir, { recursive: true });
} catch (e: any) {
if (e.code !== "ENOENT") throw e;
}
await fs.mkdir(tmpDir, { recursive: true });
await fs.writeFile(
path.join(tmpDir, "package.json"),
JSON.stringify({ type: "module" }),
);
let prerenderEntry;
for (const output of Object.keys(bundle)) {
if (!/\.js$/.test(output) || bundle[output].type !== "chunk") continue;
await fs.writeFile(
path.join(tmpDir, path.basename(output)),
(bundle[output] as OutputChunk).code,
);
if ((bundle[output] as OutputChunk).exports?.includes("prerender")) {
prerenderEntry = bundle[output];
}
}
if (!prerenderEntry) {
this.error("Cannot detect module with `prerender` export");
}
let head: Head = { lang: "", title: "", elements: new Set() };
let prerender;
try {
const m = await import(
`file://${path.join(tmpDir, path.basename(prerenderEntry!.fileName))}`
);
prerender = m.prerender;
} catch (e) {
const isReferenceError = e instanceof ReferenceError;
const message = `
${e}
This ${
isReferenceError ? "is most likely" : "could be"
} caused by using DOM/Web APIs which are not available
available to the prerendering process which runs in Node. Consider
wrapping the offending code in a window check like so:
if (typeof window !== "undefined") {
// do something in browsers only
}
`.replace(/^\t{5}/gm, "");
this.error(message);
}
if (typeof prerender !== "function") {
this.error("Detected `prerender` export, but it is not a function");
}
// We start by pre-rendering the home page.
// Links discovered during pre-rendering get pushed into the list of routes.
const seen = new Set(["/", ...additionalPrerenderRoutes!]);
let routes: PrerenderedRoute[] = [...seen].map(link => ({ url: link }));
for (const route of routes) {
if (!route.url) continue;
const outDir = route.url.replace(/(^\/|\/$)/g, "");
const assetName = path.join(
outDir,
outDir.endsWith(".html") ? "" : "index.html",
);
// Update `location` to current URL so routers can use things like `location.pathname`
const u = new URL(route.url, "http://localhost");
for (const i in u) {
try {
// @ts-ignore
globalThis.location[i] = String(u[i]);
} catch {}
}
const result = await prerender({ ssr: true, url: route.url, route });
if (result == null) continue;
// Reset HTML doc & head data
htmlDoc = htmlParse(tpl);
head = { lang: "", title: "", elements: new Set() };
// Add any discovered links to the list of routes to pre-render:
if (result.links) {
for (let url of result.links) {
const parsed = new URL(url, "http://localhost");
url = parsed.pathname;
// ignore external links and ones we've already picked up
if (seen.has(url) || parsed.origin !== "http://localhost") continue;
seen.add(url);
routes.push({ url, _discoveredBy: route });
}
}
let body;
if (result && typeof result === "object") {
if (result.html) body = result.html;
if (result.head) {
head = result.head;
}
} else {
body = result;
}
const htmlHead = htmlDoc.querySelector("head");
if (htmlHead) {
if (head.title) {
const htmlTitle = htmlHead.querySelector("title");
htmlTitle
? htmlTitle.set_content(enc(head.title))
: htmlHead.insertAdjacentHTML(
"afterbegin",
`<title>${enc(head.title)}</title>`,
);
}
if (head.lang) {
htmlDoc.querySelector("html")!.setAttribute("lang", enc(head.lang));
}
if (head.elements) {
// Inject HTML links at the end of <head> for any stylesheets injected during rendering of the page:
htmlHead.insertAdjacentHTML(
"beforeend",
Array.from(
new Set(Array.from(head.elements).map(serializeElement)),
).join("\n"),
);
}
}
const target = htmlDoc.querySelector(renderTarget!);
if (!target)
this.error(
result.renderTarget == "body"
? "`renderTarget` was not specified in plugin options and <body> does not exist in input HTML template"
: `Unable to detect prerender renderTarget "${result.selector}" in input HTML template`,
);
target.insertAdjacentHTML("afterbegin", body);
// Add generated HTML to compilation:
if (route.url === "/")
(bundle["index.html"] as OutputAsset).source = htmlDoc.toString();
else
this.emitFile({
type: "asset",
fileName: assetName,
source: htmlDoc.toString(),
});
}
},
};
}