@sigil-dev/grimoire 0.9.6 → 0.9.10
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/.grimoire/_routes.ts +4 -0
- package/README.md +174 -174
- package/package.json +3 -3
- package/public/__grimoire__/client.js +55 -0
- package/public/__grimoire__/hydrate.js +63 -0
- package/src/client/csrf.ts +47 -0
- package/src/client/enhance.ts +5 -0
- package/src/client/router.ts +622 -619
- package/src/rendering/hydrate.ts +159 -159
- package/src/rendering/index.ts +599 -599
- package/src/routing/scanner.ts +1 -1
- package/src/server/coordinator.ts +376 -364
- package/src/server/index.ts +723 -718
- package/test/client-csrf.test.ts +61 -0
- package/test/client-router.test.ts +366 -366
- package/test/streaming.test.ts +281 -281
package/src/rendering/hydrate.ts
CHANGED
|
@@ -1,159 +1,159 @@
|
|
|
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
|
-
});
|
|
91
|
-
} finally {
|
|
92
|
-
// always rebalance the global hydration stack — a throwing
|
|
93
|
-
// page component must not corrupt claims for the whole session
|
|
94
|
-
popHydrationNodes();
|
|
95
|
-
}
|
|
96
|
-
insert(pageDiv, pageNode);
|
|
97
|
-
} else {
|
|
98
|
-
const pageNode = Page({
|
|
99
|
-
data: state.data,
|
|
100
|
-
params: state.params,
|
|
101
|
-
});
|
|
102
|
-
insert(pageDiv, pageNode);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return pageDiv;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
for (let i = matchedLayouts.length - 1; i >= 0; i--) {
|
|
109
|
-
const LayoutComponent = matchedLayouts[i].component;
|
|
110
|
-
const innerRender = renderFn;
|
|
111
|
-
const layoutData = state.layoutData?.[i];
|
|
112
|
-
renderFn = () => {
|
|
113
|
-
// Pre-render inner content so children is a Node (insert() doesn't call functions)
|
|
114
|
-
const childNode = innerRender();
|
|
115
|
-
return LayoutComponent({
|
|
116
|
-
data: layoutData,
|
|
117
|
-
params: state.params,
|
|
118
|
-
path: state.pattern,
|
|
119
|
-
children: childNode,
|
|
120
|
-
});
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// Defer effects during hydration render — they'll run after DOM insertion,
|
|
125
|
-
// so document.querySelector() etc. can find elements that are in the document.
|
|
126
|
-
(globalThis as any).__sigilDeferEffects = true;
|
|
127
|
-
try {
|
|
128
|
-
const rootNode = renderFn();
|
|
129
|
-
insert(slot, rootNode);
|
|
130
|
-
} finally {
|
|
131
|
-
// finally: a throwing render must not leave the flag set forever
|
|
132
|
-
(globalThis as any).__sigilDeferEffects = false;
|
|
133
|
-
flushDeferredEffects();
|
|
134
|
-
}
|
|
135
|
-
} finally {
|
|
136
|
-
popHydrationNodes();
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
} catch (e) {
|
|
140
|
-
console.warn("[grimoire] hydration error:", e);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
if (!slot.hasChildNodes() && ssrClones.length > 0) {
|
|
144
|
-
slot.replaceChildren(...ssrClones);
|
|
145
|
-
}
|
|
146
|
-
return initialDispose;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
hydrate().then((initialDispose) => {
|
|
150
|
-
const stateEl = document.getElementById("__grimoire_state__");
|
|
151
|
-
const state = stateEl ? JSON.parse(stateEl.textContent!) : null;
|
|
152
|
-
initRouter(routes, layouts, initialDispose, {
|
|
153
|
-
matchers,
|
|
154
|
-
deps:
|
|
155
|
-
state && Array.isArray(state.deps)
|
|
156
|
-
? { [state.pattern]: state.deps }
|
|
157
|
-
: undefined,
|
|
158
|
-
});
|
|
159
|
-
});
|
|
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
|
+
});
|
|
91
|
+
} finally {
|
|
92
|
+
// always rebalance the global hydration stack — a throwing
|
|
93
|
+
// page component must not corrupt claims for the whole session
|
|
94
|
+
popHydrationNodes();
|
|
95
|
+
}
|
|
96
|
+
insert(pageDiv, pageNode);
|
|
97
|
+
} else {
|
|
98
|
+
const pageNode = Page({
|
|
99
|
+
data: state.data,
|
|
100
|
+
params: state.params,
|
|
101
|
+
});
|
|
102
|
+
insert(pageDiv, pageNode);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return pageDiv;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
for (let i = matchedLayouts.length - 1; i >= 0; i--) {
|
|
109
|
+
const LayoutComponent = matchedLayouts[i].component;
|
|
110
|
+
const innerRender = renderFn;
|
|
111
|
+
const layoutData = state.layoutData?.[i];
|
|
112
|
+
renderFn = () => {
|
|
113
|
+
// Pre-render inner content so children is a Node (insert() doesn't call functions)
|
|
114
|
+
const childNode = innerRender();
|
|
115
|
+
return LayoutComponent({
|
|
116
|
+
data: layoutData,
|
|
117
|
+
params: state.params,
|
|
118
|
+
path: state.pattern,
|
|
119
|
+
children: childNode,
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Defer effects during hydration render — they'll run after DOM insertion,
|
|
125
|
+
// so document.querySelector() etc. can find elements that are in the document.
|
|
126
|
+
(globalThis as any).__sigilDeferEffects = true;
|
|
127
|
+
try {
|
|
128
|
+
const rootNode = renderFn();
|
|
129
|
+
insert(slot, rootNode);
|
|
130
|
+
} finally {
|
|
131
|
+
// finally: a throwing render must not leave the flag set forever
|
|
132
|
+
(globalThis as any).__sigilDeferEffects = false;
|
|
133
|
+
flushDeferredEffects();
|
|
134
|
+
}
|
|
135
|
+
} finally {
|
|
136
|
+
popHydrationNodes();
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
} catch (e) {
|
|
140
|
+
console.warn("[grimoire] hydration error:", e);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (!slot.hasChildNodes() && ssrClones.length > 0) {
|
|
144
|
+
slot.replaceChildren(...ssrClones);
|
|
145
|
+
}
|
|
146
|
+
return initialDispose;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
hydrate().then((initialDispose) => {
|
|
150
|
+
const stateEl = document.getElementById("__grimoire_state__");
|
|
151
|
+
const state = stateEl ? JSON.parse(stateEl.textContent!) : null;
|
|
152
|
+
initRouter(routes, layouts, initialDispose, {
|
|
153
|
+
matchers,
|
|
154
|
+
deps:
|
|
155
|
+
state && Array.isArray(state.deps)
|
|
156
|
+
? { [state.pattern]: state.deps }
|
|
157
|
+
: undefined,
|
|
158
|
+
});
|
|
159
|
+
});
|