@sigil-dev/grimoire 0.8.6 → 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 -1
- 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 +137 -63
- package/src/server/build.ts +199 -219
- package/src/server/coordinator.ts +316 -232
- package/src/server/index.ts +623 -622
- package/src/server/interactive.ts +56 -54
- package/src/server/plugins.ts +2 -1
- package/src/types.ts +282 -272
- package/test/client-router.test.ts +38 -0
- package/test/env.test.ts +90 -0
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"module": "index.ts",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"private": false,
|
|
6
|
-
"version": "0.
|
|
6
|
+
"version": "0.9.0",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": "./index.ts",
|
|
9
9
|
"./server": "./server.ts",
|
|
@@ -30,16 +30,14 @@
|
|
|
30
30
|
"@babel/plugin-syntax-typescript": "^8.0.0-rc.6",
|
|
31
31
|
"@babel/preset-typescript": "^8.0.0-rc.6",
|
|
32
32
|
"@babel/types": "^8.0.0-rc.6",
|
|
33
|
+
"@sigil-dev/compiler": "0.9.0",
|
|
34
|
+
"@sigil-dev/runtime": "0.9.0",
|
|
33
35
|
"vite": "^8.0.16"
|
|
34
36
|
},
|
|
35
37
|
"peerDependencies": {
|
|
36
|
-
"@sigil-dev/compiler": "0.8.6",
|
|
37
|
-
"@sigil-dev/runtime": "0.8.6",
|
|
38
38
|
"typescript": "^5"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@types/bun": "latest"
|
|
42
|
-
"@sigil-dev/compiler": "0.8.6",
|
|
43
|
-
"@sigil-dev/runtime": "0.8.6"
|
|
41
|
+
"@types/bun": "latest"
|
|
44
42
|
}
|
|
45
43
|
}
|
package/server.ts
CHANGED
|
@@ -4,6 +4,11 @@ export { createServer } from "./src/server";
|
|
|
4
4
|
export { buildProject } from "./src/server/build";
|
|
5
5
|
export { parseCookies } from "./src/server/cookie-utils";
|
|
6
6
|
export { startCoordinator } from "./src/server/coordinator.ts";
|
|
7
|
+
export {
|
|
8
|
+
createWaiter,
|
|
9
|
+
listenForKeys,
|
|
10
|
+
stopListening,
|
|
11
|
+
} from "./src/server/interactive.ts";
|
|
7
12
|
export {
|
|
8
13
|
runDeserializeLocals,
|
|
9
14
|
runRouteRequest,
|
|
@@ -12,4 +17,3 @@ export {
|
|
|
12
17
|
} from "./src/server/plugins";
|
|
13
18
|
export type { TypegenConfig } from "./src/typegen";
|
|
14
19
|
export { generateTypes } from "./src/typegen";
|
|
15
|
-
export {createWaiter, listenForKeys, stopListening} from "./src/server/interactive.ts"
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const routeDeps = new Map<string, Set<string>>();
|
|
2
|
+
let _collecting: Set<string> | null = null;
|
|
3
|
+
|
|
4
|
+
export function startCollecting(): void { _collecting = new Set(); }
|
|
5
|
+
export function stopCollecting(pattern: string): void {
|
|
6
|
+
if (_collecting) routeDeps.set(pattern, _collecting);
|
|
7
|
+
_collecting = null;
|
|
8
|
+
}
|
|
9
|
+
export function depends(key: string): void { _collecting?.add(key); }
|
|
10
|
+
export function hasDep(pattern: string, key: string): boolean {
|
|
11
|
+
return routeDeps.get(pattern)?.has(key) ?? false;
|
|
12
|
+
}
|
|
13
|
+
export function clearDeps(pattern: string): void { routeDeps.delete(pattern); }
|
package/src/client/head.ts
CHANGED
|
@@ -9,6 +9,7 @@ export function collectHead(): (string | Node)[] {
|
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
export function applyHead(): void {
|
|
12
|
+
//biome-ignore lint: ChildNode.remove() returns void??? sure typescript
|
|
12
13
|
document.querySelectorAll("[data-sigil-head]").forEach((el) => el.remove());
|
|
13
14
|
for (const el of headElements) {
|
|
14
15
|
if (el instanceof Node) {
|
package/src/client/router.ts
CHANGED
|
@@ -1,9 +1,43 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import {popHydrationNodes, pushHydrationNodes} from "@sigil-dev/runtime";
|
|
2
|
+
import {withEffectScope} from "./scope";
|
|
3
|
+
|
|
4
|
+
// ── Dependency tracking ──────────────────────────────────────────────────────
|
|
5
|
+
const routeDeps = new Map<string, Set<string>>(); // pattern → dep keys
|
|
6
|
+
let _collectingDeps: Set<string> | null = null;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Call from inside a load() function to register a dependency key.
|
|
10
|
+
* When invalidate(key) is called, any route that depends on that key
|
|
11
|
+
* will refetch if it's the current route.
|
|
12
|
+
*/
|
|
13
|
+
export function depends(key: string): void {
|
|
14
|
+
_collectingDeps?.add(key);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async function refetchCurrent(): Promise<void> {
|
|
18
|
+
if (!lastState) return;
|
|
19
|
+
await navigate(location.pathname, true); // true = replace, no pushState
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function invalidate(...keys: string[]): Promise<void> {
|
|
23
|
+
const current = lastState?.pattern;
|
|
24
|
+
if (!current) return;
|
|
25
|
+
const deps = routeDeps.get(current);
|
|
26
|
+
if (!deps) return;
|
|
27
|
+
if (keys.some(k => deps.has(k))) await refetchCurrent();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function invalidateAll(): Promise<void> {
|
|
31
|
+
await refetchCurrent();
|
|
32
|
+
}
|
|
3
33
|
|
|
4
34
|
let routeMap: Record<
|
|
5
|
-
|
|
6
|
-
|
|
35
|
+
string,
|
|
36
|
+
((props: any) => any) & {
|
|
37
|
+
load?: (ctx: any) => Promise<any>;
|
|
38
|
+
loading?: () => any;
|
|
39
|
+
error?: (props: { error: unknown }) => any;
|
|
40
|
+
}
|
|
7
41
|
> = {};
|
|
8
42
|
let layoutList: { path: string; component: (props: any) => any }[] = [];
|
|
9
43
|
let disposeCurrentPage: (() => void) | null = null;
|
|
@@ -20,272 +54,318 @@ let afterNavigateCb: ((url: URL) => void) | null = null;
|
|
|
20
54
|
// (globalThis as any).__grimoire_navigate__ = navigate;
|
|
21
55
|
|
|
22
56
|
export async function hmrRerender(): Promise<void> {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
57
|
+
if (!lastState) {
|
|
58
|
+
location.reload();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
disposeCurrentPage?.();
|
|
62
|
+
pushHydrationNodes([]);
|
|
63
|
+
let rootNode: any;
|
|
64
|
+
disposeCurrentPage = withEffectScope(() => {
|
|
65
|
+
rootNode = buildRouteTree(lastState!);
|
|
66
|
+
});
|
|
67
|
+
popHydrationNodes();
|
|
68
|
+
document.getElementById("grimoire-root")?.replaceChildren(rootNode);
|
|
35
69
|
}
|
|
36
70
|
|
|
37
71
|
// (globalThis as any).__grimoire_rerender__ = hmrRerender;
|
|
38
72
|
|
|
39
73
|
export function beforeNavigate(cb: (url: URL) => boolean | undefined): void {
|
|
40
|
-
|
|
74
|
+
beforeNavigateCb = cb;
|
|
41
75
|
}
|
|
42
76
|
|
|
43
77
|
export function onNavigate(cb: (url: URL) => void): void {
|
|
44
|
-
|
|
78
|
+
onNavigateCb = cb;
|
|
45
79
|
}
|
|
46
80
|
|
|
47
81
|
export function afterNavigate(cb: (url: URL) => void): void {
|
|
48
|
-
|
|
82
|
+
afterNavigateCb = cb;
|
|
49
83
|
}
|
|
50
84
|
|
|
51
85
|
// C2: Scroll restoration
|
|
52
86
|
const scrollPositions = new Map<string, number>();
|
|
53
87
|
|
|
54
88
|
function saveScrollPosition(): void {
|
|
55
|
-
|
|
89
|
+
scrollPositions.set(currentPath, window.scrollY);
|
|
56
90
|
}
|
|
57
91
|
|
|
58
92
|
function restoreScrollPosition(path: string): void {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
93
|
+
const saved = scrollPositions.get(path);
|
|
94
|
+
if (saved !== undefined) {
|
|
95
|
+
requestAnimationFrame(() => window.scrollTo(0, saved));
|
|
96
|
+
} else {
|
|
97
|
+
requestAnimationFrame(() => window.scrollTo(0, 0));
|
|
98
|
+
}
|
|
65
99
|
}
|
|
66
100
|
|
|
67
101
|
// C3: Preloading
|
|
68
102
|
let preloadTimer: Timer | null = null;
|
|
69
103
|
|
|
70
104
|
function setupPreload(): void {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
105
|
+
document.addEventListener("mouseover", (e) => {
|
|
106
|
+
const a = (e.target as Element).closest("a");
|
|
107
|
+
if (!a) return;
|
|
108
|
+
const href = a.getAttribute("href");
|
|
109
|
+
if (!href) return;
|
|
110
|
+
if (/^(https?:\/\/|\/\/|#|mailto:|tel:)/.test(href)) return;
|
|
111
|
+
|
|
112
|
+
const preloadData = a.hasAttribute("data-sigil-preload-data");
|
|
113
|
+
const preloadCode = a.hasAttribute("data-sigil-preload-code");
|
|
114
|
+
if (!preloadData && !preloadCode) return;
|
|
115
|
+
|
|
116
|
+
if (preloadTimer) clearTimeout(preloadTimer);
|
|
117
|
+
preloadTimer = setTimeout(() => {
|
|
118
|
+
if (preloadCode) {
|
|
119
|
+
const link = document.createElement("link");
|
|
120
|
+
link.rel = "prefetch";
|
|
121
|
+
link.href = href;
|
|
122
|
+
document.head.appendChild(link);
|
|
123
|
+
}
|
|
124
|
+
if (preloadData) {
|
|
125
|
+
fetch(href, {
|
|
126
|
+
headers: {"x-grimoire-navigate": "1"},
|
|
127
|
+
priority: "low",
|
|
128
|
+
}).catch(() => {
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}, 80);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
document.addEventListener("mouseout", (e) => {
|
|
135
|
+
const a = (e.target as Element).closest("a");
|
|
136
|
+
if (!a) return;
|
|
137
|
+
if (preloadTimer) clearTimeout(preloadTimer);
|
|
138
|
+
});
|
|
104
139
|
}
|
|
105
140
|
|
|
106
141
|
let lastState: {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
142
|
+
data: any;
|
|
143
|
+
layoutData: any[];
|
|
144
|
+
params: any;
|
|
145
|
+
pattern: string;
|
|
111
146
|
} | null = null;
|
|
112
147
|
|
|
113
148
|
function buildRouteTree(state: typeof lastState): any {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
149
|
+
const {data, layoutData, params, pattern} = state!;
|
|
150
|
+
const Page = routeMap[pattern];
|
|
151
|
+
if (!Page) throw new Error(`no component for pattern ${pattern}`);
|
|
152
|
+
|
|
153
|
+
const matchedLayouts = layoutList
|
|
154
|
+
.filter(
|
|
155
|
+
(l) =>
|
|
156
|
+
l.path === "/" ||
|
|
157
|
+
pattern === l.path ||
|
|
158
|
+
pattern.startsWith(`${l.path}/`),
|
|
159
|
+
)
|
|
160
|
+
.sort((a, b) => a.path.length - b.path.length);
|
|
161
|
+
|
|
162
|
+
let renderFn = () => {
|
|
163
|
+
const pageNode = Page({data, params});
|
|
164
|
+
const pageDiv = document.createElement("div");
|
|
165
|
+
pageDiv.id = "grimoire-page";
|
|
166
|
+
pageDiv.appendChild(pageNode);
|
|
167
|
+
return pageDiv;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
for (let i = matchedLayouts.length - 1; i >= 0; i--) {
|
|
171
|
+
const LayoutComponent = matchedLayouts[i].component;
|
|
172
|
+
const innerRender = renderFn;
|
|
173
|
+
const currentLayoutData = layoutData?.[i];
|
|
174
|
+
renderFn = () => {
|
|
175
|
+
const childNode = innerRender();
|
|
176
|
+
return LayoutComponent({
|
|
177
|
+
data: currentLayoutData,
|
|
178
|
+
params,
|
|
179
|
+
children: childNode,
|
|
180
|
+
});
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return renderFn();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function matchClientPattern(pattern: string, pathname: string): boolean {
|
|
188
|
+
// haha pp and ph
|
|
189
|
+
const pp = pattern.split("/").filter(Boolean);
|
|
190
|
+
const ph = pathname.split("/").filter(Boolean);
|
|
191
|
+
let pi = 0, pj = 0;
|
|
192
|
+
while (pi < pp.length) {
|
|
193
|
+
if (pp[pi]!.startsWith("*")) return true;
|
|
194
|
+
if (pj >= ph.length) return false;
|
|
195
|
+
if (!pp[pi]!.startsWith(":") && pp[pi] !== ph[pj]) return false;
|
|
196
|
+
pi++;
|
|
197
|
+
pj++;
|
|
198
|
+
}
|
|
199
|
+
return pj === ph.length;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function findPattern(pathname: string): string | null {
|
|
203
|
+
for (const pattern of Object.keys(routeMap)) {
|
|
204
|
+
if (matchClientPattern(pattern, pathname)) return pattern;
|
|
205
|
+
}
|
|
206
|
+
return null;
|
|
150
207
|
}
|
|
151
208
|
|
|
152
|
-
export async function navigate(path: string) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
209
|
+
export async function navigate(path: string, replace = false) {
|
|
210
|
+
const url = new URL(path, location.origin);
|
|
211
|
+
|
|
212
|
+
if (beforeNavigateCb) {
|
|
213
|
+
const shouldProceed = beforeNavigateCb(url);
|
|
214
|
+
if (shouldProceed === false) return;
|
|
215
|
+
}
|
|
216
|
+
onNavigateCb?.(url);
|
|
217
|
+
saveScrollPosition();
|
|
218
|
+
|
|
219
|
+
// load the loading immediately
|
|
220
|
+
const destPattern = findPattern(url.pathname);
|
|
221
|
+
const DestPage = destPattern ? routeMap[destPattern] : null;
|
|
222
|
+
if (DestPage?.loading) {
|
|
223
|
+
disposeCurrentPage?.();
|
|
224
|
+
disposeCurrentPage = null;
|
|
225
|
+
pushHydrationNodes([]);
|
|
226
|
+
let loadingNode: any;
|
|
227
|
+
disposeCurrentPage = withEffectScope(() => {
|
|
228
|
+
loadingNode = (DestPage as any).loading();
|
|
229
|
+
});
|
|
230
|
+
popHydrationNodes();
|
|
231
|
+
// replace only grimoire-page, keeps layout stable during transition
|
|
232
|
+
const pageEl = document.getElementById("grimoire-page");
|
|
233
|
+
if (pageEl) pageEl.replaceChildren(loadingNode);
|
|
234
|
+
else document.getElementById("grimoire-root")?.replaceChildren(loadingNode);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const res = await fetch(path, {headers: {"x-grimoire-navigate": "1"}});
|
|
238
|
+
const contentType = res.headers.get("content-type") ?? "";
|
|
239
|
+
if (!contentType.includes("application/json")) {
|
|
240
|
+
window.location.href = path;
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
const json = await res.json();
|
|
244
|
+
const {data, layoutData, params, pattern, head} = json;
|
|
245
|
+
const Page = routeMap[pattern];
|
|
246
|
+
if (!Page) {
|
|
247
|
+
window.location.href = path;
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
let mergedData = data;
|
|
252
|
+
if (Page.load) {
|
|
253
|
+
try {
|
|
254
|
+
_collectingDeps = new Set()
|
|
255
|
+
const clientData = await Page.load({
|
|
256
|
+
params,
|
|
257
|
+
url,
|
|
258
|
+
request: new Request(url),
|
|
259
|
+
});
|
|
260
|
+
routeDeps.set(pattern, _collectingDeps);
|
|
261
|
+
_collectingDeps = null;
|
|
262
|
+
mergedData = {...clientData, ...data};
|
|
263
|
+
} catch (e: any) {
|
|
264
|
+
_collectingDeps = null;
|
|
265
|
+
if (e?.location) {
|
|
266
|
+
window.location.href = e.location;
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
console.error("[grimoire] universal load error:", e);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// track state for HMR rerender
|
|
274
|
+
lastState = {data: mergedData, layoutData, params, pattern};
|
|
275
|
+
(globalThis as any).__grimoire_current_pattern__ = pattern;
|
|
276
|
+
|
|
277
|
+
disposeCurrentPage?.();
|
|
278
|
+
disposeCurrentPage = null;
|
|
279
|
+
pushHydrationNodes([]);
|
|
280
|
+
let rootNode: any;
|
|
281
|
+
disposeCurrentPage = withEffectScope(() => {
|
|
282
|
+
try {
|
|
283
|
+
rootNode = buildRouteTree(lastState!);
|
|
284
|
+
} catch (e) {
|
|
285
|
+
console.error(e);
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
popHydrationNodes();
|
|
289
|
+
|
|
290
|
+
updateHead(head);
|
|
291
|
+
document.title = head?.title ?? document.title;
|
|
292
|
+
document.getElementById("grimoire-root")?.replaceChildren(rootNode);
|
|
293
|
+
currentPath = path;
|
|
294
|
+
|
|
295
|
+
history[replace ? "replaceState" : "pushState"](null, "", path);
|
|
296
|
+
restoreScrollPosition(path);
|
|
297
|
+
afterNavigateCb?.(url);
|
|
218
298
|
}
|
|
219
299
|
|
|
220
300
|
export function initRouter(
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
301
|
+
routes: Record<
|
|
302
|
+
string,
|
|
303
|
+
((props: any) => any) & { load?: (ctx: any) => Promise<any> }
|
|
304
|
+
>,
|
|
305
|
+
layouts: any[],
|
|
306
|
+
initialDispose?: () => void,
|
|
227
307
|
) {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
308
|
+
routeMap = routes;
|
|
309
|
+
layoutList = layouts;
|
|
310
|
+
disposeCurrentPage = initialDispose ?? null;
|
|
311
|
+
currentPath = location.pathname;
|
|
312
|
+
|
|
313
|
+
// set initial HMR state from SSR
|
|
314
|
+
const stateEl = document.getElementById("__grimoire_state__");
|
|
315
|
+
if (stateEl) {
|
|
316
|
+
const state = JSON.parse(stateEl.textContent!);
|
|
317
|
+
lastState = {
|
|
318
|
+
data: state.data,
|
|
319
|
+
layoutData: state.layoutData,
|
|
320
|
+
params: state.params,
|
|
321
|
+
pattern: state.pattern,
|
|
322
|
+
};
|
|
323
|
+
(globalThis as any).__grimoire_current_pattern__ = state.pattern;
|
|
324
|
+
(globalThis as any).__grimoire_routes__ = routeMap;
|
|
325
|
+
(globalThis as any).__grimoire_rerender__ = hmrRerender;
|
|
326
|
+
(globalThis as any).__grimoire_navigate__ = (path: string) =>
|
|
327
|
+
navigate(path);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
saveScrollPosition();
|
|
331
|
+
document.addEventListener("click", handleClick);
|
|
332
|
+
window.addEventListener("popstate", () => {
|
|
333
|
+
if (location.pathname !== currentPath) {
|
|
334
|
+
saveScrollPosition();
|
|
335
|
+
currentPath = location.pathname;
|
|
336
|
+
navigate(location.pathname);
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
setupPreload();
|
|
260
340
|
}
|
|
261
341
|
|
|
262
342
|
function updateHead(headHtml: string) {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
343
|
+
document
|
|
344
|
+
.querySelectorAll("[data-grimoire-head]")
|
|
345
|
+
//biome-ignore lint/suspicious/useIterableCallbackReturn: shut up
|
|
346
|
+
.forEach((el) => el.remove());
|
|
347
|
+
const parsed = headHtml
|
|
348
|
+
? new DOMParser().parseFromString(`<head>${headHtml}</head>`, "text/html")
|
|
349
|
+
: null;
|
|
350
|
+
const incomingTitle = parsed?.querySelector("title");
|
|
351
|
+
//biome-ignore lint/suspicious/useIterableCallbackReturn: shut up
|
|
352
|
+
document.querySelectorAll("title").forEach((el) => el.remove());
|
|
353
|
+
document.title = incomingTitle?.textContent ?? document.title;
|
|
354
|
+
if (parsed) {
|
|
355
|
+
for (const el of Array.from(parsed.head.children)) {
|
|
356
|
+
if (el.tagName === "TITLE") continue;
|
|
357
|
+
(el as HTMLElement).dataset.grimoireHead = "1";
|
|
358
|
+
document.head.appendChild(el);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
281
361
|
}
|
|
282
362
|
|
|
283
363
|
function handleClick(e: MouseEvent) {
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
364
|
+
const a = (e.target as Element).closest("a");
|
|
365
|
+
if (!a) return;
|
|
366
|
+
const href = a.getAttribute("href");
|
|
367
|
+
if (!href) return;
|
|
368
|
+
if (/^(https?:\/\/|\/\/|#|mailto:|tel:)/.test(href)) return;
|
|
369
|
+
e.preventDefault();
|
|
370
|
+
navigate(href);
|
|
291
371
|
}
|