@sigil-dev/grimoire 0.9.21 → 0.9.23
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/README.md +83 -83
- package/index.ts +50 -48
- package/package.json +22 -10
- package/public/__grimoire__/grimoire-client.js +12 -0
- package/public/__grimoire__/runtime.js +1241 -0
- package/server.ts +22 -20
- package/src/adapters/bun.ts +17 -0
- package/src/adapters/static.ts +41 -0
- package/src/adapters/types.ts +36 -0
- package/src/base.ts +91 -0
- package/src/client/csrf.ts +47 -47
- package/src/client/deferred.ts +179 -179
- package/src/client/enhance.ts +160 -160
- package/src/client/router.ts +871 -833
- package/src/dev/compile-module.ts +211 -211
- package/src/dev/loader.ts +141 -141
- package/src/dev/resolve.ts +70 -70
- package/src/dev/runtime-bundle.ts +51 -51
- package/src/env/modules.ts +62 -62
- package/src/env/required.ts +42 -0
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +105 -105
- package/src/logger/transports.ts +117 -117
- package/src/rendering/head.ts +49 -49
- package/src/rendering/hydrate.ts +161 -161
- package/src/rendering/images.ts +43 -43
- package/src/rendering/index.ts +755 -749
- package/src/rendering/preload.ts +102 -101
- package/src/rendering/ssrPlugin.ts +1 -1
- package/src/rendering/timing.ts +40 -40
- package/src/routing/router.ts +186 -186
- package/src/routing/scanner.ts +225 -196
- 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 +262 -262
- package/src/server/cached.ts +114 -114
- package/src/server/client-address.ts +82 -82
- package/src/server/client-dir.ts +57 -57
- package/src/server/client-entry.ts +44 -44
- package/src/server/cookie-utils.ts +80 -80
- package/src/server/coordinator.ts +476 -471
- package/src/server/hooks.ts +122 -122
- package/src/server/index.ts +211 -80
- package/src/server/plugins.ts +172 -172
- package/src/server/prerender.ts +444 -0
- package/src/server/reserved.ts +17 -0
- package/src/server/resolver.ts +98 -98
- package/src/server/rewrite.ts +28 -0
- package/src/server/warm.ts +91 -91
- package/src/server/with-headers.ts +27 -27
- package/src/server/worker.ts +74 -72
- package/src/testing/dom.ts +73 -0
- package/src/testing/index.ts +169 -0
- package/src/testing/server.ts +162 -0
- package/src/typegen/index.ts +450 -450
- package/src/types.ts +501 -432
- package/test/after-response.test.ts +132 -132
- package/test/asset-cache-policy.test.ts +96 -96
- package/test/base.test.ts +134 -0
- 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 +789 -734
- package/test/coordinator.test.ts +96 -94
- package/test/deferred.test.ts +256 -256
- package/test/dev-loader.test.ts +30 -30
- package/test/env.test.ts +168 -168
- package/test/force-production.test.ts +44 -0
- package/test/images.test.ts +49 -49
- package/test/locals-header.test.ts +61 -0
- package/test/middleware.test.ts +243 -243
- package/test/parser-plugins.test.ts +1 -1
- package/test/plugin-handle.test.ts +62 -62
- package/test/plugin-services-context.test.ts +97 -97
- package/test/preload.test.ts +48 -48
- package/test/prerender.test.ts +353 -0
- package/test/rendering.test.ts +799 -798
- package/test/required-env.test.ts +32 -0
- package/test/rewrite-html.test.ts +90 -0
- package/test/scanning.test.ts +349 -314
- package/test/server.test.ts +640 -619
- package/test/services.test.ts +90 -90
- package/test/streaming.test.ts +381 -378
- package/test/testing-helpers.test.ts +166 -0
- package/test/typegen.test.ts +808 -799
- package/test/warm.test.ts +96 -96
- package/test/worker-context.test.ts +59 -59
- package/test/workspace-files.test.ts +149 -146
- package/test/workspace-versions.test.ts +1 -1
- package/.grimoire/_routes.ts +0 -4
- package/public/__grimoire__/client.js +0 -55
- package/public/__grimoire__/hydrate.js +0 -63
package/src/rendering/head.ts
CHANGED
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
import { createContext, getContext, setContext } from "@sigil-dev/runtime";
|
|
2
|
-
|
|
3
|
-
const HeadKey = createContext<string[]>();
|
|
4
|
-
const NonceKey = createContext<string>();
|
|
5
|
-
|
|
6
|
-
export function initHead(nonce?: string): void {
|
|
7
|
-
setContext(HeadKey, []);
|
|
8
|
-
if (nonce) setContext(NonceKey, nonce);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function setHeadNonce(nonce: string): void {
|
|
12
|
-
setContext(NonceKey, nonce);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function getHeadNonce(): string {
|
|
16
|
-
return getContext(NonceKey) ?? "";
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function collectHead(): string {
|
|
20
|
-
return (getContext(HeadKey) ?? []).join("\n ");
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/** Puts its children in the page's <head>. */
|
|
24
|
-
export function Head({ children }: { children?: unknown }): string | Comment {
|
|
25
|
-
if (typeof document === "undefined") {
|
|
26
|
-
const buf = getContext(HeadKey);
|
|
27
|
-
if (buf) {
|
|
28
|
-
// Dynamic text inside <Head> children arrives wrapped in hydration
|
|
29
|
-
// claim markers; those belong to the body runtime and must never
|
|
30
|
-
// reach document.title or meta content.
|
|
31
|
-
const nonce = getContext(NonceKey);
|
|
32
|
-
const clean = String(children).replace(/<!--\/?g[^>]*-->/g, "");
|
|
33
|
-
if (nonce) {
|
|
34
|
-
buf.push(
|
|
35
|
-
clean
|
|
36
|
-
.replace(/<script /g, `<script nonce="${nonce}" `)
|
|
37
|
-
.replace(/<style /g, `<style nonce="${nonce}" `),
|
|
38
|
-
);
|
|
39
|
-
} else {
|
|
40
|
-
buf.push(clean);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return "";
|
|
44
|
-
}
|
|
45
|
-
if (children instanceof Node) {
|
|
46
|
-
document.head.appendChild(children);
|
|
47
|
-
}
|
|
48
|
-
return document.createComment("head");
|
|
49
|
-
}
|
|
1
|
+
import { createContext, getContext, setContext } from "@sigil-dev/runtime";
|
|
2
|
+
|
|
3
|
+
const HeadKey = createContext<string[]>();
|
|
4
|
+
const NonceKey = createContext<string>();
|
|
5
|
+
|
|
6
|
+
export function initHead(nonce?: string): void {
|
|
7
|
+
setContext(HeadKey, []);
|
|
8
|
+
if (nonce) setContext(NonceKey, nonce);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function setHeadNonce(nonce: string): void {
|
|
12
|
+
setContext(NonceKey, nonce);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function getHeadNonce(): string {
|
|
16
|
+
return getContext(NonceKey) ?? "";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function collectHead(): string {
|
|
20
|
+
return (getContext(HeadKey) ?? []).join("\n ");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Puts its children in the page's <head>. */
|
|
24
|
+
export function Head({ children }: { children?: unknown }): string | Comment {
|
|
25
|
+
if (typeof document === "undefined") {
|
|
26
|
+
const buf = getContext(HeadKey);
|
|
27
|
+
if (buf) {
|
|
28
|
+
// Dynamic text inside <Head> children arrives wrapped in hydration
|
|
29
|
+
// claim markers; those belong to the body runtime and must never
|
|
30
|
+
// reach document.title or meta content.
|
|
31
|
+
const nonce = getContext(NonceKey);
|
|
32
|
+
const clean = String(children).replace(/<!--\/?g[^>]*-->/g, "");
|
|
33
|
+
if (nonce) {
|
|
34
|
+
buf.push(
|
|
35
|
+
clean
|
|
36
|
+
.replace(/<script /g, `<script nonce="${nonce}" `)
|
|
37
|
+
.replace(/<style /g, `<style nonce="${nonce}" `),
|
|
38
|
+
);
|
|
39
|
+
} else {
|
|
40
|
+
buf.push(clean);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return "";
|
|
44
|
+
}
|
|
45
|
+
if (children instanceof Node) {
|
|
46
|
+
document.head.appendChild(children);
|
|
47
|
+
}
|
|
48
|
+
return document.createComment("head");
|
|
49
|
+
}
|
package/src/rendering/hydrate.ts
CHANGED
|
@@ -1,161 +1,161 @@
|
|
|
1
|
-
import {
|
|
2
|
-
claim,
|
|
3
|
-
flushDeferredEffects,
|
|
4
|
-
getHydrationNodes,
|
|
5
|
-
insert,
|
|
6
|
-
popHydrationNodes,
|
|
7
|
-
pushHydrationNodes,
|
|
8
|
-
} from "@sigil-dev/runtime";
|
|
9
|
-
//@ts-expect-error compiler generated
|
|
10
|
-
import { layouts, matchers, routes } from "#grimoire-routes";
|
|
11
|
-
import {
|
|
12
|
-
afterNavigate,
|
|
13
|
-
beforeNavigate,
|
|
14
|
-
navigate,
|
|
15
|
-
onNavigate,
|
|
16
|
-
} from "../client/router";
|
|
17
|
-
import { initRouter } from "../client/router.ts";
|
|
18
|
-
import { withEffectScope } from "../client/scope.ts";
|
|
19
|
-
import { Head } from "./head";
|
|
20
|
-
|
|
21
|
-
// expose for HMR module shim
|
|
22
|
-
(globalThis as any).__grimoire_Head__ = Head;
|
|
23
|
-
(globalThis as any).__grimoire_navigate__ = navigate;
|
|
24
|
-
(globalThis as any).__grimoire_beforeNavigate__ = beforeNavigate;
|
|
25
|
-
(globalThis as any).__grimoire_onNavigate__ = onNavigate;
|
|
26
|
-
(globalThis as any).__grimoire_afterNavigate__ = afterNavigate;
|
|
27
|
-
|
|
28
|
-
async function resolveInitialPage(pattern: string): Promise<any> {
|
|
29
|
-
const entry = routes[pattern];
|
|
30
|
-
if (!entry) return null;
|
|
31
|
-
if (typeof entry === "function") return entry;
|
|
32
|
-
return (await entry.__lazy()).default;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async function hydrate(): Promise<(() => void) | undefined> {
|
|
36
|
-
const stateEl = document.getElementById("__grimoire_state__");
|
|
37
|
-
if (!stateEl) return undefined;
|
|
38
|
-
|
|
39
|
-
const state = JSON.parse(stateEl.textContent!);
|
|
40
|
-
const Page = await resolveInitialPage(state.pattern);
|
|
41
|
-
if (!Page) return undefined;
|
|
42
|
-
const slot = document.getElementById("grimoire-root");
|
|
43
|
-
if (!slot) return undefined;
|
|
44
|
-
|
|
45
|
-
const matchedLayouts = layouts
|
|
46
|
-
.filter(
|
|
47
|
-
(l: any) =>
|
|
48
|
-
l.path === "/" ||
|
|
49
|
-
state.pattern === l.path ||
|
|
50
|
-
state.pattern.startsWith(`${l.path}/`),
|
|
51
|
-
)
|
|
52
|
-
.sort((a: any, b: any) => a.path.length - b.path.length);
|
|
53
|
-
|
|
54
|
-
// The page's SSR container is findable by id, so the page can hydrate
|
|
55
|
-
// in place inside it while a single root layout claims from the
|
|
56
|
-
// top-level pool. With nested layouts (>1) the inner layout's DOM
|
|
57
|
-
// position can't be located in the flat pool — fall back to clearing
|
|
58
|
-
// the SSR content and re-rendering in DOM mode (empty pool).
|
|
59
|
-
const pageHost = document.getElementById("grimoire-page");
|
|
60
|
-
const hasLayouts = matchedLayouts.length > 0;
|
|
61
|
-
const inPlace = !hasLayouts || (matchedLayouts.length === 1 && !!pageHost);
|
|
62
|
-
if (!inPlace) {
|
|
63
|
-
slot.replaceChildren();
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const ssrClones = inPlace
|
|
67
|
-
? Array.from(slot.childNodes).map((n) => n.cloneNode(true))
|
|
68
|
-
: [];
|
|
69
|
-
pushHydrationNodes(
|
|
70
|
-
inPlace ? (Array.from(slot.childNodes) as ChildNode[]) : [],
|
|
71
|
-
);
|
|
72
|
-
let initialDispose: (() => void) | undefined;
|
|
73
|
-
try {
|
|
74
|
-
initialDispose = withEffectScope(() => {
|
|
75
|
-
try {
|
|
76
|
-
let renderFn = () => {
|
|
77
|
-
const pageDiv =
|
|
78
|
-
hasLayouts && inPlace && pageHost
|
|
79
|
-
? pageHost
|
|
80
|
-
: claim(getHydrationNodes(), "div");
|
|
81
|
-
pageDiv.id = "grimoire-page";
|
|
82
|
-
|
|
83
|
-
if (pageDiv.childNodes.length > 0) {
|
|
84
|
-
pushHydrationNodes(Array.from(pageDiv.childNodes) as ChildNode[]);
|
|
85
|
-
let pageNode: unknown;
|
|
86
|
-
try {
|
|
87
|
-
pageNode = Page({
|
|
88
|
-
data: state.data,
|
|
89
|
-
params: state.params,
|
|
90
|
-
form: state.form,
|
|
91
|
-
});
|
|
92
|
-
} finally {
|
|
93
|
-
// always rebalance the global hydration stack — a throwing
|
|
94
|
-
// page component must not corrupt claims for the whole session
|
|
95
|
-
popHydrationNodes();
|
|
96
|
-
}
|
|
97
|
-
insert(pageDiv, pageNode);
|
|
98
|
-
} else {
|
|
99
|
-
const pageNode = Page({
|
|
100
|
-
data: state.data,
|
|
101
|
-
params: state.params,
|
|
102
|
-
form: state.form,
|
|
103
|
-
});
|
|
104
|
-
insert(pageDiv, pageNode);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return pageDiv;
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
for (let i = matchedLayouts.length - 1; i >= 0; i--) {
|
|
111
|
-
const LayoutComponent = matchedLayouts[i].component;
|
|
112
|
-
const innerRender = renderFn;
|
|
113
|
-
const layoutData = state.layoutData?.[i];
|
|
114
|
-
renderFn = () => {
|
|
115
|
-
// Pre-render inner content so children is a Node (insert() doesn't call functions)
|
|
116
|
-
const childNode = innerRender();
|
|
117
|
-
return LayoutComponent({
|
|
118
|
-
data: layoutData,
|
|
119
|
-
params: state.params,
|
|
120
|
-
path: state.pattern,
|
|
121
|
-
children: childNode,
|
|
122
|
-
});
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// Defer effects during hydration render — they'll run after DOM insertion,
|
|
127
|
-
// so document.querySelector() etc. can find elements that are in the document.
|
|
128
|
-
(globalThis as any).__sigilDeferEffects = true;
|
|
129
|
-
try {
|
|
130
|
-
const rootNode = renderFn();
|
|
131
|
-
insert(slot, rootNode);
|
|
132
|
-
} finally {
|
|
133
|
-
// finally: a throwing render must not leave the flag set forever
|
|
134
|
-
(globalThis as any).__sigilDeferEffects = false;
|
|
135
|
-
flushDeferredEffects();
|
|
136
|
-
}
|
|
137
|
-
} finally {
|
|
138
|
-
popHydrationNodes();
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
} catch (e) {
|
|
142
|
-
console.warn("[grimoire] hydration error:", e);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (!slot.hasChildNodes() && ssrClones.length > 0) {
|
|
146
|
-
slot.replaceChildren(...ssrClones);
|
|
147
|
-
}
|
|
148
|
-
return initialDispose;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
hydrate().then((initialDispose) => {
|
|
152
|
-
const stateEl = document.getElementById("__grimoire_state__");
|
|
153
|
-
const state = stateEl ? JSON.parse(stateEl.textContent!) : null;
|
|
154
|
-
initRouter(routes, layouts, initialDispose, {
|
|
155
|
-
matchers,
|
|
156
|
-
deps:
|
|
157
|
-
state && Array.isArray(state.deps)
|
|
158
|
-
? { [state.pattern]: state.deps }
|
|
159
|
-
: undefined,
|
|
160
|
-
});
|
|
161
|
-
});
|
|
1
|
+
import {
|
|
2
|
+
claim,
|
|
3
|
+
flushDeferredEffects,
|
|
4
|
+
getHydrationNodes,
|
|
5
|
+
insert,
|
|
6
|
+
popHydrationNodes,
|
|
7
|
+
pushHydrationNodes,
|
|
8
|
+
} from "@sigil-dev/runtime";
|
|
9
|
+
//@ts-expect-error compiler generated
|
|
10
|
+
import { layouts, matchers, routes } from "#grimoire-routes";
|
|
11
|
+
import {
|
|
12
|
+
afterNavigate,
|
|
13
|
+
beforeNavigate,
|
|
14
|
+
navigate,
|
|
15
|
+
onNavigate,
|
|
16
|
+
} from "../client/router";
|
|
17
|
+
import { initRouter } from "../client/router.ts";
|
|
18
|
+
import { withEffectScope } from "../client/scope.ts";
|
|
19
|
+
import { Head } from "./head";
|
|
20
|
+
|
|
21
|
+
// expose for HMR module shim
|
|
22
|
+
(globalThis as any).__grimoire_Head__ = Head;
|
|
23
|
+
(globalThis as any).__grimoire_navigate__ = navigate;
|
|
24
|
+
(globalThis as any).__grimoire_beforeNavigate__ = beforeNavigate;
|
|
25
|
+
(globalThis as any).__grimoire_onNavigate__ = onNavigate;
|
|
26
|
+
(globalThis as any).__grimoire_afterNavigate__ = afterNavigate;
|
|
27
|
+
|
|
28
|
+
async function resolveInitialPage(pattern: string): Promise<any> {
|
|
29
|
+
const entry = routes[pattern];
|
|
30
|
+
if (!entry) return null;
|
|
31
|
+
if (typeof entry === "function") return entry;
|
|
32
|
+
return (await entry.__lazy()).default;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function hydrate(): Promise<(() => void) | undefined> {
|
|
36
|
+
const stateEl = document.getElementById("__grimoire_state__");
|
|
37
|
+
if (!stateEl) return undefined;
|
|
38
|
+
|
|
39
|
+
const state = JSON.parse(stateEl.textContent!);
|
|
40
|
+
const Page = await resolveInitialPage(state.pattern);
|
|
41
|
+
if (!Page) return undefined;
|
|
42
|
+
const slot = document.getElementById("grimoire-root");
|
|
43
|
+
if (!slot) return undefined;
|
|
44
|
+
|
|
45
|
+
const matchedLayouts = layouts
|
|
46
|
+
.filter(
|
|
47
|
+
(l: any) =>
|
|
48
|
+
l.path === "/" ||
|
|
49
|
+
state.pattern === l.path ||
|
|
50
|
+
state.pattern.startsWith(`${l.path}/`),
|
|
51
|
+
)
|
|
52
|
+
.sort((a: any, b: any) => a.path.length - b.path.length);
|
|
53
|
+
|
|
54
|
+
// The page's SSR container is findable by id, so the page can hydrate
|
|
55
|
+
// in place inside it while a single root layout claims from the
|
|
56
|
+
// top-level pool. With nested layouts (>1) the inner layout's DOM
|
|
57
|
+
// position can't be located in the flat pool — fall back to clearing
|
|
58
|
+
// the SSR content and re-rendering in DOM mode (empty pool).
|
|
59
|
+
const pageHost = document.getElementById("grimoire-page");
|
|
60
|
+
const hasLayouts = matchedLayouts.length > 0;
|
|
61
|
+
const inPlace = !hasLayouts || (matchedLayouts.length === 1 && !!pageHost);
|
|
62
|
+
if (!inPlace) {
|
|
63
|
+
slot.replaceChildren();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const ssrClones = inPlace
|
|
67
|
+
? Array.from(slot.childNodes).map((n) => n.cloneNode(true))
|
|
68
|
+
: [];
|
|
69
|
+
pushHydrationNodes(
|
|
70
|
+
inPlace ? (Array.from(slot.childNodes) as ChildNode[]) : [],
|
|
71
|
+
);
|
|
72
|
+
let initialDispose: (() => void) | undefined;
|
|
73
|
+
try {
|
|
74
|
+
initialDispose = withEffectScope(() => {
|
|
75
|
+
try {
|
|
76
|
+
let renderFn = () => {
|
|
77
|
+
const pageDiv =
|
|
78
|
+
hasLayouts && inPlace && pageHost
|
|
79
|
+
? pageHost
|
|
80
|
+
: claim(getHydrationNodes(), "div");
|
|
81
|
+
pageDiv.id = "grimoire-page";
|
|
82
|
+
|
|
83
|
+
if (pageDiv.childNodes.length > 0) {
|
|
84
|
+
pushHydrationNodes(Array.from(pageDiv.childNodes) as ChildNode[]);
|
|
85
|
+
let pageNode: unknown;
|
|
86
|
+
try {
|
|
87
|
+
pageNode = Page({
|
|
88
|
+
data: state.data,
|
|
89
|
+
params: state.params,
|
|
90
|
+
form: state.form,
|
|
91
|
+
});
|
|
92
|
+
} finally {
|
|
93
|
+
// always rebalance the global hydration stack — a throwing
|
|
94
|
+
// page component must not corrupt claims for the whole session
|
|
95
|
+
popHydrationNodes();
|
|
96
|
+
}
|
|
97
|
+
insert(pageDiv, pageNode);
|
|
98
|
+
} else {
|
|
99
|
+
const pageNode = Page({
|
|
100
|
+
data: state.data,
|
|
101
|
+
params: state.params,
|
|
102
|
+
form: state.form,
|
|
103
|
+
});
|
|
104
|
+
insert(pageDiv, pageNode);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return pageDiv;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
for (let i = matchedLayouts.length - 1; i >= 0; i--) {
|
|
111
|
+
const LayoutComponent = matchedLayouts[i].component;
|
|
112
|
+
const innerRender = renderFn;
|
|
113
|
+
const layoutData = state.layoutData?.[i];
|
|
114
|
+
renderFn = () => {
|
|
115
|
+
// Pre-render inner content so children is a Node (insert() doesn't call functions)
|
|
116
|
+
const childNode = innerRender();
|
|
117
|
+
return LayoutComponent({
|
|
118
|
+
data: layoutData,
|
|
119
|
+
params: state.params,
|
|
120
|
+
path: state.pattern,
|
|
121
|
+
children: childNode,
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Defer effects during hydration render — they'll run after DOM insertion,
|
|
127
|
+
// so document.querySelector() etc. can find elements that are in the document.
|
|
128
|
+
(globalThis as any).__sigilDeferEffects = true;
|
|
129
|
+
try {
|
|
130
|
+
const rootNode = renderFn();
|
|
131
|
+
insert(slot, rootNode);
|
|
132
|
+
} finally {
|
|
133
|
+
// finally: a throwing render must not leave the flag set forever
|
|
134
|
+
(globalThis as any).__sigilDeferEffects = false;
|
|
135
|
+
flushDeferredEffects();
|
|
136
|
+
}
|
|
137
|
+
} finally {
|
|
138
|
+
popHydrationNodes();
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
} catch (e) {
|
|
142
|
+
console.warn("[grimoire] hydration error:", e);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (!slot.hasChildNodes() && ssrClones.length > 0) {
|
|
146
|
+
slot.replaceChildren(...ssrClones);
|
|
147
|
+
}
|
|
148
|
+
return initialDispose;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
hydrate().then((initialDispose) => {
|
|
152
|
+
const stateEl = document.getElementById("__grimoire_state__");
|
|
153
|
+
const state = stateEl ? JSON.parse(stateEl.textContent!) : null;
|
|
154
|
+
initRouter(routes, layouts, initialDispose, {
|
|
155
|
+
matchers,
|
|
156
|
+
deps:
|
|
157
|
+
state && Array.isArray(state.deps)
|
|
158
|
+
? { [state.pattern]: state.deps }
|
|
159
|
+
: undefined,
|
|
160
|
+
});
|
|
161
|
+
});
|
package/src/rendering/images.ts
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Default image loading for server-rendered pages.
|
|
3
|
-
*
|
|
4
|
-
* The first few images are left alone: they are what the visitor sees first
|
|
5
|
-
* and lazy-loading them would delay the largest paint. Every later `<img>`
|
|
6
|
-
* gets `loading="lazy"` and `decoding="async"` so a long grid of thumbnails
|
|
7
|
-
* does not compete with the stylesheet and scripts for the first seconds of
|
|
8
|
-
* bandwidth. An author's own `loading` / `decoding` always wins.
|
|
9
|
-
*
|
|
10
|
-
* `data-sigil-priority` marks the image that should paint first (the hero,
|
|
11
|
-
* the likely largest paint): it stays eager and is fetched at high priority,
|
|
12
|
-
* and does not count toward the eager budget above.
|
|
13
|
-
*/
|
|
14
|
-
export const EAGER_IMAGE_COUNT = 4;
|
|
15
|
-
|
|
16
|
-
const IMG_TAG = /<img\b[^>]*>/gi;
|
|
17
|
-
// inline scripts and styles may contain "<img" inside strings
|
|
18
|
-
const RAW_BLOCK = /<(script|style|template)\b[\s\S]*?<\/\1>/gi;
|
|
19
|
-
|
|
20
|
-
export function optimizeImages(html: string): string {
|
|
21
|
-
if (!html.includes("<img")) return html;
|
|
22
|
-
let seen = 0;
|
|
23
|
-
const tag = (t: string) => {
|
|
24
|
-
if (/\sdata-sigil-priority\b/i.test(t)) {
|
|
25
|
-
return /\sfetchpriority\s*=/i.test(t)
|
|
26
|
-
? t
|
|
27
|
-
: t.replace(/(\s*\/?>)$/, ' fetchpriority="high"$1');
|
|
28
|
-
}
|
|
29
|
-
if (seen++ < EAGER_IMAGE_COUNT) return t;
|
|
30
|
-
let extra = "";
|
|
31
|
-
if (!/\sloading\s*=/i.test(t)) extra += ' loading="lazy"';
|
|
32
|
-
if (!/\sdecoding\s*=/i.test(t)) extra += ' decoding="async"';
|
|
33
|
-
// attributes go before the tag's own trailing space and `/>`
|
|
34
|
-
return extra ? t.replace(/(\s*\/?>)$/, `${extra}$1`) : t;
|
|
35
|
-
};
|
|
36
|
-
let out = "";
|
|
37
|
-
let last = 0;
|
|
38
|
-
for (const m of html.matchAll(RAW_BLOCK)) {
|
|
39
|
-
out += html.slice(last, m.index).replace(IMG_TAG, tag) + m[0];
|
|
40
|
-
last = m.index + m[0].length;
|
|
41
|
-
}
|
|
42
|
-
return out + html.slice(last).replace(IMG_TAG, tag);
|
|
43
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Default image loading for server-rendered pages.
|
|
3
|
+
*
|
|
4
|
+
* The first few images are left alone: they are what the visitor sees first
|
|
5
|
+
* and lazy-loading them would delay the largest paint. Every later `<img>`
|
|
6
|
+
* gets `loading="lazy"` and `decoding="async"` so a long grid of thumbnails
|
|
7
|
+
* does not compete with the stylesheet and scripts for the first seconds of
|
|
8
|
+
* bandwidth. An author's own `loading` / `decoding` always wins.
|
|
9
|
+
*
|
|
10
|
+
* `data-sigil-priority` marks the image that should paint first (the hero,
|
|
11
|
+
* the likely largest paint): it stays eager and is fetched at high priority,
|
|
12
|
+
* and does not count toward the eager budget above.
|
|
13
|
+
*/
|
|
14
|
+
export const EAGER_IMAGE_COUNT = 4;
|
|
15
|
+
|
|
16
|
+
const IMG_TAG = /<img\b[^>]*>/gi;
|
|
17
|
+
// inline scripts and styles may contain "<img" inside strings
|
|
18
|
+
const RAW_BLOCK = /<(script|style|template)\b[\s\S]*?<\/\1>/gi;
|
|
19
|
+
|
|
20
|
+
export function optimizeImages(html: string): string {
|
|
21
|
+
if (!html.includes("<img")) return html;
|
|
22
|
+
let seen = 0;
|
|
23
|
+
const tag = (t: string) => {
|
|
24
|
+
if (/\sdata-sigil-priority\b/i.test(t)) {
|
|
25
|
+
return /\sfetchpriority\s*=/i.test(t)
|
|
26
|
+
? t
|
|
27
|
+
: t.replace(/(\s*\/?>)$/, ' fetchpriority="high"$1');
|
|
28
|
+
}
|
|
29
|
+
if (seen++ < EAGER_IMAGE_COUNT) return t;
|
|
30
|
+
let extra = "";
|
|
31
|
+
if (!/\sloading\s*=/i.test(t)) extra += ' loading="lazy"';
|
|
32
|
+
if (!/\sdecoding\s*=/i.test(t)) extra += ' decoding="async"';
|
|
33
|
+
// attributes go before the tag's own trailing space and `/>`
|
|
34
|
+
return extra ? t.replace(/(\s*\/?>)$/, `${extra}$1`) : t;
|
|
35
|
+
};
|
|
36
|
+
let out = "";
|
|
37
|
+
let last = 0;
|
|
38
|
+
for (const m of html.matchAll(RAW_BLOCK)) {
|
|
39
|
+
out += html.slice(last, m.index).replace(IMG_TAG, tag) + m[0];
|
|
40
|
+
last = m.index + m[0].length;
|
|
41
|
+
}
|
|
42
|
+
return out + html.slice(last).replace(IMG_TAG, tag);
|
|
43
|
+
}
|