@sigil-dev/grimoire 0.9.28 → 0.9.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/sigil.ts +4 -0
- package/index.ts +16 -10
- package/package.json +14 -6
- package/server.ts +2 -2
- package/src/adapters/bun.ts +17 -17
- package/src/adapters/static.ts +41 -41
- package/src/adapters/types.ts +36 -36
- package/src/base.ts +4 -1
- package/src/client/csrf.ts +47 -47
- package/src/client/deferred.ts +1 -4
- package/src/client/enhance.ts +160 -160
- package/src/client/router.ts +927 -926
- package/src/config-file.ts +84 -0
- package/src/dev/compile-module.ts +199 -193
- package/src/dev/hmr-client.ts +229 -229
- package/src/dev/hmr-server.ts +274 -266
- package/src/dev/loader.ts +144 -144
- package/src/dev/runtime-bundle.ts +5 -3
- package/src/duration.ts +46 -0
- package/src/env/modules.ts +62 -62
- package/src/env/required.ts +42 -42
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +105 -105
- package/src/logger/transports.ts +117 -117
- package/src/plugin-order.ts +88 -0
- package/src/rendering/head.ts +49 -49
- package/src/rendering/hydrate.ts +162 -162
- package/src/rendering/images.ts +43 -43
- package/src/rendering/preload.ts +22 -8
- package/src/rendering/ssrPlugin.ts +107 -107
- package/src/rendering/timing.ts +40 -40
- package/src/routing/route-ids.ts +65 -0
- package/src/routing/route.ts +65 -0
- package/src/routing/router.ts +84 -32
- package/src/routing/scanner.ts +18 -5
- package/src/routing/transform-routes.ts +102 -102
- package/src/server/after-response.ts +206 -206
- package/src/server/asset-cache.ts +57 -57
- package/src/server/build.ts +304 -295
- package/src/server/cached.ts +114 -114
- package/src/server/client-address.ts +82 -82
- package/src/server/client-dir.ts +63 -63
- package/src/server/client-entry.ts +44 -44
- package/src/server/context.ts +18 -10
- package/src/server/cookie-utils.ts +80 -80
- package/src/server/coordinator.ts +476 -476
- package/src/server/hooks.ts +122 -122
- package/src/server/index.ts +8 -0
- package/src/server/plugins.ts +172 -172
- package/src/server/prerender.ts +473 -473
- package/src/server/reserved.ts +17 -17
- package/src/server/resolver.ts +149 -123
- package/src/server/rewrite.ts +28 -28
- package/src/server/warm.ts +9 -3
- package/src/server/with-headers.ts +27 -27
- package/src/server/worker.ts +2 -6
- package/src/server-only.ts +78 -0
- package/src/testing/dom.ts +73 -73
- package/src/testing/index.ts +169 -169
- package/src/testing/server.ts +1 -0
- package/src/typegen/index.ts +97 -2
- package/src/types.ts +140 -19
- package/test/after-response.test.ts +132 -132
- package/test/asset-cache-policy.test.ts +6 -2
- package/test/base.test.ts +20 -6
- package/test/cached.test.ts +126 -126
- package/test/client-address.test.ts +78 -78
- package/test/client-csrf.test.ts +61 -61
- package/test/client-dir.test.ts +101 -101
- package/test/client-router.test.ts +37 -5
- package/test/config-file.test.ts +84 -0
- package/test/coordinator.test.ts +9 -2
- package/test/deferred.test.ts +4 -2
- package/test/dev-loader.test.ts +30 -30
- package/test/env.test.ts +171 -171
- package/test/force-production.test.ts +7 -2
- package/test/images.test.ts +20 -8
- package/test/locals-header.test.ts +61 -61
- package/test/middleware.test.ts +243 -243
- package/test/parser-plugins.test.ts +98 -92
- package/test/plugin-handle.test.ts +62 -62
- package/test/plugin-order.test.ts +98 -0
- package/test/plugin-routes.test.ts +47 -42
- package/test/plugin-services-context.test.ts +7 -2
- package/test/preload.test.ts +4 -1
- package/test/prerender.test.ts +3 -1
- package/test/rendering.test.ts +3 -2
- package/test/required-env.test.ts +32 -32
- package/test/resolver-windows.test.ts +22 -23
- package/test/rewrite-html.test.ts +90 -90
- package/test/route-group-layouts.test.ts +38 -36
- package/test/route.test.ts +103 -0
- package/test/routing.test.ts +225 -197
- package/test/scanning.test.ts +53 -33
- package/test/server-context.test.ts +22 -0
- package/test/server-only.test.ts +110 -0
- package/test/server.test.ts +640 -640
- package/test/services.test.ts +3 -3
- package/test/streaming.test.ts +11 -6
- package/test/testing-helpers.test.ts +166 -166
- package/test/typegen.test.ts +209 -112
- package/test/warm.test.ts +96 -96
- package/test/worker-context.test.ts +59 -59
- package/test/workspace-files.test.ts +26 -10
- package/test/workspace-versions.test.ts +146 -131
package/src/server/prerender.ts
CHANGED
|
@@ -1,473 +1,473 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Prerendering: turn routes into files by sending requests through the real
|
|
3
|
-
* pipeline.
|
|
4
|
-
*
|
|
5
|
-
* Going through `app.fetch` and not calling the renderer directly is the point.
|
|
6
|
-
* `hooks.server.ts`, plugins, layouts and load functions all run exactly as
|
|
7
|
-
* they will in production, so a prerendered page cannot differ from the live
|
|
8
|
-
* one because a code path was skipped.
|
|
9
|
-
*
|
|
10
|
-
* The pure helpers at the top (path/file mapping, link extraction) are exported
|
|
11
|
-
* for tests. `prerenderSite` is the orchestration.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import { copyFile, cp, mkdir, rm, writeFile } from "node:fs/promises";
|
|
15
|
-
import {
|
|
16
|
-
basename,
|
|
17
|
-
dirname,
|
|
18
|
-
isAbsolute,
|
|
19
|
-
join,
|
|
20
|
-
relative,
|
|
21
|
-
resolve,
|
|
22
|
-
sep,
|
|
23
|
-
} from "node:path";
|
|
24
|
-
import type { AdapterContext } from "../adapters/types";
|
|
25
|
-
import { normalizeBase, stripBase, withBase } from "../base";
|
|
26
|
-
import { matchRoute } from "../routing/router";
|
|
27
|
-
import type { RouteFile, RouteTree } from "../routing/scanner";
|
|
28
|
-
import { CLIENT_URL, clientDir, DEV_FILES } from "./client-dir";
|
|
29
|
-
import { publicCssMap } from "./public-assets";
|
|
30
|
-
import { RESERVED_EXPORTS } from "./reserved";
|
|
31
|
-
|
|
32
|
-
// ---------------------------------------------------------------------------
|
|
33
|
-
// pure helpers
|
|
34
|
-
// ---------------------------------------------------------------------------
|
|
35
|
-
|
|
36
|
-
/** `/blog/:slug` + `{ slug: "a b" }` → `/blog/a%20b`. Null when a required param is missing. */
|
|
37
|
-
export function fillPattern(
|
|
38
|
-
pattern: string,
|
|
39
|
-
params: Record<string, string | undefined>,
|
|
40
|
-
): string | null {
|
|
41
|
-
const out: string[] = [];
|
|
42
|
-
for (const part of pattern.split("/").filter(Boolean)) {
|
|
43
|
-
if (part.startsWith("*")) {
|
|
44
|
-
const value = params[part.slice(1)];
|
|
45
|
-
if (value) {
|
|
46
|
-
out.push(
|
|
47
|
-
value
|
|
48
|
-
.split("/")
|
|
49
|
-
.map((s) => encodeURIComponent(s))
|
|
50
|
-
.join("/"),
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
} else if (part.startsWith(":")) {
|
|
54
|
-
const optional = part.endsWith("?");
|
|
55
|
-
const name = part.slice(1, optional ? -1 : undefined);
|
|
56
|
-
const value = params[name];
|
|
57
|
-
if (value === undefined || value === "") {
|
|
58
|
-
if (optional) continue;
|
|
59
|
-
return null;
|
|
60
|
-
}
|
|
61
|
-
out.push(encodeURIComponent(value));
|
|
62
|
-
} else {
|
|
63
|
-
out.push(part);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return `/${out.join("/")}`;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const HAS_EXTENSION = /\.[A-Za-z0-9]+$/;
|
|
70
|
-
|
|
71
|
-
/** The file a page at `pathname` is written to: `/` → `index.html`, `/a/b` → `a/b/index.html`. */
|
|
72
|
-
export function htmlFileFor(pathname: string): string {
|
|
73
|
-
const clean = pathname.replace(/^\/+|\/+$/g, "");
|
|
74
|
-
if (!clean) return "index.html";
|
|
75
|
-
if (/\.html?$/.test(clean)) return clean;
|
|
76
|
-
return `${clean}/index.html`;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Where the client router finds the navigation payload for `pathname` on a
|
|
81
|
-
* static host. The router builds the same string, so the two must agree.
|
|
82
|
-
*/
|
|
83
|
-
export function dataFileFor(pathname: string): string {
|
|
84
|
-
const clean = pathname.replace(/^\/+|\/+$/g, "");
|
|
85
|
-
return clean ? `${clean}/__data.json` : "__data.json";
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Same-origin page paths linked from `html`, normalised: no query, no hash, no
|
|
90
|
-
* trailing slash, nothing that points at a file with an extension (assets, feeds).
|
|
91
|
-
*/
|
|
92
|
-
export function extractLinks(
|
|
93
|
-
html: string,
|
|
94
|
-
origin: string,
|
|
95
|
-
base = "",
|
|
96
|
-
): string[] {
|
|
97
|
-
const found = new Set<string>();
|
|
98
|
-
for (const m of html.matchAll(/\shref\s*=\s*("([^"]*)"|'([^']*)')/gi)) {
|
|
99
|
-
const href = (m[2] ?? m[3] ?? "").trim();
|
|
100
|
-
let path: string | null = null;
|
|
101
|
-
if (href.startsWith("/") && !href.startsWith("//")) path = href;
|
|
102
|
-
else if (href.startsWith(`${origin}/`)) path = href.slice(origin.length);
|
|
103
|
-
if (path === null) continue;
|
|
104
|
-
path = path.split("#")[0]!.split("?")[0]!;
|
|
105
|
-
// a link outside the base is not one of this site's pages
|
|
106
|
-
if (base) {
|
|
107
|
-
const route = stripBase(path, base);
|
|
108
|
-
if (route === null) continue;
|
|
109
|
-
path = route;
|
|
110
|
-
}
|
|
111
|
-
if (path.length > 1) path = path.replace(/\/+$/, "");
|
|
112
|
-
if (!path) continue;
|
|
113
|
-
if (HAS_EXTENSION.test(path) && !/\.html?$/.test(path)) continue;
|
|
114
|
-
found.add(path);
|
|
115
|
-
}
|
|
116
|
-
return [...found];
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Tell the client router this site has no server to ask for navigation JSON.
|
|
121
|
-
* It then reads `<page>/__data.json`, which prerendering wrote beside the page.
|
|
122
|
-
*/
|
|
123
|
-
export function markStatic(html: string): string {
|
|
124
|
-
const tag = "<script>globalThis.__grimoire_static__=1</script>";
|
|
125
|
-
if (/<\/head>/i.test(html)) return html.replace(/<\/head>/i, `${tag}</head>`);
|
|
126
|
-
return tag + html;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function redirectPage(location: string): string {
|
|
130
|
-
const safe = location.replace(/&/g, "&").replace(/"/g, """);
|
|
131
|
-
return `<!doctype html><meta charset="utf-8"><meta http-equiv="refresh" content="0;url=${safe}"><link rel="canonical" href="${safe}"><a href="${safe}">Redirecting</a>`;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// ---------------------------------------------------------------------------
|
|
135
|
-
// route options
|
|
136
|
-
// ---------------------------------------------------------------------------
|
|
137
|
-
|
|
138
|
-
interface RouteOptions {
|
|
139
|
-
/** `export const prerender`, from the page, its server file or the nearest layout. */
|
|
140
|
-
prerender: boolean | undefined;
|
|
141
|
-
entries: (() => unknown) | undefined;
|
|
142
|
-
/** Names of exports that are actions: they need a server, so they cannot be prerendered. */
|
|
143
|
-
actions: string[];
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
type Importer = (path: string) => Promise<Record<string, unknown>>;
|
|
147
|
-
|
|
148
|
-
async function readOptions(
|
|
149
|
-
route: RouteFile,
|
|
150
|
-
tree: RouteTree,
|
|
151
|
-
load: Importer,
|
|
152
|
-
): Promise<RouteOptions> {
|
|
153
|
-
const layoutFor = (l: RouteFile) =>
|
|
154
|
-
l.path === "/" ||
|
|
155
|
-
route.path === l.path ||
|
|
156
|
-
route.path.startsWith(`${l.path}/`);
|
|
157
|
-
const pageServer = tree.routes.find(
|
|
158
|
-
(r) => r.type === "pageServer" && r.path === route.path,
|
|
159
|
-
);
|
|
160
|
-
// most specific first: page server, page, then layouts innermost to root
|
|
161
|
-
const sources = [
|
|
162
|
-
pageServer?.filePath,
|
|
163
|
-
route.filePath,
|
|
164
|
-
...tree.layouts
|
|
165
|
-
.filter(layoutFor)
|
|
166
|
-
.sort((a, b) => b.path.length - a.path.length)
|
|
167
|
-
.map((l) => l.filePath),
|
|
168
|
-
].filter((p): p is string => !!p);
|
|
169
|
-
|
|
170
|
-
let prerender: boolean | undefined;
|
|
171
|
-
let entries: RouteOptions["entries"];
|
|
172
|
-
const actions: string[] = [];
|
|
173
|
-
for (const file of sources) {
|
|
174
|
-
const mod = await load(file);
|
|
175
|
-
if (prerender === undefined && typeof mod.prerender === "boolean") {
|
|
176
|
-
prerender = mod.prerender;
|
|
177
|
-
}
|
|
178
|
-
if (!entries && typeof mod.entries === "function") {
|
|
179
|
-
entries = mod.entries as () => unknown;
|
|
180
|
-
}
|
|
181
|
-
if (file === pageServer?.filePath) {
|
|
182
|
-
for (const [name, value] of Object.entries(mod)) {
|
|
183
|
-
if (typeof value === "function" && !RESERVED_EXPORTS.has(name)) {
|
|
184
|
-
actions.push(name);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
return { prerender, entries, actions };
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// ---------------------------------------------------------------------------
|
|
193
|
-
// orchestration
|
|
194
|
-
// ---------------------------------------------------------------------------
|
|
195
|
-
|
|
196
|
-
export interface PrerenderOptions {
|
|
197
|
-
/** Directory the site is written to, relative to the project root. */
|
|
198
|
-
out: string;
|
|
199
|
-
/** What `url.origin` is while rendering. Default `http://localhost`. */
|
|
200
|
-
origin?: string;
|
|
201
|
-
/** Follow `<a href>` from rendered pages to find more pages. Default true. */
|
|
202
|
-
crawl?: boolean;
|
|
203
|
-
/** Extra paths to render. */
|
|
204
|
-
paths?: string[];
|
|
205
|
-
/** Fail the build when something cannot be a file. When false, warn and skip. Default true. */
|
|
206
|
-
strict?: boolean;
|
|
207
|
-
/** Parallel requests. Default 6. */
|
|
208
|
-
concurrency?: number;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
export interface PrerenderReport {
|
|
212
|
-
pages: string[];
|
|
213
|
-
problems: string[];
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
export async function prerenderSite(
|
|
217
|
-
ctx: AdapterContext,
|
|
218
|
-
opts: PrerenderOptions,
|
|
219
|
-
load: Importer = (p) => import(p),
|
|
220
|
-
): Promise<PrerenderReport> {
|
|
221
|
-
const { app, root, log } = ctx;
|
|
222
|
-
const tree = app.tree as RouteTree;
|
|
223
|
-
// `[id=int]` routes only match through their matcher; without it nothing
|
|
224
|
-
// that has one could be reached, linked to, or counted as rendered.
|
|
225
|
-
const matchers = app.matchers;
|
|
226
|
-
const origin = opts.origin ?? "http://localhost";
|
|
227
|
-
// requests carry the base; the files written do not, because the host
|
|
228
|
-
// serves the output directory *as* the base
|
|
229
|
-
const base = normalizeBase(app.config.base);
|
|
230
|
-
const strict = opts.strict ?? true;
|
|
231
|
-
const crawl = opts.crawl ?? true;
|
|
232
|
-
const concurrency = Math.max(1, opts.concurrency ?? 6);
|
|
233
|
-
const problems: string[] = [];
|
|
234
|
-
const outDir = resolve(root, opts.out);
|
|
235
|
-
assertSafeOut(root, outDir);
|
|
236
|
-
|
|
237
|
-
// ---- seeds ----------------------------------------------------------
|
|
238
|
-
const pageRoutes = tree.routes.filter(
|
|
239
|
-
(r) => r.type === "page" || r.type === "simple",
|
|
240
|
-
);
|
|
241
|
-
const queue: { path: string; from: string }[] = [];
|
|
242
|
-
const seen = new Set<string>();
|
|
243
|
-
const enqueue = (path: string, from: string) => {
|
|
244
|
-
if (seen.has(path)) return;
|
|
245
|
-
seen.add(path);
|
|
246
|
-
queue.push({ path, from });
|
|
247
|
-
};
|
|
248
|
-
|
|
249
|
-
const excluded = new Set<string>();
|
|
250
|
-
for (const route of pageRoutes) {
|
|
251
|
-
const o = await readOptions(route, tree, load);
|
|
252
|
-
if (o.prerender === false) {
|
|
253
|
-
excluded.add(route.path);
|
|
254
|
-
problems.push(
|
|
255
|
-
`${route.clientPath} sets prerender = false, so it cannot exist on a static host.`,
|
|
256
|
-
);
|
|
257
|
-
continue;
|
|
258
|
-
}
|
|
259
|
-
if (o.actions.length) {
|
|
260
|
-
excluded.add(route.path);
|
|
261
|
-
problems.push(
|
|
262
|
-
`${route.path} has server actions (${o.actions.join(", ")}) and a static site has no server to run them.`,
|
|
263
|
-
);
|
|
264
|
-
continue;
|
|
265
|
-
}
|
|
266
|
-
const dynamic =
|
|
267
|
-
route.paramNames.length > 0 ||
|
|
268
|
-
route.restParamNames.length > 0 ||
|
|
269
|
-
route.optionalParamNames.length > 0;
|
|
270
|
-
if (!dynamic) {
|
|
271
|
-
enqueue(route.path, route.clientPath);
|
|
272
|
-
} else if (o.entries) {
|
|
273
|
-
const list = await o.entries();
|
|
274
|
-
if (!Array.isArray(list)) {
|
|
275
|
-
problems.push(
|
|
276
|
-
`${route.clientPath}: entries() must return an array of param objects.`,
|
|
277
|
-
);
|
|
278
|
-
continue;
|
|
279
|
-
}
|
|
280
|
-
for (const params of list as Record<string, string>[]) {
|
|
281
|
-
const path = fillPattern(route.path, params ?? {});
|
|
282
|
-
if (path === null) {
|
|
283
|
-
problems.push(
|
|
284
|
-
`${route.clientPath}: entries() returned ${JSON.stringify(params)}, which is missing a param of "${route.path}".`,
|
|
285
|
-
);
|
|
286
|
-
} else enqueue(path, `${route.clientPath} entries()`);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
for (const path of opts.paths ?? []) enqueue(path, "config paths");
|
|
291
|
-
|
|
292
|
-
// +server.ts routes: only an explicit GET with `prerender = true` can be a file.
|
|
293
|
-
const files = new Map<string, string | Uint8Array>();
|
|
294
|
-
for (const server of tree.servers) {
|
|
295
|
-
const mod = await load(server.filePath);
|
|
296
|
-
const methods = Object.keys(mod).filter((k) =>
|
|
297
|
-
["POST", "PUT", "PATCH", "DELETE"].includes(k),
|
|
298
|
-
);
|
|
299
|
-
const wantsFile = mod.prerender === true && typeof mod.GET === "function";
|
|
300
|
-
if (wantsFile && !methods.length && !mod.websocket) {
|
|
301
|
-
const path = fillPattern(server.path, {});
|
|
302
|
-
if (!path || !HAS_EXTENSION.test(path)) {
|
|
303
|
-
problems.push(
|
|
304
|
-
`${server.clientPath} has prerender = true but "${server.path}" has no file extension or takes params, so there is no file name to write.`,
|
|
305
|
-
);
|
|
306
|
-
} else enqueue(path, server.clientPath);
|
|
307
|
-
} else {
|
|
308
|
-
problems.push(
|
|
309
|
-
`${server.clientPath} is a server endpoint${mod.websocket ? " with a websocket" : ""}; a static site cannot run it. ` +
|
|
310
|
-
"Export `prerender = true` from a GET-only file with an extension in its path (like sitemap.xml) to bake it into a file.",
|
|
311
|
-
);
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
// ---- render ---------------------------------------------------------
|
|
316
|
-
/** Queue a discovered link, unless it is no page or one already ruled out. */
|
|
317
|
-
const follow = (path: string, from: string) => {
|
|
318
|
-
const m = matchRoute(tree, new URL(origin + path), matchers);
|
|
319
|
-
if (m && !excluded.has(m.route.path)) enqueue(path, from);
|
|
320
|
-
};
|
|
321
|
-
const written: string[] = [];
|
|
322
|
-
const render = async (item: { path: string; from: string }) => {
|
|
323
|
-
const url = origin + withBase(item.path, base);
|
|
324
|
-
let res: Response;
|
|
325
|
-
try {
|
|
326
|
-
res = await app.fetch(new Request(url));
|
|
327
|
-
} catch (e) {
|
|
328
|
-
problems.push(
|
|
329
|
-
`${item.path} (from ${item.from}) threw: ${(e as Error).message}`,
|
|
330
|
-
);
|
|
331
|
-
return;
|
|
332
|
-
}
|
|
333
|
-
if (res.status >= 300 && res.status < 400) {
|
|
334
|
-
const location = res.headers.get("location") ?? "/";
|
|
335
|
-
files.set(htmlFileFor(item.path), redirectPage(location));
|
|
336
|
-
written.push(item.path);
|
|
337
|
-
// A redirect to another page of this site is a page to render too.
|
|
338
|
-
if (crawl && location.startsWith("/")) {
|
|
339
|
-
const route = stripBase(location.split(/[?#]/)[0]!, base);
|
|
340
|
-
if (route !== null) follow(route, item.path);
|
|
341
|
-
}
|
|
342
|
-
return;
|
|
343
|
-
}
|
|
344
|
-
if (res.status !== 200) {
|
|
345
|
-
problems.push(`${item.path} (from ${item.from}) returned ${res.status}.`);
|
|
346
|
-
return;
|
|
347
|
-
}
|
|
348
|
-
const type = res.headers.get("content-type") ?? "";
|
|
349
|
-
if (!type.includes("text/html")) {
|
|
350
|
-
// a prerendered endpoint: bytes, exactly as served
|
|
351
|
-
files.set(
|
|
352
|
-
item.path.replace(/^\/+/, ""),
|
|
353
|
-
new Uint8Array(await res.arrayBuffer()),
|
|
354
|
-
);
|
|
355
|
-
written.push(item.path);
|
|
356
|
-
return;
|
|
357
|
-
}
|
|
358
|
-
const html = await res.text();
|
|
359
|
-
files.set(htmlFileFor(item.path), markStatic(html));
|
|
360
|
-
written.push(item.path);
|
|
361
|
-
|
|
362
|
-
// The navigation payload the client router reads in place of a server.
|
|
363
|
-
const nav = await app.fetch(
|
|
364
|
-
new Request(url, { headers: { "x-grimoire-navigate": "1" } }),
|
|
365
|
-
);
|
|
366
|
-
if ((nav.headers.get("content-type") ?? "").includes("application/json")) {
|
|
367
|
-
files.set(dataFileFor(item.path), await nav.text());
|
|
368
|
-
}
|
|
369
|
-
if (crawl) {
|
|
370
|
-
for (const link of extractLinks(html, origin, base))
|
|
371
|
-
follow(link, item.path);
|
|
372
|
-
}
|
|
373
|
-
};
|
|
374
|
-
|
|
375
|
-
while (queue.length) {
|
|
376
|
-
const batch = queue.splice(0, concurrency);
|
|
377
|
-
await Promise.all(batch.map(render));
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
// ---- coverage -------------------------------------------------------
|
|
381
|
-
const reached = new Set<string>();
|
|
382
|
-
for (const path of written) {
|
|
383
|
-
const m = matchRoute(tree, new URL(origin + path), matchers);
|
|
384
|
-
if (m) reached.add(m.route.path);
|
|
385
|
-
}
|
|
386
|
-
for (const route of pageRoutes) {
|
|
387
|
-
if (reached.has(route.path) || excluded.has(route.path)) continue;
|
|
388
|
-
problems.push(
|
|
389
|
-
`${route.clientPath} was never rendered: "${route.path}" takes params and nothing produced one. ` +
|
|
390
|
-
"Export `entries()` from the page, or link to it from a page that is prerendered.",
|
|
391
|
-
);
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
// ---- not found ------------------------------------------------------
|
|
395
|
-
try {
|
|
396
|
-
const nf = await app.fetch(
|
|
397
|
-
new Request(`${origin}${withBase("/__grimoire_not_found__", base)}`),
|
|
398
|
-
);
|
|
399
|
-
if (
|
|
400
|
-
nf.status === 404 &&
|
|
401
|
-
(nf.headers.get("content-type") ?? "").includes("text/html")
|
|
402
|
-
) {
|
|
403
|
-
files.set("404.html", markStatic(await nf.text()));
|
|
404
|
-
}
|
|
405
|
-
} catch {
|
|
406
|
-
// no error page is fine; the host shows its own
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
if (problems.length && strict) {
|
|
410
|
-
throw new Error(
|
|
411
|
-
`Prerender found ${problems.length} problem${problems.length > 1 ? "s" : ""}:\n${problems
|
|
412
|
-
.map((p) => ` - ${p}`)
|
|
413
|
-
.join(
|
|
414
|
-
"\n",
|
|
415
|
-
)}\n\nFix them, or set { strict: false } on the adapter to skip what cannot be a file.`,
|
|
416
|
-
);
|
|
417
|
-
}
|
|
418
|
-
for (const p of problems) log.warn(p);
|
|
419
|
-
|
|
420
|
-
// ---- write ----------------------------------------------------------
|
|
421
|
-
await rm(outDir, { recursive: true, force: true });
|
|
422
|
-
await mkdir(outDir, { recursive: true });
|
|
423
|
-
await cp(join(root, "public"), outDir, { recursive: true }).catch(
|
|
424
|
-
(e: NodeJS.ErrnoException) => {
|
|
425
|
-
if (e.code !== "ENOENT") throw e;
|
|
426
|
-
},
|
|
427
|
-
);
|
|
428
|
-
// The browser bundle is built into .grimoire/client/, not public/, and
|
|
429
|
-
// every page loads it from CLIENT_URL; a static host has only what is here.
|
|
430
|
-
await cp(clientDir(root), join(outDir, CLIENT_URL.replaceAll("/", "")), {
|
|
431
|
-
recursive: true,
|
|
432
|
-
filter: (src) => !DEV_FILES.has(basename(src)),
|
|
433
|
-
}).catch((e: NodeJS.ErrnoException) => {
|
|
434
|
-
if (e.code !== "ENOENT") throw e;
|
|
435
|
-
});
|
|
436
|
-
// The pages link /style.<hash>.css; a static host has only the plain name.
|
|
437
|
-
// Both are written, so a page that still asks for the plain one works too.
|
|
438
|
-
for (const [plain, hashed] of Object.entries(publicCssMap(false, root))) {
|
|
439
|
-
const from = stripBase(plain, base);
|
|
440
|
-
const to = stripBase(hashed, base);
|
|
441
|
-
if (from && to)
|
|
442
|
-
await copyFile(join(root, "public", from), join(outDir, to));
|
|
443
|
-
}
|
|
444
|
-
for (const [rel, body] of files) {
|
|
445
|
-
const target = join(outDir, rel);
|
|
446
|
-
await mkdir(dirname(target), { recursive: true });
|
|
447
|
-
await writeFile(target, body);
|
|
448
|
-
}
|
|
449
|
-
return { pages: written.sort(), problems };
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
/** `out` is deleted before writing, so refuse anything that could take the project with it. */
|
|
453
|
-
function assertSafeOut(root: string, out: string): void {
|
|
454
|
-
const rel = relative(root, out);
|
|
455
|
-
if (rel === "") {
|
|
456
|
-
throw new Error(
|
|
457
|
-
`adapter-static: out "." is the project root. Use a directory like "build".`,
|
|
458
|
-
);
|
|
459
|
-
}
|
|
460
|
-
const inside = !rel.startsWith("..") && !isAbsolute(rel);
|
|
461
|
-
if (inside) {
|
|
462
|
-
const top = rel.split(sep)[0]!;
|
|
463
|
-
if (["src", "public", ".git", "node_modules", ".grimoire"].includes(top)) {
|
|
464
|
-
throw new Error(
|
|
465
|
-
`adapter-static: out "${rel}" is inside "${top}", which the build must not delete. Use a directory like "build".`,
|
|
466
|
-
);
|
|
467
|
-
}
|
|
468
|
-
} else if (!relative(out, root).startsWith("..")) {
|
|
469
|
-
throw new Error(
|
|
470
|
-
`adapter-static: out "${out}" contains the project. Use a directory inside it, like "build".`,
|
|
471
|
-
);
|
|
472
|
-
}
|
|
473
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Prerendering: turn routes into files by sending requests through the real
|
|
3
|
+
* pipeline.
|
|
4
|
+
*
|
|
5
|
+
* Going through `app.fetch` and not calling the renderer directly is the point.
|
|
6
|
+
* `hooks.server.ts`, plugins, layouts and load functions all run exactly as
|
|
7
|
+
* they will in production, so a prerendered page cannot differ from the live
|
|
8
|
+
* one because a code path was skipped.
|
|
9
|
+
*
|
|
10
|
+
* The pure helpers at the top (path/file mapping, link extraction) are exported
|
|
11
|
+
* for tests. `prerenderSite` is the orchestration.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { copyFile, cp, mkdir, rm, writeFile } from "node:fs/promises";
|
|
15
|
+
import {
|
|
16
|
+
basename,
|
|
17
|
+
dirname,
|
|
18
|
+
isAbsolute,
|
|
19
|
+
join,
|
|
20
|
+
relative,
|
|
21
|
+
resolve,
|
|
22
|
+
sep,
|
|
23
|
+
} from "node:path";
|
|
24
|
+
import type { AdapterContext } from "../adapters/types";
|
|
25
|
+
import { normalizeBase, stripBase, withBase } from "../base";
|
|
26
|
+
import { matchRoute } from "../routing/router";
|
|
27
|
+
import type { RouteFile, RouteTree } from "../routing/scanner";
|
|
28
|
+
import { CLIENT_URL, clientDir, DEV_FILES } from "./client-dir";
|
|
29
|
+
import { publicCssMap } from "./public-assets";
|
|
30
|
+
import { RESERVED_EXPORTS } from "./reserved";
|
|
31
|
+
|
|
32
|
+
// ---------------------------------------------------------------------------
|
|
33
|
+
// pure helpers
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
|
|
36
|
+
/** `/blog/:slug` + `{ slug: "a b" }` → `/blog/a%20b`. Null when a required param is missing. */
|
|
37
|
+
export function fillPattern(
|
|
38
|
+
pattern: string,
|
|
39
|
+
params: Record<string, string | undefined>,
|
|
40
|
+
): string | null {
|
|
41
|
+
const out: string[] = [];
|
|
42
|
+
for (const part of pattern.split("/").filter(Boolean)) {
|
|
43
|
+
if (part.startsWith("*")) {
|
|
44
|
+
const value = params[part.slice(1)];
|
|
45
|
+
if (value) {
|
|
46
|
+
out.push(
|
|
47
|
+
value
|
|
48
|
+
.split("/")
|
|
49
|
+
.map((s) => encodeURIComponent(s))
|
|
50
|
+
.join("/"),
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
} else if (part.startsWith(":")) {
|
|
54
|
+
const optional = part.endsWith("?");
|
|
55
|
+
const name = part.slice(1, optional ? -1 : undefined);
|
|
56
|
+
const value = params[name];
|
|
57
|
+
if (value === undefined || value === "") {
|
|
58
|
+
if (optional) continue;
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
out.push(encodeURIComponent(value));
|
|
62
|
+
} else {
|
|
63
|
+
out.push(part);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return `/${out.join("/")}`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const HAS_EXTENSION = /\.[A-Za-z0-9]+$/;
|
|
70
|
+
|
|
71
|
+
/** The file a page at `pathname` is written to: `/` → `index.html`, `/a/b` → `a/b/index.html`. */
|
|
72
|
+
export function htmlFileFor(pathname: string): string {
|
|
73
|
+
const clean = pathname.replace(/^\/+|\/+$/g, "");
|
|
74
|
+
if (!clean) return "index.html";
|
|
75
|
+
if (/\.html?$/.test(clean)) return clean;
|
|
76
|
+
return `${clean}/index.html`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Where the client router finds the navigation payload for `pathname` on a
|
|
81
|
+
* static host. The router builds the same string, so the two must agree.
|
|
82
|
+
*/
|
|
83
|
+
export function dataFileFor(pathname: string): string {
|
|
84
|
+
const clean = pathname.replace(/^\/+|\/+$/g, "");
|
|
85
|
+
return clean ? `${clean}/__data.json` : "__data.json";
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Same-origin page paths linked from `html`, normalised: no query, no hash, no
|
|
90
|
+
* trailing slash, nothing that points at a file with an extension (assets, feeds).
|
|
91
|
+
*/
|
|
92
|
+
export function extractLinks(
|
|
93
|
+
html: string,
|
|
94
|
+
origin: string,
|
|
95
|
+
base = "",
|
|
96
|
+
): string[] {
|
|
97
|
+
const found = new Set<string>();
|
|
98
|
+
for (const m of html.matchAll(/\shref\s*=\s*("([^"]*)"|'([^']*)')/gi)) {
|
|
99
|
+
const href = (m[2] ?? m[3] ?? "").trim();
|
|
100
|
+
let path: string | null = null;
|
|
101
|
+
if (href.startsWith("/") && !href.startsWith("//")) path = href;
|
|
102
|
+
else if (href.startsWith(`${origin}/`)) path = href.slice(origin.length);
|
|
103
|
+
if (path === null) continue;
|
|
104
|
+
path = path.split("#")[0]!.split("?")[0]!;
|
|
105
|
+
// a link outside the base is not one of this site's pages
|
|
106
|
+
if (base) {
|
|
107
|
+
const route = stripBase(path, base);
|
|
108
|
+
if (route === null) continue;
|
|
109
|
+
path = route;
|
|
110
|
+
}
|
|
111
|
+
if (path.length > 1) path = path.replace(/\/+$/, "");
|
|
112
|
+
if (!path) continue;
|
|
113
|
+
if (HAS_EXTENSION.test(path) && !/\.html?$/.test(path)) continue;
|
|
114
|
+
found.add(path);
|
|
115
|
+
}
|
|
116
|
+
return [...found];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Tell the client router this site has no server to ask for navigation JSON.
|
|
121
|
+
* It then reads `<page>/__data.json`, which prerendering wrote beside the page.
|
|
122
|
+
*/
|
|
123
|
+
export function markStatic(html: string): string {
|
|
124
|
+
const tag = "<script>globalThis.__grimoire_static__=1</script>";
|
|
125
|
+
if (/<\/head>/i.test(html)) return html.replace(/<\/head>/i, `${tag}</head>`);
|
|
126
|
+
return tag + html;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function redirectPage(location: string): string {
|
|
130
|
+
const safe = location.replace(/&/g, "&").replace(/"/g, """);
|
|
131
|
+
return `<!doctype html><meta charset="utf-8"><meta http-equiv="refresh" content="0;url=${safe}"><link rel="canonical" href="${safe}"><a href="${safe}">Redirecting</a>`;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// ---------------------------------------------------------------------------
|
|
135
|
+
// route options
|
|
136
|
+
// ---------------------------------------------------------------------------
|
|
137
|
+
|
|
138
|
+
interface RouteOptions {
|
|
139
|
+
/** `export const prerender`, from the page, its server file or the nearest layout. */
|
|
140
|
+
prerender: boolean | undefined;
|
|
141
|
+
entries: (() => unknown) | undefined;
|
|
142
|
+
/** Names of exports that are actions: they need a server, so they cannot be prerendered. */
|
|
143
|
+
actions: string[];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
type Importer = (path: string) => Promise<Record<string, unknown>>;
|
|
147
|
+
|
|
148
|
+
async function readOptions(
|
|
149
|
+
route: RouteFile,
|
|
150
|
+
tree: RouteTree,
|
|
151
|
+
load: Importer,
|
|
152
|
+
): Promise<RouteOptions> {
|
|
153
|
+
const layoutFor = (l: RouteFile) =>
|
|
154
|
+
l.path === "/" ||
|
|
155
|
+
route.path === l.path ||
|
|
156
|
+
route.path.startsWith(`${l.path}/`);
|
|
157
|
+
const pageServer = tree.routes.find(
|
|
158
|
+
(r) => r.type === "pageServer" && r.path === route.path,
|
|
159
|
+
);
|
|
160
|
+
// most specific first: page server, page, then layouts innermost to root
|
|
161
|
+
const sources = [
|
|
162
|
+
pageServer?.filePath,
|
|
163
|
+
route.filePath,
|
|
164
|
+
...tree.layouts
|
|
165
|
+
.filter(layoutFor)
|
|
166
|
+
.sort((a, b) => b.path.length - a.path.length)
|
|
167
|
+
.map((l) => l.filePath),
|
|
168
|
+
].filter((p): p is string => !!p);
|
|
169
|
+
|
|
170
|
+
let prerender: boolean | undefined;
|
|
171
|
+
let entries: RouteOptions["entries"];
|
|
172
|
+
const actions: string[] = [];
|
|
173
|
+
for (const file of sources) {
|
|
174
|
+
const mod = await load(file);
|
|
175
|
+
if (prerender === undefined && typeof mod.prerender === "boolean") {
|
|
176
|
+
prerender = mod.prerender;
|
|
177
|
+
}
|
|
178
|
+
if (!entries && typeof mod.entries === "function") {
|
|
179
|
+
entries = mod.entries as () => unknown;
|
|
180
|
+
}
|
|
181
|
+
if (file === pageServer?.filePath) {
|
|
182
|
+
for (const [name, value] of Object.entries(mod)) {
|
|
183
|
+
if (typeof value === "function" && !RESERVED_EXPORTS.has(name)) {
|
|
184
|
+
actions.push(name);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return { prerender, entries, actions };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// ---------------------------------------------------------------------------
|
|
193
|
+
// orchestration
|
|
194
|
+
// ---------------------------------------------------------------------------
|
|
195
|
+
|
|
196
|
+
export interface PrerenderOptions {
|
|
197
|
+
/** Directory the site is written to, relative to the project root. */
|
|
198
|
+
out: string;
|
|
199
|
+
/** What `url.origin` is while rendering. Default `http://localhost`. */
|
|
200
|
+
origin?: string;
|
|
201
|
+
/** Follow `<a href>` from rendered pages to find more pages. Default true. */
|
|
202
|
+
crawl?: boolean;
|
|
203
|
+
/** Extra paths to render. */
|
|
204
|
+
paths?: string[];
|
|
205
|
+
/** Fail the build when something cannot be a file. When false, warn and skip. Default true. */
|
|
206
|
+
strict?: boolean;
|
|
207
|
+
/** Parallel requests. Default 6. */
|
|
208
|
+
concurrency?: number;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export interface PrerenderReport {
|
|
212
|
+
pages: string[];
|
|
213
|
+
problems: string[];
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export async function prerenderSite(
|
|
217
|
+
ctx: AdapterContext,
|
|
218
|
+
opts: PrerenderOptions,
|
|
219
|
+
load: Importer = (p) => import(p),
|
|
220
|
+
): Promise<PrerenderReport> {
|
|
221
|
+
const { app, root, log } = ctx;
|
|
222
|
+
const tree = app.tree as RouteTree;
|
|
223
|
+
// `[id=int]` routes only match through their matcher; without it nothing
|
|
224
|
+
// that has one could be reached, linked to, or counted as rendered.
|
|
225
|
+
const matchers = app.matchers;
|
|
226
|
+
const origin = opts.origin ?? "http://localhost";
|
|
227
|
+
// requests carry the base; the files written do not, because the host
|
|
228
|
+
// serves the output directory *as* the base
|
|
229
|
+
const base = normalizeBase(app.config.base);
|
|
230
|
+
const strict = opts.strict ?? true;
|
|
231
|
+
const crawl = opts.crawl ?? true;
|
|
232
|
+
const concurrency = Math.max(1, opts.concurrency ?? 6);
|
|
233
|
+
const problems: string[] = [];
|
|
234
|
+
const outDir = resolve(root, opts.out);
|
|
235
|
+
assertSafeOut(root, outDir);
|
|
236
|
+
|
|
237
|
+
// ---- seeds ----------------------------------------------------------
|
|
238
|
+
const pageRoutes = tree.routes.filter(
|
|
239
|
+
(r) => r.type === "page" || r.type === "simple",
|
|
240
|
+
);
|
|
241
|
+
const queue: { path: string; from: string }[] = [];
|
|
242
|
+
const seen = new Set<string>();
|
|
243
|
+
const enqueue = (path: string, from: string) => {
|
|
244
|
+
if (seen.has(path)) return;
|
|
245
|
+
seen.add(path);
|
|
246
|
+
queue.push({ path, from });
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
const excluded = new Set<string>();
|
|
250
|
+
for (const route of pageRoutes) {
|
|
251
|
+
const o = await readOptions(route, tree, load);
|
|
252
|
+
if (o.prerender === false) {
|
|
253
|
+
excluded.add(route.path);
|
|
254
|
+
problems.push(
|
|
255
|
+
`${route.clientPath} sets prerender = false, so it cannot exist on a static host.`,
|
|
256
|
+
);
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
if (o.actions.length) {
|
|
260
|
+
excluded.add(route.path);
|
|
261
|
+
problems.push(
|
|
262
|
+
`${route.path} has server actions (${o.actions.join(", ")}) and a static site has no server to run them.`,
|
|
263
|
+
);
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
const dynamic =
|
|
267
|
+
route.paramNames.length > 0 ||
|
|
268
|
+
route.restParamNames.length > 0 ||
|
|
269
|
+
route.optionalParamNames.length > 0;
|
|
270
|
+
if (!dynamic) {
|
|
271
|
+
enqueue(route.path, route.clientPath);
|
|
272
|
+
} else if (o.entries) {
|
|
273
|
+
const list = await o.entries();
|
|
274
|
+
if (!Array.isArray(list)) {
|
|
275
|
+
problems.push(
|
|
276
|
+
`${route.clientPath}: entries() must return an array of param objects.`,
|
|
277
|
+
);
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
for (const params of list as Record<string, string>[]) {
|
|
281
|
+
const path = fillPattern(route.path, params ?? {});
|
|
282
|
+
if (path === null) {
|
|
283
|
+
problems.push(
|
|
284
|
+
`${route.clientPath}: entries() returned ${JSON.stringify(params)}, which is missing a param of "${route.path}".`,
|
|
285
|
+
);
|
|
286
|
+
} else enqueue(path, `${route.clientPath} entries()`);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
for (const path of opts.paths ?? []) enqueue(path, "config paths");
|
|
291
|
+
|
|
292
|
+
// +server.ts routes: only an explicit GET with `prerender = true` can be a file.
|
|
293
|
+
const files = new Map<string, string | Uint8Array>();
|
|
294
|
+
for (const server of tree.servers) {
|
|
295
|
+
const mod = await load(server.filePath);
|
|
296
|
+
const methods = Object.keys(mod).filter((k) =>
|
|
297
|
+
["POST", "PUT", "PATCH", "DELETE"].includes(k),
|
|
298
|
+
);
|
|
299
|
+
const wantsFile = mod.prerender === true && typeof mod.GET === "function";
|
|
300
|
+
if (wantsFile && !methods.length && !mod.websocket) {
|
|
301
|
+
const path = fillPattern(server.path, {});
|
|
302
|
+
if (!path || !HAS_EXTENSION.test(path)) {
|
|
303
|
+
problems.push(
|
|
304
|
+
`${server.clientPath} has prerender = true but "${server.path}" has no file extension or takes params, so there is no file name to write.`,
|
|
305
|
+
);
|
|
306
|
+
} else enqueue(path, server.clientPath);
|
|
307
|
+
} else {
|
|
308
|
+
problems.push(
|
|
309
|
+
`${server.clientPath} is a server endpoint${mod.websocket ? " with a websocket" : ""}; a static site cannot run it. ` +
|
|
310
|
+
"Export `prerender = true` from a GET-only file with an extension in its path (like sitemap.xml) to bake it into a file.",
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// ---- render ---------------------------------------------------------
|
|
316
|
+
/** Queue a discovered link, unless it is no page or one already ruled out. */
|
|
317
|
+
const follow = (path: string, from: string) => {
|
|
318
|
+
const m = matchRoute(tree, new URL(origin + path), matchers);
|
|
319
|
+
if (m && !excluded.has(m.route.path)) enqueue(path, from);
|
|
320
|
+
};
|
|
321
|
+
const written: string[] = [];
|
|
322
|
+
const render = async (item: { path: string; from: string }) => {
|
|
323
|
+
const url = origin + withBase(item.path, base);
|
|
324
|
+
let res: Response;
|
|
325
|
+
try {
|
|
326
|
+
res = await app.fetch(new Request(url));
|
|
327
|
+
} catch (e) {
|
|
328
|
+
problems.push(
|
|
329
|
+
`${item.path} (from ${item.from}) threw: ${(e as Error).message}`,
|
|
330
|
+
);
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
if (res.status >= 300 && res.status < 400) {
|
|
334
|
+
const location = res.headers.get("location") ?? "/";
|
|
335
|
+
files.set(htmlFileFor(item.path), redirectPage(location));
|
|
336
|
+
written.push(item.path);
|
|
337
|
+
// A redirect to another page of this site is a page to render too.
|
|
338
|
+
if (crawl && location.startsWith("/")) {
|
|
339
|
+
const route = stripBase(location.split(/[?#]/)[0]!, base);
|
|
340
|
+
if (route !== null) follow(route, item.path);
|
|
341
|
+
}
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
if (res.status !== 200) {
|
|
345
|
+
problems.push(`${item.path} (from ${item.from}) returned ${res.status}.`);
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
const type = res.headers.get("content-type") ?? "";
|
|
349
|
+
if (!type.includes("text/html")) {
|
|
350
|
+
// a prerendered endpoint: bytes, exactly as served
|
|
351
|
+
files.set(
|
|
352
|
+
item.path.replace(/^\/+/, ""),
|
|
353
|
+
new Uint8Array(await res.arrayBuffer()),
|
|
354
|
+
);
|
|
355
|
+
written.push(item.path);
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
const html = await res.text();
|
|
359
|
+
files.set(htmlFileFor(item.path), markStatic(html));
|
|
360
|
+
written.push(item.path);
|
|
361
|
+
|
|
362
|
+
// The navigation payload the client router reads in place of a server.
|
|
363
|
+
const nav = await app.fetch(
|
|
364
|
+
new Request(url, { headers: { "x-grimoire-navigate": "1" } }),
|
|
365
|
+
);
|
|
366
|
+
if ((nav.headers.get("content-type") ?? "").includes("application/json")) {
|
|
367
|
+
files.set(dataFileFor(item.path), await nav.text());
|
|
368
|
+
}
|
|
369
|
+
if (crawl) {
|
|
370
|
+
for (const link of extractLinks(html, origin, base))
|
|
371
|
+
follow(link, item.path);
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
while (queue.length) {
|
|
376
|
+
const batch = queue.splice(0, concurrency);
|
|
377
|
+
await Promise.all(batch.map(render));
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// ---- coverage -------------------------------------------------------
|
|
381
|
+
const reached = new Set<string>();
|
|
382
|
+
for (const path of written) {
|
|
383
|
+
const m = matchRoute(tree, new URL(origin + path), matchers);
|
|
384
|
+
if (m) reached.add(m.route.path);
|
|
385
|
+
}
|
|
386
|
+
for (const route of pageRoutes) {
|
|
387
|
+
if (reached.has(route.path) || excluded.has(route.path)) continue;
|
|
388
|
+
problems.push(
|
|
389
|
+
`${route.clientPath} was never rendered: "${route.path}" takes params and nothing produced one. ` +
|
|
390
|
+
"Export `entries()` from the page, or link to it from a page that is prerendered.",
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// ---- not found ------------------------------------------------------
|
|
395
|
+
try {
|
|
396
|
+
const nf = await app.fetch(
|
|
397
|
+
new Request(`${origin}${withBase("/__grimoire_not_found__", base)}`),
|
|
398
|
+
);
|
|
399
|
+
if (
|
|
400
|
+
nf.status === 404 &&
|
|
401
|
+
(nf.headers.get("content-type") ?? "").includes("text/html")
|
|
402
|
+
) {
|
|
403
|
+
files.set("404.html", markStatic(await nf.text()));
|
|
404
|
+
}
|
|
405
|
+
} catch {
|
|
406
|
+
// no error page is fine; the host shows its own
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
if (problems.length && strict) {
|
|
410
|
+
throw new Error(
|
|
411
|
+
`Prerender found ${problems.length} problem${problems.length > 1 ? "s" : ""}:\n${problems
|
|
412
|
+
.map((p) => ` - ${p}`)
|
|
413
|
+
.join(
|
|
414
|
+
"\n",
|
|
415
|
+
)}\n\nFix them, or set { strict: false } on the adapter to skip what cannot be a file.`,
|
|
416
|
+
);
|
|
417
|
+
}
|
|
418
|
+
for (const p of problems) log.warn(p);
|
|
419
|
+
|
|
420
|
+
// ---- write ----------------------------------------------------------
|
|
421
|
+
await rm(outDir, { recursive: true, force: true });
|
|
422
|
+
await mkdir(outDir, { recursive: true });
|
|
423
|
+
await cp(join(root, "public"), outDir, { recursive: true }).catch(
|
|
424
|
+
(e: NodeJS.ErrnoException) => {
|
|
425
|
+
if (e.code !== "ENOENT") throw e;
|
|
426
|
+
},
|
|
427
|
+
);
|
|
428
|
+
// The browser bundle is built into .grimoire/client/, not public/, and
|
|
429
|
+
// every page loads it from CLIENT_URL; a static host has only what is here.
|
|
430
|
+
await cp(clientDir(root), join(outDir, CLIENT_URL.replaceAll("/", "")), {
|
|
431
|
+
recursive: true,
|
|
432
|
+
filter: (src) => !DEV_FILES.has(basename(src)),
|
|
433
|
+
}).catch((e: NodeJS.ErrnoException) => {
|
|
434
|
+
if (e.code !== "ENOENT") throw e;
|
|
435
|
+
});
|
|
436
|
+
// The pages link /style.<hash>.css; a static host has only the plain name.
|
|
437
|
+
// Both are written, so a page that still asks for the plain one works too.
|
|
438
|
+
for (const [plain, hashed] of Object.entries(publicCssMap(false, root))) {
|
|
439
|
+
const from = stripBase(plain, base);
|
|
440
|
+
const to = stripBase(hashed, base);
|
|
441
|
+
if (from && to)
|
|
442
|
+
await copyFile(join(root, "public", from), join(outDir, to));
|
|
443
|
+
}
|
|
444
|
+
for (const [rel, body] of files) {
|
|
445
|
+
const target = join(outDir, rel);
|
|
446
|
+
await mkdir(dirname(target), { recursive: true });
|
|
447
|
+
await writeFile(target, body);
|
|
448
|
+
}
|
|
449
|
+
return { pages: written.sort(), problems };
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/** `out` is deleted before writing, so refuse anything that could take the project with it. */
|
|
453
|
+
function assertSafeOut(root: string, out: string): void {
|
|
454
|
+
const rel = relative(root, out);
|
|
455
|
+
if (rel === "") {
|
|
456
|
+
throw new Error(
|
|
457
|
+
`adapter-static: out "." is the project root. Use a directory like "build".`,
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
const inside = !rel.startsWith("..") && !isAbsolute(rel);
|
|
461
|
+
if (inside) {
|
|
462
|
+
const top = rel.split(sep)[0]!;
|
|
463
|
+
if (["src", "public", ".git", "node_modules", ".grimoire"].includes(top)) {
|
|
464
|
+
throw new Error(
|
|
465
|
+
`adapter-static: out "${rel}" is inside "${top}", which the build must not delete. Use a directory like "build".`,
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
} else if (!relative(out, root).startsWith("..")) {
|
|
469
|
+
throw new Error(
|
|
470
|
+
`adapter-static: out "${out}" contains the project. Use a directory inside it, like "build".`,
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
}
|