@sigil-dev/grimoire 0.9.26 → 0.9.28

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.
@@ -1,161 +1,162 @@
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
+ __component,
9
+ } from "@sigil-dev/runtime";
10
+ //@ts-expect-error compiler generated
11
+ import { layouts, matchers, routes } from "#grimoire-routes";
12
+ import {
13
+ afterNavigate,
14
+ beforeNavigate,
15
+ navigate,
16
+ onNavigate,
17
+ } from "../client/router";
18
+ import { initRouter } from "../client/router.ts";
19
+ import { withEffectScope } from "../client/scope.ts";
20
+ import { Head } from "./head";
21
+
22
+ // expose for HMR module shim
23
+ (globalThis as any).__grimoire_Head__ = Head;
24
+ (globalThis as any).__grimoire_navigate__ = navigate;
25
+ (globalThis as any).__grimoire_beforeNavigate__ = beforeNavigate;
26
+ (globalThis as any).__grimoire_onNavigate__ = onNavigate;
27
+ (globalThis as any).__grimoire_afterNavigate__ = afterNavigate;
28
+
29
+ async function resolveInitialPage(pattern: string): Promise<any> {
30
+ const entry = routes[pattern];
31
+ if (!entry) return null;
32
+ if (typeof entry === "function") return entry;
33
+ return (await entry.__lazy()).default;
34
+ }
35
+
36
+ async function hydrate(): Promise<(() => void) | undefined> {
37
+ const stateEl = document.getElementById("__grimoire_state__");
38
+ if (!stateEl) return undefined;
39
+
40
+ const state = JSON.parse(stateEl.textContent!);
41
+ const Page = await resolveInitialPage(state.pattern);
42
+ if (!Page) return undefined;
43
+ const slot = document.getElementById("grimoire-root");
44
+ if (!slot) return undefined;
45
+
46
+ const matchedLayouts = layouts
47
+ .filter(
48
+ (l: any) =>
49
+ l.path === "/" ||
50
+ state.pattern === l.path ||
51
+ state.pattern.startsWith(`${l.path}/`),
52
+ )
53
+ .sort((a: any, b: any) => a.path.length - b.path.length);
54
+
55
+ // The page's SSR container is findable by id, so the page can hydrate
56
+ // in place inside it while a single root layout claims from the
57
+ // top-level pool. With nested layouts (>1) the inner layout's DOM
58
+ // position can't be located in the flat pool — fall back to clearing
59
+ // the SSR content and re-rendering in DOM mode (empty pool).
60
+ const pageHost = document.getElementById("grimoire-page");
61
+ const hasLayouts = matchedLayouts.length > 0;
62
+ const inPlace = !hasLayouts || (matchedLayouts.length === 1 && !!pageHost);
63
+ if (!inPlace) {
64
+ slot.replaceChildren();
65
+ }
66
+
67
+ const ssrClones = inPlace
68
+ ? Array.from(slot.childNodes).map((n) => n.cloneNode(true))
69
+ : [];
70
+ pushHydrationNodes(
71
+ inPlace ? (Array.from(slot.childNodes) as ChildNode[]) : [],
72
+ );
73
+ let initialDispose: (() => void) | undefined;
74
+ try {
75
+ initialDispose = withEffectScope(() => {
76
+ try {
77
+ let renderFn = () => {
78
+ const pageDiv =
79
+ hasLayouts && inPlace && pageHost
80
+ ? pageHost
81
+ : claim(getHydrationNodes(), "div");
82
+ pageDiv.id = "grimoire-page";
83
+
84
+ if (pageDiv.childNodes.length > 0) {
85
+ pushHydrationNodes(Array.from(pageDiv.childNodes) as ChildNode[]);
86
+ let pageNode: unknown;
87
+ try {
88
+ pageNode = __component(Page, {
89
+ data: state.data,
90
+ params: state.params,
91
+ form: state.form,
92
+ });
93
+ } finally {
94
+ // always rebalance the global hydration stack — a throwing
95
+ // page component must not corrupt claims for the whole session
96
+ popHydrationNodes();
97
+ }
98
+ insert(pageDiv, pageNode);
99
+ } else {
100
+ const pageNode = __component(Page, {
101
+ data: state.data,
102
+ params: state.params,
103
+ form: state.form,
104
+ });
105
+ insert(pageDiv, pageNode);
106
+ }
107
+
108
+ return pageDiv;
109
+ };
110
+
111
+ for (let i = matchedLayouts.length - 1; i >= 0; i--) {
112
+ const LayoutComponent = matchedLayouts[i].component;
113
+ const innerRender = renderFn;
114
+ const layoutData = state.layoutData?.[i];
115
+ renderFn = () => {
116
+ // Pre-render inner content so children is a Node (insert() doesn't call functions)
117
+ const childNode = innerRender();
118
+ return __component(LayoutComponent, {
119
+ data: layoutData,
120
+ params: state.params,
121
+ path: state.pattern,
122
+ children: childNode,
123
+ });
124
+ };
125
+ }
126
+
127
+ // Defer effects during hydration render — they'll run after DOM insertion,
128
+ // so document.querySelector() etc. can find elements that are in the document.
129
+ (globalThis as any).__sigilDeferEffects = true;
130
+ try {
131
+ const rootNode = renderFn();
132
+ insert(slot, rootNode);
133
+ } finally {
134
+ // finally: a throwing render must not leave the flag set forever
135
+ (globalThis as any).__sigilDeferEffects = false;
136
+ flushDeferredEffects();
137
+ }
138
+ } finally {
139
+ popHydrationNodes();
140
+ }
141
+ });
142
+ } catch (e) {
143
+ console.warn("[grimoire] hydration error:", e);
144
+ }
145
+
146
+ if (!slot.hasChildNodes() && ssrClones.length > 0) {
147
+ slot.replaceChildren(...ssrClones);
148
+ }
149
+ return initialDispose;
150
+ }
151
+
152
+ hydrate().then((initialDispose) => {
153
+ const stateEl = document.getElementById("__grimoire_state__");
154
+ const state = stateEl ? JSON.parse(stateEl.textContent!) : null;
155
+ initRouter(routes, layouts, initialDispose, {
156
+ matchers,
157
+ deps:
158
+ state && Array.isArray(state.deps)
159
+ ? { [state.pattern]: state.deps }
160
+ : undefined,
161
+ });
162
+ });