@sigil-dev/grimoire 0.8.5 → 0.9.0
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/package.json +4 -6
- package/server.ts +5 -0
- package/src/client/deps.ts +13 -0
- package/src/client/head.ts +1 -0
- package/src/client/router.ts +308 -228
- package/src/dev/compile-module.ts +1 -1
- package/src/dev/hmr-client.ts +30 -3
- package/src/dev/hmr-server.ts +187 -187
- package/src/dev/watcher.ts +4 -2
- package/src/env/dynamic-private.ts +19 -0
- package/src/env/dynamic-public.ts +22 -0
- package/src/env/plugin.ts +6 -0
- package/src/env/typegen.ts +10 -0
- package/src/logger/index.ts +0 -2
- package/src/rendering/index.ts +135 -59
- package/src/server/build.ts +199 -219
- package/src/server/coordinator.ts +157 -106
- package/src/server/index.ts +11 -4
- package/src/server/interactive.ts +56 -0
- package/src/server/plugins.ts +2 -1
- package/src/types.ts +282 -270
- package/test/client-router.test.ts +38 -0
- package/test/env.test.ts +90 -0
package/src/rendering/index.ts
CHANGED
|
@@ -28,6 +28,16 @@ async function renderErrorPage(
|
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
function buildHtmlSkeleton(nonceAttr: string, dev?: boolean): string {
|
|
32
|
+
return `<!DOCTYPE html>
|
|
33
|
+
<html>
|
|
34
|
+
<head>
|
|
35
|
+
<meta charset="UTF-8" />
|
|
36
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
37
|
+
<script type="module" src="/__grimoire__/hydrate.js"${nonceAttr}></script>
|
|
38
|
+
${dev ? `<script src="/__grimoire__/hmr-client.js"${nonceAttr}></script>` : ""}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
31
41
|
export async function renderRoute(
|
|
32
42
|
matched: MatchedRoute,
|
|
33
43
|
req: Request,
|
|
@@ -37,6 +47,7 @@ export async function renderRoute(
|
|
|
37
47
|
plugins: GrimoirePlugin[] = [],
|
|
38
48
|
cspNonce?: string,
|
|
39
49
|
dev?: boolean,
|
|
50
|
+
patchedFetch?: typeof fetch,
|
|
40
51
|
): Promise<Response> {
|
|
41
52
|
return runWithContext(async () => {
|
|
42
53
|
const context: LoadContext = {
|
|
@@ -44,6 +55,7 @@ export async function renderRoute(
|
|
|
44
55
|
params: matched.params,
|
|
45
56
|
url: new URL(req.url),
|
|
46
57
|
locals,
|
|
58
|
+
fetch: patchedFetch ?? globalThis.fetch,
|
|
47
59
|
};
|
|
48
60
|
|
|
49
61
|
initHead(cspNonce);
|
|
@@ -57,6 +69,7 @@ export async function renderRoute(
|
|
|
57
69
|
};
|
|
58
70
|
await runHook(plugins, "onRouteLoad", route, context);
|
|
59
71
|
|
|
72
|
+
// ── layout loading ──────────────────────────────────────────────────────
|
|
60
73
|
const layoutPairs: { layout: RouteFile; mod: any; data: unknown }[] = [];
|
|
61
74
|
for (const layout of matched.layouts) {
|
|
62
75
|
const layoutMod = await loadModule(layout.filePath);
|
|
@@ -118,32 +131,33 @@ export async function renderRoute(
|
|
|
118
131
|
throw e;
|
|
119
132
|
}
|
|
120
133
|
}
|
|
134
|
+
|
|
121
135
|
const universalLayoutData = layoutMod.load
|
|
122
136
|
? await layoutMod.load(context)
|
|
123
137
|
: undefined;
|
|
124
138
|
|
|
125
139
|
const mergedLayoutData =
|
|
126
140
|
universalLayoutData !== undefined
|
|
127
|
-
? //@ts-expect-error
|
|
128
|
-
|
|
141
|
+
? //@ts-expect-error user-provided data is always unknown
|
|
142
|
+
{ ...universalLayoutData, ...data }
|
|
129
143
|
: data;
|
|
130
144
|
|
|
131
145
|
layoutPairs.push({ layout, mod: layoutMod, data: mergedLayoutData });
|
|
132
146
|
}
|
|
133
147
|
|
|
148
|
+
// ── page server load ────────────────────────────────────────────────────
|
|
134
149
|
let pageData: unknown;
|
|
135
150
|
if (matched.pageServer) {
|
|
136
151
|
try {
|
|
137
152
|
const mod = await loadModule(matched.pageServer.filePath);
|
|
138
153
|
pageData = await mod.load?.(context);
|
|
139
154
|
} catch (e) {
|
|
140
|
-
if (isRedirectResult(e))
|
|
155
|
+
if (isRedirectResult(e))
|
|
141
156
|
return new Response(null, {
|
|
142
157
|
status: e.status,
|
|
143
158
|
headers: { Location: e.location },
|
|
144
159
|
});
|
|
145
|
-
|
|
146
|
-
if (isErrorResult(e)) {
|
|
160
|
+
if (isErrorResult(e))
|
|
147
161
|
return (
|
|
148
162
|
(await renderErrorPage(
|
|
149
163
|
errorRoutes,
|
|
@@ -153,16 +167,18 @@ export async function renderRoute(
|
|
|
153
167
|
e,
|
|
154
168
|
)) ?? new Response(e.message, { status: e.status })
|
|
155
169
|
);
|
|
156
|
-
}
|
|
157
170
|
throw e;
|
|
158
171
|
}
|
|
159
172
|
}
|
|
160
173
|
|
|
174
|
+
// ── page module ─────────────────────────────────────────────────────────
|
|
161
175
|
const pageMod = await loadModule(matched.route.filePath);
|
|
176
|
+
|
|
162
177
|
if (pageMod.canMatch) {
|
|
163
178
|
try {
|
|
164
179
|
const result = await pageMod.canMatch(context);
|
|
165
|
-
if (result === false)
|
|
180
|
+
if (result === false)
|
|
181
|
+
return new Response("Not Found", { status: 404 });
|
|
166
182
|
if (isRedirectResult(result))
|
|
167
183
|
return new Response(null, {
|
|
168
184
|
status: result.status,
|
|
@@ -188,30 +204,119 @@ export async function renderRoute(
|
|
|
188
204
|
}
|
|
189
205
|
}
|
|
190
206
|
|
|
207
|
+
const isNavigate = req.headers.get("x-grimoire-navigate") === "1";
|
|
208
|
+
const hasLoading = typeof pageMod.loading === "function";
|
|
209
|
+
const nonceAttr = cspNonce ? ` nonce="${cspNonce}"` : "";
|
|
210
|
+
|
|
211
|
+
// helper: wrap page html in layout tree
|
|
212
|
+
function applyLayouts(pageHtml: string): string {
|
|
213
|
+
let bodyHtml = `<div id="grimoire-page">${pageHtml}</div>`;
|
|
214
|
+
for (const { mod: layoutMod, data } of [...layoutPairs].reverse()) {
|
|
215
|
+
bodyHtml = String(
|
|
216
|
+
layoutMod.default({
|
|
217
|
+
data,
|
|
218
|
+
children: new SafeHtml(bodyHtml),
|
|
219
|
+
params: matched.params,
|
|
220
|
+
}),
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
return `<div id="grimoire-root">${bodyHtml}</div>`;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// ── two-pass streaming (loading export present) ─────────────────────────
|
|
227
|
+
if (hasLoading && !isNavigate) {
|
|
228
|
+
const uid = Math.random().toString(36).slice(2);
|
|
229
|
+
const loadingHtml = String(pageMod.loading());
|
|
230
|
+
const loadingBody = applyLayouts(
|
|
231
|
+
`<div id="ab-${uid}">${loadingHtml}</div>`,
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
const stream = new ReadableStream({
|
|
235
|
+
async start(controller) {
|
|
236
|
+
// 1. skeleton + loading shell — browser paints immediately
|
|
237
|
+
controller.enqueue(
|
|
238
|
+
`${buildHtmlSkeleton(nonceAttr, dev)}
|
|
239
|
+
</head>
|
|
240
|
+
<body>
|
|
241
|
+
<div id="app">${loadingBody}`,
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
// 2. run universal load while browser renders shell
|
|
245
|
+
const universalData = pageMod.load
|
|
246
|
+
? await pageMod.load(context).catch((e: unknown) => {
|
|
247
|
+
if (isRedirectResult(e)) throw e;
|
|
248
|
+
if (isErrorResult(e)) throw e;
|
|
249
|
+
throw e;
|
|
250
|
+
})
|
|
251
|
+
: undefined;
|
|
252
|
+
|
|
253
|
+
const mergedData =
|
|
254
|
+
universalData !== undefined
|
|
255
|
+
? //@ts-expect-error user-provided data is always unknown
|
|
256
|
+
{ ...universalData, ...pageData }
|
|
257
|
+
: pageData;
|
|
258
|
+
|
|
259
|
+
// 3. render real page + collect head
|
|
260
|
+
const pageHtml = await Promise.resolve(
|
|
261
|
+
pageMod.default({ data: mergedData, params: matched.params }),
|
|
262
|
+
);
|
|
263
|
+
const headHtml = collectHead();
|
|
264
|
+
const stateJson = JSON.stringify({
|
|
265
|
+
params: matched.params,
|
|
266
|
+
data: mergedData,
|
|
267
|
+
layoutData: layoutPairs.map((l) => l.data),
|
|
268
|
+
pattern: matched.route.path,
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
// 4. inject late head tags
|
|
272
|
+
if (headHtml) {
|
|
273
|
+
controller.enqueue(
|
|
274
|
+
`<script${nonceAttr}>(function(){var p=new DOMParser().parseFromString('<head>${headHtml.replace(/\\/g, "\\\\").replace(/`/g, "\\`")}</head>','text/html');for(var e of Array.from(p.head.children))document.head.appendChild(e.cloneNode(true))})();</script>`,
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// 5. swap shell with real content
|
|
279
|
+
controller.enqueue(
|
|
280
|
+
`<template id="ab-${uid}-r">${String(pageHtml)}</template>
|
|
281
|
+
<script${nonceAttr}>(function(){var t=document.getElementById("ab-${uid}-r");var target=document.getElementById("ab-${uid}");if(t&&target)target.replaceWith(t.content)})();</script>
|
|
282
|
+
<script id="__grimoire_state__" type="application/json"${nonceAttr}>${stateJson}</script>
|
|
283
|
+
</div>
|
|
284
|
+
</body>
|
|
285
|
+
</html>`,
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
controller.close();
|
|
289
|
+
},
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
return new Response(stream, {
|
|
293
|
+
headers: { "Content-Type": "text/html" },
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// ── single-pass (no loading export) ────────────────────────────────────
|
|
191
298
|
const universalData = pageMod.load
|
|
192
299
|
? await pageMod.load(context).catch((e) => {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
300
|
+
if (isRedirectResult(e)) throw e;
|
|
301
|
+
if (isErrorResult(e)) throw e;
|
|
302
|
+
throw e;
|
|
303
|
+
})
|
|
197
304
|
: undefined;
|
|
198
305
|
|
|
199
306
|
const mergedData =
|
|
200
307
|
universalData !== undefined
|
|
201
|
-
? //@ts-expect-error
|
|
202
|
-
|
|
308
|
+
? //@ts-expect-error user-provided data is always unknown
|
|
309
|
+
{ ...universalData, ...pageData }
|
|
203
310
|
: pageData;
|
|
204
311
|
|
|
205
|
-
const pageHtml =
|
|
206
|
-
data: mergedData,
|
|
207
|
-
|
|
208
|
-
});
|
|
312
|
+
const pageHtml = await Promise.resolve(
|
|
313
|
+
pageMod.default({ data: mergedData, params: matched.params }),
|
|
314
|
+
);
|
|
209
315
|
|
|
210
|
-
// collect head AFTER page render so <Head> calls are captured
|
|
211
316
|
const headHtml = collectHead();
|
|
212
317
|
|
|
213
|
-
// navigation request
|
|
214
|
-
if (
|
|
318
|
+
// ── navigation request — client handles rendering ───────────────────────
|
|
319
|
+
if (isNavigate) {
|
|
215
320
|
return Response.json({
|
|
216
321
|
data: mergedData ?? {},
|
|
217
322
|
layoutData: layoutPairs.map((l) => l.data),
|
|
@@ -221,20 +326,7 @@ export async function renderRoute(
|
|
|
221
326
|
});
|
|
222
327
|
}
|
|
223
328
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
let bodyHtml: string = wrappedPage;
|
|
227
|
-
for (const { mod: layoutMod, data } of [...layoutPairs].reverse()) {
|
|
228
|
-
bodyHtml = String(
|
|
229
|
-
layoutMod.default({
|
|
230
|
-
data,
|
|
231
|
-
children: new SafeHtml(bodyHtml),
|
|
232
|
-
params: matched.params,
|
|
233
|
-
}),
|
|
234
|
-
);
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
bodyHtml = `<div id="grimoire-root">${bodyHtml}</div>`;
|
|
329
|
+
// ── single-pass SSR stream ──────────────────────────────────────────────
|
|
238
330
|
const csrfToken = randomBytes(32).toString("hex");
|
|
239
331
|
const stateJson = JSON.stringify({
|
|
240
332
|
params: matched.params,
|
|
@@ -242,43 +334,27 @@ export async function renderRoute(
|
|
|
242
334
|
layoutData: layoutPairs.map((l) => l.data),
|
|
243
335
|
pattern: matched.route.path,
|
|
244
336
|
});
|
|
337
|
+
|
|
338
|
+
let bodyHtml = applyLayouts(String(pageHtml));
|
|
339
|
+
|
|
245
340
|
const csrfInput = `<input type="hidden" name="_csrf" value="${csrfToken}">`;
|
|
246
341
|
bodyHtml = bodyHtml.replace(
|
|
247
342
|
/<form([^>]*action=[^>]*)>/gi,
|
|
248
343
|
`<form$1>${csrfInput}`,
|
|
249
344
|
);
|
|
250
|
-
|
|
251
|
-
// Send DOCTYPE + head skeleton immediately (browser starts resource fetch).
|
|
252
|
-
// Then stream body + full head content + state as they become available.
|
|
253
|
-
const nonceAttr = cspNonce ? ` nonce="${cspNonce}"` : "";
|
|
345
|
+
|
|
254
346
|
const stream = new ReadableStream({
|
|
255
347
|
start(controller) {
|
|
256
|
-
|
|
257
|
-
controller.enqueue(
|
|
258
|
-
`<!DOCTYPE html>
|
|
259
|
-
<html>
|
|
260
|
-
<head>
|
|
261
|
-
<meta charset="UTF-8" />
|
|
262
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
263
|
-
<script type="module" src="/__grimoire__/hydrate.js"${nonceAttr}></script>
|
|
264
|
-
${dev ? `<script src="/__grimoire__/hmr-client.js"${nonceAttr}></script>` : ""}`,
|
|
265
|
-
);
|
|
348
|
+
controller.enqueue(buildHtmlSkeleton(nonceAttr, dev));
|
|
266
349
|
|
|
267
|
-
// 2. Head content (captured from <Head> component calls during render)
|
|
268
350
|
if (headHtml) {
|
|
269
351
|
controller.enqueue(`\n${headHtml}`);
|
|
270
352
|
}
|
|
271
353
|
|
|
272
354
|
controller.enqueue(`\n</head>
|
|
273
355
|
<body>
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
// 3. Page body
|
|
277
|
-
controller.enqueue(bodyHtml);
|
|
278
|
-
|
|
279
|
-
// 4. State script + closing tags
|
|
280
|
-
controller.enqueue(`</div>
|
|
281
|
-
<script id="__grimoire_state__" type="application/json"${nonceAttr}>${stateJson}</script>
|
|
356
|
+
<div id="app">${bodyHtml}</div>
|
|
357
|
+
<script id="__grimoire_state__" type="application/json"${nonceAttr}>${stateJson}</script>
|
|
282
358
|
</body>
|
|
283
359
|
</html>`);
|
|
284
360
|
|
|
@@ -289,8 +365,8 @@ export async function renderRoute(
|
|
|
289
365
|
return new Response(stream, {
|
|
290
366
|
headers: {
|
|
291
367
|
"Content-Type": "text/html",
|
|
292
|
-
"Set-Cookie":
|
|
368
|
+
"Set-Cookie": `_csrf=${csrfToken}; SameSite=Strict; Path=/`,
|
|
293
369
|
},
|
|
294
370
|
});
|
|
295
371
|
});
|
|
296
|
-
}
|
|
372
|
+
}
|