@sigil-dev/grimoire 0.9.23 → 0.9.25
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 +3 -3
- package/src/client/router.ts +31 -1
- package/src/rendering/index.ts +40 -16
- package/src/routing/router.ts +15 -8
- package/src/routing/scanner.ts +14 -3
- package/src/server/index.ts +1171 -1149
- package/src/server/plugin-routes.ts +38 -0
- package/src/server/prerender.ts +15 -3
- package/src/server/public-assets.ts +133 -0
- package/src/server/resolver.ts +5 -1
- package/src/types.ts +7 -0
- package/test/env.test.ts +4 -1
- package/test/plugin-routes.test.ts +42 -0
- package/test/prerender.test.ts +29 -4
- package/test/public-assets.test.ts +108 -0
- package/test/resolver-windows.test.ts +23 -0
- package/test/route-group-layouts.test.ts +36 -0
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"module": "index.ts",
|
|
12
12
|
"type": "module",
|
|
13
13
|
"private": false,
|
|
14
|
-
"version": "0.9.
|
|
14
|
+
"version": "0.9.25",
|
|
15
15
|
"exports": {
|
|
16
16
|
".": "./index.ts",
|
|
17
17
|
"./server": "./server.ts",
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
"test": "bun test"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@sigil-dev/compiler": "0.9.
|
|
39
|
-
"@sigil-dev/runtime": "0.9.
|
|
38
|
+
"@sigil-dev/compiler": "0.9.25",
|
|
39
|
+
"@sigil-dev/runtime": "0.9.25",
|
|
40
40
|
"vite": "^8.0.16"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
package/src/client/router.ts
CHANGED
|
@@ -267,7 +267,9 @@ function prefetchKey(url: URL): string {
|
|
|
267
267
|
* `dataFileFor` in server/prerender.ts.
|
|
268
268
|
*/
|
|
269
269
|
function navigateTarget(path: string): string {
|
|
270
|
-
if (
|
|
270
|
+
if (
|
|
271
|
+
(globalThis as { __grimoire_static__?: unknown }).__grimoire_static__ !== 1
|
|
272
|
+
) {
|
|
271
273
|
return path;
|
|
272
274
|
}
|
|
273
275
|
const { pathname } = new URL(path, location.origin);
|
|
@@ -332,6 +334,33 @@ function takePrefetched(path: string): Promise<Response> | null {
|
|
|
332
334
|
return hit.res;
|
|
333
335
|
}
|
|
334
336
|
|
|
337
|
+
/**
|
|
338
|
+
* The server points `<link rel="stylesheet">` at `/style.<hash>.css`, but a
|
|
339
|
+
* link the router renders after a navigation is built from the component's own
|
|
340
|
+
* `/style.css`. Point it at the hashed URL too, so it hits the cached copy
|
|
341
|
+
* instead of revalidating the plain one.
|
|
342
|
+
*/
|
|
343
|
+
function setupStylesheetHashes(): void {
|
|
344
|
+
const map = (globalThis as { __grimoire_css__?: Record<string, string> })
|
|
345
|
+
.__grimoire_css__;
|
|
346
|
+
if (!map || typeof MutationObserver === "undefined") return;
|
|
347
|
+
const fix = (el: Element) => {
|
|
348
|
+
if (!(el instanceof HTMLLinkElement) || el.rel !== "stylesheet") return;
|
|
349
|
+
const hashed = map[el.getAttribute("href") ?? ""];
|
|
350
|
+
if (hashed) el.setAttribute("href", hashed);
|
|
351
|
+
};
|
|
352
|
+
new MutationObserver((records) => {
|
|
353
|
+
for (const r of records) {
|
|
354
|
+
for (const node of r.addedNodes) {
|
|
355
|
+
if (node.nodeType !== 1) continue;
|
|
356
|
+
fix(node as Element);
|
|
357
|
+
for (const l of (node as Element).querySelectorAll?.("link") ?? [])
|
|
358
|
+
fix(l);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}).observe(document.documentElement, { childList: true, subtree: true });
|
|
362
|
+
}
|
|
363
|
+
|
|
335
364
|
function setupPreload(): void {
|
|
336
365
|
// Hover is a desktop signal; a short delay ignores cursors passing over.
|
|
337
366
|
document.addEventListener("mouseover", (e) => {
|
|
@@ -799,6 +828,7 @@ export function initRouter(
|
|
|
799
828
|
navigate(location.pathname + location.search, { history: "none" });
|
|
800
829
|
});
|
|
801
830
|
setupPreload();
|
|
831
|
+
setupStylesheetHashes();
|
|
802
832
|
}
|
|
803
833
|
|
|
804
834
|
/**
|
package/src/rendering/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { randomBytes } from "node:crypto";
|
|
2
2
|
import { SafeHtml } from "@sigil-dev/runtime";
|
|
3
|
+
import { base, withBase } from "../base";
|
|
3
4
|
import { startCollecting, stopCollecting } from "../client/deps";
|
|
4
5
|
import { filterPublic } from "../env/index";
|
|
5
6
|
import { findClosestError, type MatchedRoute } from "../routing/router";
|
|
@@ -9,8 +10,8 @@ import { isRedirectResult } from "../sentinels/redirect.ts";
|
|
|
9
10
|
import { clientAddressOf } from "../server/client-address";
|
|
10
11
|
import { hydrateEntry } from "../server/client-entry";
|
|
11
12
|
import { runWithContext } from "../server/context";
|
|
12
|
-
import { base, withBase } from "../base";
|
|
13
13
|
import { runHook } from "../server/plugins";
|
|
14
|
+
import { hashPublicStylesheets, publicCssMap } from "../server/public-assets";
|
|
14
15
|
import type { GrimoirePlugin, LoadContext, Route } from "../types";
|
|
15
16
|
import { collectHead, initHead } from "./head";
|
|
16
17
|
import { optimizeImages } from "./images.ts";
|
|
@@ -112,6 +113,20 @@ function publicEnvScript(nonceAttr: string): string {
|
|
|
112
113
|
return `\n <script${nonceAttr}>window.__grimoire_env__=${safeStateJson(env)}</script>`;
|
|
113
114
|
}
|
|
114
115
|
|
|
116
|
+
/** `/style.css` → `/style.<hash>.css` for the stylesheets in public/. */
|
|
117
|
+
const hashCss = (html: string, dev?: boolean): string =>
|
|
118
|
+
hashPublicStylesheets(html, publicCssMap(!!dev));
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The same map for the browser, so a link the client router creates after
|
|
122
|
+
* navigation is pointed at the hashed URL the server already put in the page.
|
|
123
|
+
*/
|
|
124
|
+
function publicCssScript(nonceAttr: string, dev?: boolean): string {
|
|
125
|
+
const map = publicCssMap(!!dev);
|
|
126
|
+
if (Object.keys(map).length === 0) return "";
|
|
127
|
+
return `\n <script${nonceAttr}>window.__grimoire_css__=${safeStateJson(map)}</script>`;
|
|
128
|
+
}
|
|
129
|
+
|
|
115
130
|
function buildHtmlSkeleton(nonceAttr: string, dev?: boolean): string {
|
|
116
131
|
// a meta tag, not a script: the base has to reach the router under a CSP
|
|
117
132
|
// nonce too, and the bundle reads it so one build works under any base
|
|
@@ -122,7 +137,7 @@ function buildHtmlSkeleton(nonceAttr: string, dev?: boolean): string {
|
|
|
122
137
|
<html>
|
|
123
138
|
<head>
|
|
124
139
|
<meta charset="UTF-8" />
|
|
125
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />${publicEnvScript(nonceAttr)}
|
|
140
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />${publicEnvScript(nonceAttr)}${publicCssScript(nonceAttr, dev)}
|
|
126
141
|
<script type="module" src="/__grimoire__/${hydrateEntry()}"${nonceAttr}></script>
|
|
127
142
|
${dev ? `<script src="/__grimoire__/hmr-client.js"${nonceAttr}></script>` : ""}`;
|
|
128
143
|
}
|
|
@@ -258,7 +273,10 @@ export async function renderErrorPage(
|
|
|
258
273
|
const errorHtml = String(
|
|
259
274
|
mod.default({ status, message, error, route: pathname }),
|
|
260
275
|
);
|
|
261
|
-
const bodyHtml =
|
|
276
|
+
const bodyHtml = hashCss(
|
|
277
|
+
applyLayoutTree(errorHtml, layoutPairs, params),
|
|
278
|
+
dev,
|
|
279
|
+
);
|
|
262
280
|
const headHtml = collectHead();
|
|
263
281
|
const hoistedCss = hoistStylesheets(bodyHtml);
|
|
264
282
|
const styles = scopedStyles(bodyHtml, nonceAttr);
|
|
@@ -527,9 +545,12 @@ export async function renderRoute(
|
|
|
527
545
|
const uid = Math.random().toString(36).slice(2);
|
|
528
546
|
const csrfToken = csrfTokenFor(req);
|
|
529
547
|
const loadingHtml = String(pageMod.loading());
|
|
530
|
-
const loadingBody =
|
|
531
|
-
|
|
532
|
-
|
|
548
|
+
const loadingBody = hashCss(
|
|
549
|
+
injectCsrf(
|
|
550
|
+
applyLayouts(`<div id="ab-${uid}">${loadingHtml}</div>`),
|
|
551
|
+
csrfToken,
|
|
552
|
+
),
|
|
553
|
+
dev,
|
|
533
554
|
);
|
|
534
555
|
// layout stylesheets are in the loading shell — hoist before first flush
|
|
535
556
|
const hoistedCss = hoistStylesheets(loadingBody);
|
|
@@ -564,17 +585,20 @@ export async function renderRoute(
|
|
|
564
585
|
extractDeferred(mergedData, deferred, deferredCounter);
|
|
565
586
|
|
|
566
587
|
// 3. render real page + collect head
|
|
567
|
-
const pageHtml =
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
588
|
+
const pageHtml = hashCss(
|
|
589
|
+
injectCsrf(
|
|
590
|
+
String(
|
|
591
|
+
await Promise.resolve(
|
|
592
|
+
pageMod.default({
|
|
593
|
+
data: renderable(mergedData),
|
|
594
|
+
params: matched.params,
|
|
595
|
+
form: actionFailure?.data,
|
|
596
|
+
}),
|
|
597
|
+
),
|
|
575
598
|
),
|
|
599
|
+
csrfToken,
|
|
576
600
|
),
|
|
577
|
-
|
|
601
|
+
dev,
|
|
578
602
|
);
|
|
579
603
|
const headHtml = collectHead();
|
|
580
604
|
const stateJson = safeStateJson({
|
|
@@ -701,7 +725,7 @@ export async function renderRoute(
|
|
|
701
725
|
form: actionFailure?.data,
|
|
702
726
|
});
|
|
703
727
|
|
|
704
|
-
bodyHtml = optimizeImages(injectCsrf(bodyHtml, csrfToken));
|
|
728
|
+
bodyHtml = hashCss(optimizeImages(injectCsrf(bodyHtml, csrfToken)), dev);
|
|
705
729
|
|
|
706
730
|
const hoistedCss = hoistStylesheets(bodyHtml);
|
|
707
731
|
const styles = scopedStyles(bodyHtml, nonceAttr);
|
package/src/routing/router.ts
CHANGED
|
@@ -10,6 +10,9 @@ export interface MatchedRoute {
|
|
|
10
10
|
notFound?: boolean;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
/** The folder a route file sits in, as written on disk: /src/routes/(app)/plan. */
|
|
14
|
+
const folderOf = (f: RouteFile) => f.clientPath.slice(0, f.clientPath.lastIndexOf("/"));
|
|
15
|
+
|
|
13
16
|
export function matchRoute(
|
|
14
17
|
tree: RouteTree,
|
|
15
18
|
url: URL,
|
|
@@ -45,20 +48,24 @@ export function matchRoute(
|
|
|
45
48
|
}
|
|
46
49
|
}
|
|
47
50
|
if (!matched) continue;
|
|
48
|
-
//
|
|
49
|
-
//
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
// A layout wraps its own folder and the folders below it, so compare
|
|
52
|
+
// folders on disk, not URL patterns: (app)/+layout.tsx has the URL "/"
|
|
53
|
+
// once the group is stripped, yet must not wrap login/+page.tsx.
|
|
54
|
+
const routeDir = folderOf(route);
|
|
55
|
+
const layoutMatches = (l: RouteFile) => {
|
|
56
|
+
const dir = folderOf(l);
|
|
57
|
+
return routeDir === dir || routeDir.startsWith(`${dir}/`);
|
|
58
|
+
};
|
|
59
|
+
const outermostFirst = (a: RouteFile, b: RouteFile) =>
|
|
60
|
+
folderOf(a).length - folderOf(b).length;
|
|
54
61
|
|
|
55
62
|
const layouts = tree.layouts
|
|
56
63
|
.filter((l) => l.type === "layout" && layoutMatches(l))
|
|
57
|
-
.sort(
|
|
64
|
+
.sort(outermostFirst);
|
|
58
65
|
|
|
59
66
|
const layoutServers = tree.layouts
|
|
60
67
|
.filter((l) => l.type === "layoutServer" && layoutMatches(l))
|
|
61
|
-
.sort(
|
|
68
|
+
.sort(outermostFirst);
|
|
62
69
|
|
|
63
70
|
const pageServer = tree.routes.find(
|
|
64
71
|
(r) => r.type === "pageServer" && r.path === route.path,
|
package/src/routing/scanner.ts
CHANGED
|
@@ -173,13 +173,17 @@ export async function scanRoutes(
|
|
|
173
173
|
routes.filter((r) => r.type === "pageServer"),
|
|
174
174
|
"page server",
|
|
175
175
|
);
|
|
176
|
+
// Layouts belong to a folder, not a URL: (app)/+layout.tsx and the root
|
|
177
|
+
// +layout.tsx both sit at "/" once the group is stripped, and both are fine.
|
|
176
178
|
assertNoDuplicatePaths(
|
|
177
179
|
layouts.filter((l) => l.type === "layout"),
|
|
178
180
|
"layout",
|
|
181
|
+
folderOf,
|
|
179
182
|
);
|
|
180
183
|
assertNoDuplicatePaths(
|
|
181
184
|
layouts.filter((l) => l.type === "layoutServer"),
|
|
182
185
|
"layout server",
|
|
186
|
+
folderOf,
|
|
183
187
|
);
|
|
184
188
|
assertNoDuplicatePaths(servers, "API route");
|
|
185
189
|
assertNoDuplicatePaths(errors, "error page");
|
|
@@ -188,16 +192,23 @@ export async function scanRoutes(
|
|
|
188
192
|
return { routes, layouts, servers, errors, paramMatcherFiles };
|
|
189
193
|
}
|
|
190
194
|
|
|
191
|
-
|
|
195
|
+
const folderOf = (f: RouteFile) => f.clientPath.slice(0, f.clientPath.lastIndexOf("/"));
|
|
196
|
+
|
|
197
|
+
function assertNoDuplicatePaths(
|
|
198
|
+
files: RouteFile[],
|
|
199
|
+
label: string,
|
|
200
|
+
keyOf: (f: RouteFile) => string = (f) => f.path,
|
|
201
|
+
): void {
|
|
192
202
|
const seen = new Map<string, string>();
|
|
193
203
|
for (const file of files) {
|
|
194
|
-
const
|
|
204
|
+
const key = keyOf(file);
|
|
205
|
+
const existing = seen.get(key);
|
|
195
206
|
if (existing) {
|
|
196
207
|
throw new Error(
|
|
197
208
|
`Duplicate ${label} at "${file.path}": "${existing}" and "${file.clientPath}"`,
|
|
198
209
|
);
|
|
199
210
|
}
|
|
200
|
-
seen.set(
|
|
211
|
+
seen.set(key, file.clientPath);
|
|
201
212
|
}
|
|
202
213
|
}
|
|
203
214
|
|