@sigil-dev/grimoire 0.9.6 → 0.9.7
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/client/router.ts
CHANGED
|
@@ -1,619 +1,622 @@
|
|
|
1
|
-
import { popHydrationNodes, pushHydrationNodes } from "@sigil-dev/runtime";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
let
|
|
57
|
-
|
|
58
|
-
//
|
|
59
|
-
//
|
|
60
|
-
|
|
61
|
-
let
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
let
|
|
66
|
-
let
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
//
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
ordinal
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
if (
|
|
186
|
-
if (
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
//
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
preloadTimer
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
link
|
|
204
|
-
link.
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
pattern
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
const
|
|
246
|
-
pageDiv
|
|
247
|
-
pageDiv.
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
const
|
|
254
|
-
const
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
const
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
//
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
if (
|
|
283
|
-
|
|
284
|
-
if (
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
//
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
const
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
const
|
|
334
|
-
|
|
335
|
-
if (
|
|
336
|
-
|
|
337
|
-
disposeCurrentPage
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
const
|
|
364
|
-
|
|
365
|
-
if (
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
//
|
|
380
|
-
parent
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
//
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
...
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
//
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
disposeCurrentPage
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
//
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
//
|
|
434
|
-
//
|
|
435
|
-
//
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
//
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
//
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
//
|
|
461
|
-
|
|
462
|
-
const
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
// the
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
//
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
globals.
|
|
527
|
-
globals.
|
|
528
|
-
globals.
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
//
|
|
560
|
-
//
|
|
561
|
-
//
|
|
562
|
-
//
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
const
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
//
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
if (
|
|
600
|
-
if (
|
|
601
|
-
|
|
602
|
-
if (a
|
|
603
|
-
|
|
604
|
-
if (
|
|
605
|
-
if (
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
}
|
|
1
|
+
import { popHydrationNodes, pushHydrationNodes } from "@sigil-dev/runtime";
|
|
2
|
+
import { onSubmitEnsureCsrf } from "./csrf.ts";
|
|
3
|
+
import {
|
|
4
|
+
clearDeps,
|
|
5
|
+
depends,
|
|
6
|
+
getDeps,
|
|
7
|
+
setDeps,
|
|
8
|
+
startCollecting,
|
|
9
|
+
stopCollecting,
|
|
10
|
+
} from "./deps";
|
|
11
|
+
import { withEffectScope } from "./scope";
|
|
12
|
+
|
|
13
|
+
// re-exported for app code that imports depends() from the router module
|
|
14
|
+
export { depends };
|
|
15
|
+
|
|
16
|
+
async function refetchCurrent(): Promise<void> {
|
|
17
|
+
if (!lastState) return;
|
|
18
|
+
await navigate(location.pathname, { history: "replace" });
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function invalidate(...keys: string[]): Promise<void> {
|
|
22
|
+
const current = lastState?.pattern;
|
|
23
|
+
if (!current) return;
|
|
24
|
+
const deps = getDeps(current);
|
|
25
|
+
// no recorded deps → conservative refetch; otherwise only on intersection
|
|
26
|
+
if (!deps || deps.size === 0 || keys.some((k) => deps.has(k))) {
|
|
27
|
+
await refetchCurrent();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function invalidateAll(): Promise<void> {
|
|
32
|
+
await refetchCurrent();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type PageComponent = ((props: any) => any) & {
|
|
36
|
+
load?: (ctx: any) => Promise<any>;
|
|
37
|
+
loading?: () => any;
|
|
38
|
+
error?: (props: { error: unknown }) => any;
|
|
39
|
+
};
|
|
40
|
+
/** Manifest entries are lazy (`{ __lazy: () => import(...) }`); plain
|
|
41
|
+
* component functions are also accepted (tests, HMR replacements). */
|
|
42
|
+
type RouteEntry =
|
|
43
|
+
| PageComponent
|
|
44
|
+
| { __lazy: () => Promise<{ default: PageComponent }> };
|
|
45
|
+
|
|
46
|
+
let routeMap: Record<string, RouteEntry> = {};
|
|
47
|
+
const resolvedRoutes: Record<string, PageComponent> = {};
|
|
48
|
+
|
|
49
|
+
async function resolveRoute(pattern: string): Promise<PageComponent | null> {
|
|
50
|
+
const entry = routeMap[pattern];
|
|
51
|
+
if (!entry) return null;
|
|
52
|
+
// plain function: eager component or an HMR hot-swap — always wins over cache
|
|
53
|
+
if (typeof entry === "function") return entry;
|
|
54
|
+
return (resolvedRoutes[pattern] ??= (await entry.__lazy()).default);
|
|
55
|
+
}
|
|
56
|
+
let layoutList: { path: string; component: (props: any) => any }[] = [];
|
|
57
|
+
let disposeCurrentPage: (() => void) | null = null;
|
|
58
|
+
// Scroll keys and same-page refetch detection are pathname-only on purpose.
|
|
59
|
+
// currentSearch is tracked alongside so popstate can tell "?page=2" apart
|
|
60
|
+
// from "?page=1" without polluting either of those.
|
|
61
|
+
let currentPath = location.pathname;
|
|
62
|
+
let currentSearch = location.search;
|
|
63
|
+
|
|
64
|
+
// C1: Navigation hooks
|
|
65
|
+
let beforeNavigateCb: ((url: URL) => boolean | undefined) | null = null;
|
|
66
|
+
let onNavigateCb: ((url: URL) => void) | null = null;
|
|
67
|
+
let afterNavigateCb: ((url: URL) => void) | null = null;
|
|
68
|
+
|
|
69
|
+
export async function hmrRerender(): Promise<void> {
|
|
70
|
+
if (!lastState) {
|
|
71
|
+
location.reload();
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const Page = await resolveRoute(lastState.pattern);
|
|
75
|
+
if (!Page) {
|
|
76
|
+
location.reload();
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
disposeCurrentPage?.();
|
|
80
|
+
pushHydrationNodes([]);
|
|
81
|
+
let rootNode: any;
|
|
82
|
+
disposeCurrentPage = withEffectScope(() => {
|
|
83
|
+
rootNode = buildRouteTree(lastState!, Page);
|
|
84
|
+
});
|
|
85
|
+
popHydrationNodes();
|
|
86
|
+
const preserved = capturePreservedScroll();
|
|
87
|
+
document.getElementById("grimoire-root")?.replaceChildren(rootNode);
|
|
88
|
+
restorePreservedScroll(preserved);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Dev-only surface the HMR client drives. Route entries are replaced rather
|
|
93
|
+
* than mutated in place so a swapped module also wins for later navigations,
|
|
94
|
+
* and the resolved-module cache is dropped with them.
|
|
95
|
+
*/
|
|
96
|
+
export interface HmrControl {
|
|
97
|
+
setRoute(pattern: string, entry: RouteEntry): void;
|
|
98
|
+
setLayout(path: string, component: (props: any) => any): void;
|
|
99
|
+
rerender(): Promise<void>;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const hmrControl: HmrControl = {
|
|
103
|
+
setRoute(pattern, entry) {
|
|
104
|
+
routeMap[pattern] = entry;
|
|
105
|
+
delete resolvedRoutes[pattern];
|
|
106
|
+
},
|
|
107
|
+
setLayout(path: string, component: (props: any) => any) {
|
|
108
|
+
const existing = layoutList.find((l) => l.path === path);
|
|
109
|
+
if (existing) existing.component = component;
|
|
110
|
+
else layoutList.push({ path, component });
|
|
111
|
+
},
|
|
112
|
+
rerender: hmrRerender,
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export function beforeNavigate(cb: (url: URL) => boolean | undefined): void {
|
|
116
|
+
beforeNavigateCb = cb;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function onNavigate(cb: (url: URL) => void): void {
|
|
120
|
+
onNavigateCb = cb;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function afterNavigate(cb: (url: URL) => void): void {
|
|
124
|
+
afterNavigateCb = cb;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// C2: Scroll restoration
|
|
128
|
+
const scrollPositions = new Map<string, number>();
|
|
129
|
+
|
|
130
|
+
function saveScrollPosition(): void {
|
|
131
|
+
scrollPositions.set(currentPath, window.scrollY);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function restoreScrollPosition(path: string): void {
|
|
135
|
+
const y = scrollPositions.get(path) ?? 0;
|
|
136
|
+
window.scrollTo(0, y);
|
|
137
|
+
// re-assert after layout settles (late effects/images can shift content)
|
|
138
|
+
requestAnimationFrame(() => window.scrollTo(0, y));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function scrollToTop(): void {
|
|
142
|
+
window.scrollTo(0, 0);
|
|
143
|
+
requestAnimationFrame(() => window.scrollTo(0, 0));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Elements marked `data-sigil-scroll-preserve` keep their internal scrollTop
|
|
147
|
+
// across the root swap: the router rebuilds the whole tree on navigation, so a
|
|
148
|
+
// long independently-scrollable sidebar would otherwise snap back to the top.
|
|
149
|
+
// The attribute value is the matching key; elements without one fall back to
|
|
150
|
+
// their id, then to their ordinal position.
|
|
151
|
+
const PRESERVE_ATTR = "data-sigil-scroll-preserve";
|
|
152
|
+
|
|
153
|
+
function preserveKey(el: Element, ordinal: number): string {
|
|
154
|
+
return el.getAttribute(PRESERVE_ATTR) || el.id || `#${ordinal}`;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function capturePreservedScroll(): Map<string, number> {
|
|
158
|
+
const positions = new Map<string, number>();
|
|
159
|
+
let ordinal = 0;
|
|
160
|
+
for (const el of document.querySelectorAll(`[${PRESERVE_ATTR}]`)) {
|
|
161
|
+
positions.set(preserveKey(el, ordinal), el.scrollTop);
|
|
162
|
+
ordinal++;
|
|
163
|
+
}
|
|
164
|
+
return positions;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function restorePreservedScroll(positions: Map<string, number>): void {
|
|
168
|
+
if (positions.size === 0) return;
|
|
169
|
+
let ordinal = 0;
|
|
170
|
+
for (const el of document.querySelectorAll(`[${PRESERVE_ATTR}]`)) {
|
|
171
|
+
const top = positions.get(preserveKey(el, ordinal));
|
|
172
|
+
if (top !== undefined) el.scrollTop = top;
|
|
173
|
+
ordinal++;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// C3: Preloading
|
|
178
|
+
let preloadTimer: Timer | null = null;
|
|
179
|
+
|
|
180
|
+
function setupPreload(): void {
|
|
181
|
+
document.addEventListener("mouseover", (e) => {
|
|
182
|
+
const a = (e.target as Element).closest("a");
|
|
183
|
+
if (!a) return;
|
|
184
|
+
const href = a.getAttribute("href");
|
|
185
|
+
if (!href) return;
|
|
186
|
+
if (/^(https?:\/\/|\/\/|#|mailto:|tel:)/.test(href)) return;
|
|
187
|
+
if (a.getAttribute("data-sigil-preload") === "off") return;
|
|
188
|
+
|
|
189
|
+
// Only prefetch links that resolve to a known client route — external
|
|
190
|
+
// pages and non-page endpoints (API routes, redirects) are skipped.
|
|
191
|
+
try {
|
|
192
|
+
const url = new URL(href, location.origin);
|
|
193
|
+
if (!findPattern(url.pathname)) return;
|
|
194
|
+
} catch {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const preloadCode = a.hasAttribute("data-sigil-preload-code");
|
|
199
|
+
|
|
200
|
+
clearTimeout(preloadTimer);
|
|
201
|
+
preloadTimer = setTimeout(() => {
|
|
202
|
+
if (preloadCode) {
|
|
203
|
+
const link = document.createElement("link");
|
|
204
|
+
link.rel = "prefetch";
|
|
205
|
+
link.href = href;
|
|
206
|
+
document.head.appendChild(link);
|
|
207
|
+
}
|
|
208
|
+
// data prefetch is the default; -code is the explicit escalation
|
|
209
|
+
fetch(href, {
|
|
210
|
+
headers: { "x-grimoire-navigate": "1" },
|
|
211
|
+
redirect: "manual",
|
|
212
|
+
priority: "low",
|
|
213
|
+
}).catch(() => {});
|
|
214
|
+
}, 80);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
document.addEventListener("mouseout", (e) => {
|
|
218
|
+
const a = (e.target as Element).closest("a");
|
|
219
|
+
if (!a) return;
|
|
220
|
+
clearTimeout(preloadTimer);
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
let lastState: {
|
|
225
|
+
data: any;
|
|
226
|
+
layoutData: any[];
|
|
227
|
+
params: any;
|
|
228
|
+
pattern: string;
|
|
229
|
+
} | null = null;
|
|
230
|
+
|
|
231
|
+
function buildRouteTree(state: typeof lastState, Page: PageComponent): any {
|
|
232
|
+
const { data, layoutData, params, pattern } = state!;
|
|
233
|
+
if (!Page) throw new Error(`no component for pattern ${pattern}`);
|
|
234
|
+
|
|
235
|
+
const matchedLayouts = layoutList
|
|
236
|
+
.filter(
|
|
237
|
+
(l) =>
|
|
238
|
+
l.path === "/" ||
|
|
239
|
+
pattern === l.path ||
|
|
240
|
+
pattern.startsWith(`${l.path}/`),
|
|
241
|
+
)
|
|
242
|
+
.sort((a, b) => a.path.length - b.path.length);
|
|
243
|
+
|
|
244
|
+
let renderFn = () => {
|
|
245
|
+
const pageNode = Page({ data, params });
|
|
246
|
+
const pageDiv = document.createElement("div");
|
|
247
|
+
pageDiv.id = "grimoire-page";
|
|
248
|
+
pageDiv.appendChild(pageNode);
|
|
249
|
+
return pageDiv;
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
for (let i = matchedLayouts.length - 1; i >= 0; i--) {
|
|
253
|
+
const LayoutComponent = matchedLayouts[i].component;
|
|
254
|
+
const innerRender = renderFn;
|
|
255
|
+
const currentLayoutData = layoutData?.[i];
|
|
256
|
+
renderFn = () => {
|
|
257
|
+
const childNode = innerRender();
|
|
258
|
+
return LayoutComponent({
|
|
259
|
+
data: currentLayoutData,
|
|
260
|
+
params,
|
|
261
|
+
path: pattern,
|
|
262
|
+
children: childNode,
|
|
263
|
+
});
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return renderFn();
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function matchClientPattern(pattern: string, pathname: string): boolean {
|
|
271
|
+
// haha pp and ph
|
|
272
|
+
const pp = pattern.split("/").filter(Boolean);
|
|
273
|
+
const ph = pathname.split("/").filter(Boolean);
|
|
274
|
+
|
|
275
|
+
// Depth-first walk; optional segments (:x?) try consuming one segment
|
|
276
|
+
// before consuming zero, mirroring the server's matchPattern.
|
|
277
|
+
function walk(pi: number, pj: number): boolean {
|
|
278
|
+
if (pi === pp.length) return pj === ph.length;
|
|
279
|
+
const part = pp[pi]!;
|
|
280
|
+
if (part.startsWith("*")) return true;
|
|
281
|
+
const optional = part.startsWith(":") && part.endsWith("?");
|
|
282
|
+
if (optional && pj < ph.length && walk(pi + 1, pj + 1)) return true;
|
|
283
|
+
if (!optional) {
|
|
284
|
+
if (pj >= ph.length) return false;
|
|
285
|
+
if (!part.startsWith(":") && part !== ph[pj]) return false;
|
|
286
|
+
return walk(pi + 1, pj + 1);
|
|
287
|
+
}
|
|
288
|
+
return walk(pi + 1, pj);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return walk(0, 0);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function findPattern(pathname: string): string | null {
|
|
295
|
+
for (const pattern of Object.keys(routeMap)) {
|
|
296
|
+
if (matchClientPattern(pattern, pathname)) return pattern;
|
|
297
|
+
}
|
|
298
|
+
return null;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export interface NavigateOptions {
|
|
302
|
+
/** "push" (default) adds a history entry, "replace" swaps the current one,
|
|
303
|
+
* "none" leaves history untouched (popstate — the browser already moved). */
|
|
304
|
+
history?: "push" | "replace" | "none";
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// monotonically increasing id — a navigation aborts silently the moment a
|
|
308
|
+
// newer one starts, so overlapping navigations can't commit stale responses
|
|
309
|
+
let navigationId = 0;
|
|
310
|
+
|
|
311
|
+
export async function navigate(
|
|
312
|
+
path: string,
|
|
313
|
+
opts: NavigateOptions | boolean = {},
|
|
314
|
+
) {
|
|
315
|
+
// boolean back-compat: navigate(path, true) meant "replace"
|
|
316
|
+
const historyMode =
|
|
317
|
+
typeof opts === "boolean"
|
|
318
|
+
? opts
|
|
319
|
+
? "replace"
|
|
320
|
+
: "push"
|
|
321
|
+
: (opts.history ?? "push");
|
|
322
|
+
const navId = ++navigationId;
|
|
323
|
+
const url = new URL(path, location.origin);
|
|
324
|
+
|
|
325
|
+
if (beforeNavigateCb) {
|
|
326
|
+
const shouldProceed = beforeNavigateCb(url);
|
|
327
|
+
if (shouldProceed === false) return;
|
|
328
|
+
}
|
|
329
|
+
onNavigateCb?.(url);
|
|
330
|
+
saveScrollPosition();
|
|
331
|
+
|
|
332
|
+
// load the loading immediately
|
|
333
|
+
const destPattern = findPattern(url.pathname);
|
|
334
|
+
const DestPage = destPattern ? await resolveRoute(destPattern) : null;
|
|
335
|
+
if (navId !== navigationId) return;
|
|
336
|
+
if (DestPage?.loading) {
|
|
337
|
+
disposeCurrentPage?.();
|
|
338
|
+
disposeCurrentPage = null;
|
|
339
|
+
pushHydrationNodes([]);
|
|
340
|
+
let loadingNode: any;
|
|
341
|
+
disposeCurrentPage = withEffectScope(() => {
|
|
342
|
+
loadingNode = (DestPage as any).loading();
|
|
343
|
+
});
|
|
344
|
+
popHydrationNodes();
|
|
345
|
+
// replace only grimoire-page, keeps layout stable during transition
|
|
346
|
+
const pageEl = document.getElementById("grimoire-page");
|
|
347
|
+
if (pageEl) pageEl.replaceChildren(loadingNode);
|
|
348
|
+
else document.getElementById("grimoire-root")?.replaceChildren(loadingNode);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const res = await fetch(path, {
|
|
352
|
+
headers: { "x-grimoire-navigate": "1" },
|
|
353
|
+
redirect: "manual",
|
|
354
|
+
});
|
|
355
|
+
if (navId !== navigationId) return;
|
|
356
|
+
const contentType = res.headers.get("content-type") ?? "";
|
|
357
|
+
if (!contentType.includes("application/json")) {
|
|
358
|
+
window.location.href = path;
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
const json = await res.json();
|
|
362
|
+
if (navId !== navigationId) return;
|
|
363
|
+
const { data, layoutData, params, pattern, head, deps } = json;
|
|
364
|
+
const Page = await resolveRoute(pattern);
|
|
365
|
+
if (navId !== navigationId) return;
|
|
366
|
+
if (!Page) {
|
|
367
|
+
window.location.href = path;
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
let mergedData = data;
|
|
372
|
+
if (Page.load) {
|
|
373
|
+
try {
|
|
374
|
+
startCollecting();
|
|
375
|
+
const clientData = await Page.load({
|
|
376
|
+
params,
|
|
377
|
+
url,
|
|
378
|
+
request: new Request(url),
|
|
379
|
+
// layout loads run server-side; their data arrives in layoutData —
|
|
380
|
+
// parent() merges it outermost-first, mirroring the server context
|
|
381
|
+
parent: async () =>
|
|
382
|
+
Object.assign(
|
|
383
|
+
{},
|
|
384
|
+
...(Array.isArray(layoutData) ? layoutData : []).filter(
|
|
385
|
+
(d) => d && typeof d === "object",
|
|
386
|
+
),
|
|
387
|
+
),
|
|
388
|
+
});
|
|
389
|
+
// merge server-collected keys (page/layout server loads) with
|
|
390
|
+
// whatever this client-side load itself registered
|
|
391
|
+
setDeps(pattern, [
|
|
392
|
+
...(Array.isArray(deps) ? deps : []),
|
|
393
|
+
...stopCollecting(),
|
|
394
|
+
]);
|
|
395
|
+
mergedData = { ...clientData, ...data };
|
|
396
|
+
} catch (e: unknown) {
|
|
397
|
+
stopCollecting();
|
|
398
|
+
if (e && typeof e === "object" && "location" in e) {
|
|
399
|
+
window.location.href = String((e as { location: unknown }).location);
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
console.error("[grimoire] universal load error:", e);
|
|
403
|
+
}
|
|
404
|
+
if (navId !== navigationId) return;
|
|
405
|
+
} else if (Array.isArray(deps)) {
|
|
406
|
+
// no client-side load to run — the server's collected depends() keys
|
|
407
|
+
// (from +page.server.ts / +layout.server.ts) are the whole story
|
|
408
|
+
setDeps(pattern, deps);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// track state for HMR rerender
|
|
412
|
+
lastState = { data: mergedData, layoutData, params, pattern };
|
|
413
|
+
(globalThis as any).__grimoire_current_pattern__ = pattern;
|
|
414
|
+
|
|
415
|
+
disposeCurrentPage?.();
|
|
416
|
+
disposeCurrentPage = null;
|
|
417
|
+
pushHydrationNodes([]);
|
|
418
|
+
let rootNode: any;
|
|
419
|
+
disposeCurrentPage = withEffectScope(() => {
|
|
420
|
+
try {
|
|
421
|
+
rootNode = buildRouteTree(lastState!, Page);
|
|
422
|
+
} catch (e) {
|
|
423
|
+
console.error(e);
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
popHydrationNodes();
|
|
427
|
+
|
|
428
|
+
updateHead(head);
|
|
429
|
+
// isRefetch is read before currentPath moves, and captured, because
|
|
430
|
+
// applyScroll may run after this function has returned.
|
|
431
|
+
const isRefetch = url.pathname === currentPath;
|
|
432
|
+
|
|
433
|
+
// Scroll has to be decided in the same frame the content lands. Running it
|
|
434
|
+
// here, outside doSwap, meant the hash lookup hit the *outgoing* DOM: a
|
|
435
|
+
// same-page anchor was already present and worked, while an anchor on the
|
|
436
|
+
// incoming page did not exist yet and fell through to scrollToTop().
|
|
437
|
+
const applyScroll = () => {
|
|
438
|
+
if (url.hash) {
|
|
439
|
+
const target = document.getElementById(url.hash.slice(1));
|
|
440
|
+
if (!target) {
|
|
441
|
+
scrollToTop();
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
target.scrollIntoView();
|
|
445
|
+
// re-assert after layout settles (late effects/images can shift content)
|
|
446
|
+
requestAnimationFrame(() => target.scrollIntoView());
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
if (historyMode === "none") {
|
|
450
|
+
// popstate: the browser already moved through history — restore what
|
|
451
|
+
// we saved for this path (back/forward semantics)
|
|
452
|
+
restoreScrollPosition(url.pathname);
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
// fresh navigation starts at the top; a same-path replace
|
|
456
|
+
// (invalidate/refetch) keeps the current scroll
|
|
457
|
+
if (!isRefetch) scrollToTop();
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
// View transitions: animate the swap when the browser supports it and the
|
|
461
|
+
// user hasn't asked for reduced motion. The callback must stay synchronous.
|
|
462
|
+
const preserved = capturePreservedScroll();
|
|
463
|
+
const doSwap = () => {
|
|
464
|
+
document.getElementById("grimoire-root")?.replaceChildren(rootNode);
|
|
465
|
+
restorePreservedScroll(preserved);
|
|
466
|
+
applyScroll();
|
|
467
|
+
};
|
|
468
|
+
const doc = document as Document & {
|
|
469
|
+
startViewTransition?: (cb: () => void) => unknown;
|
|
470
|
+
};
|
|
471
|
+
if (
|
|
472
|
+
typeof doc.startViewTransition === "function" &&
|
|
473
|
+
!window.matchMedia("(prefers-reduced-motion: reduce)").matches
|
|
474
|
+
) {
|
|
475
|
+
doc.startViewTransition(doSwap);
|
|
476
|
+
} else {
|
|
477
|
+
doSwap();
|
|
478
|
+
}
|
|
479
|
+
currentPath = url.pathname;
|
|
480
|
+
currentSearch = url.search;
|
|
481
|
+
|
|
482
|
+
if (historyMode !== "none") {
|
|
483
|
+
history[historyMode === "replace" ? "replaceState" : "pushState"](
|
|
484
|
+
null,
|
|
485
|
+
"",
|
|
486
|
+
path,
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
afterNavigateCb?.(url);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
export interface RouterExtensions {
|
|
493
|
+
/** [param=matcher] validators from src/params/*.ts (manifest-generated). */
|
|
494
|
+
matchers?: Record<string, (value: string) => boolean>;
|
|
495
|
+
/** Server-collected depends() keys per route pattern (from SSR state). */
|
|
496
|
+
deps?: Record<string, string[]>;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
let paramMatcherFns: Record<string, (value: string) => boolean> = {};
|
|
500
|
+
|
|
501
|
+
export function initRouter(
|
|
502
|
+
routes: Record<string, RouteEntry>,
|
|
503
|
+
layouts: any[] = [],
|
|
504
|
+
initialDispose?: () => void,
|
|
505
|
+
ext?: RouterExtensions,
|
|
506
|
+
) {
|
|
507
|
+
paramMatcherFns = ext?.matchers ?? {};
|
|
508
|
+
routeMap = routes;
|
|
509
|
+
layoutList = layouts;
|
|
510
|
+
disposeCurrentPage = initialDispose ?? null;
|
|
511
|
+
currentPath = location.pathname;
|
|
512
|
+
currentSearch = location.search;
|
|
513
|
+
|
|
514
|
+
// the app owns scroll: the browser's automatic restoration fires after
|
|
515
|
+
// the router's own scroll and clobbers scroll-to-top / saved positions
|
|
516
|
+
if ("scrollRestoration" in history) history.scrollRestoration = "manual";
|
|
517
|
+
// dev HMR surface: available even without SSR state so the client can tell
|
|
518
|
+
// a missing router apart from a missing initial payload
|
|
519
|
+
const globals = globalThis as unknown as {
|
|
520
|
+
__grimoire_current_pattern__?: string;
|
|
521
|
+
__grimoire_routes__?: Record<string, RouteEntry>;
|
|
522
|
+
__grimoire_rerender__?: () => Promise<void>;
|
|
523
|
+
__grimoire_navigate__?: (path: string, replace?: boolean) => void;
|
|
524
|
+
__grimoire_hmr__?: HmrControl;
|
|
525
|
+
};
|
|
526
|
+
globals.__grimoire_routes__ = routeMap;
|
|
527
|
+
globals.__grimoire_rerender__ = hmrRerender;
|
|
528
|
+
globals.__grimoire_hmr__ = hmrControl;
|
|
529
|
+
globals.__grimoire_navigate__ = (path, replace) =>
|
|
530
|
+
navigate(path, replace ?? false);
|
|
531
|
+
|
|
532
|
+
// initial HMR state from SSR
|
|
533
|
+
const stateEl = document.getElementById("__grimoire_state__");
|
|
534
|
+
if (stateEl) {
|
|
535
|
+
const state = JSON.parse(stateEl.textContent!);
|
|
536
|
+
lastState = {
|
|
537
|
+
data: state.data,
|
|
538
|
+
layoutData: state.layoutData,
|
|
539
|
+
params: state.params,
|
|
540
|
+
pattern: state.pattern,
|
|
541
|
+
};
|
|
542
|
+
if (Array.isArray(state.deps)) {
|
|
543
|
+
setDeps(state.pattern, state.deps);
|
|
544
|
+
}
|
|
545
|
+
globals.__grimoire_current_pattern__ = state.pattern;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
saveScrollPosition();
|
|
549
|
+
document.addEventListener("click", handleClick);
|
|
550
|
+
// forms rendered client-side have no SSR-injected _csrf input
|
|
551
|
+
document.addEventListener("submit", onSubmitEnsureCsrf, true);
|
|
552
|
+
window.addEventListener("popstate", () => {
|
|
553
|
+
if (
|
|
554
|
+
location.pathname === currentPath &&
|
|
555
|
+
location.search === currentSearch
|
|
556
|
+
) {
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
// the browser already moved through history — render only, never
|
|
560
|
+
// push/replace here or the forward stack gets clobbered.
|
|
561
|
+
// The search has to come along: refetching the bare pathname served
|
|
562
|
+
// unfiltered data while the address bar still showed the query.
|
|
563
|
+
// The hash deliberately does not, so back/forward restores the saved
|
|
564
|
+
// scroll offset instead of re-jumping to the anchor, matching the
|
|
565
|
+
// browser's own behaviour for document history navigation.
|
|
566
|
+
navigate(location.pathname + location.search, { history: "none" });
|
|
567
|
+
});
|
|
568
|
+
setupPreload();
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
function updateHead(headHtml: string) {
|
|
572
|
+
document
|
|
573
|
+
.querySelectorAll("[data-grimoire-head]")
|
|
574
|
+
//biome-ignore lint/suspicious/useIterableCallbackReturn: shut up
|
|
575
|
+
.forEach((el) => el.remove());
|
|
576
|
+
const parsed = headHtml
|
|
577
|
+
? new DOMParser().parseFromString(`<head>${headHtml}</head>`, "text/html")
|
|
578
|
+
: null;
|
|
579
|
+
const incomingTitle = parsed?.querySelector("title");
|
|
580
|
+
if (incomingTitle) {
|
|
581
|
+
// capture before removing <title> elements — reading document.title
|
|
582
|
+
// after removal returns "" and would wipe the title
|
|
583
|
+
const prevTitle = document.title;
|
|
584
|
+
//biome-ignore lint/suspicious/useIterableCallbackReturn: shut up
|
|
585
|
+
document.querySelectorAll("title").forEach((el) => el.remove());
|
|
586
|
+
document.title = incomingTitle.textContent ?? prevTitle;
|
|
587
|
+
}
|
|
588
|
+
if (parsed) {
|
|
589
|
+
for (const el of Array.from(parsed.head.children)) {
|
|
590
|
+
if (el.tagName === "TITLE") continue;
|
|
591
|
+
(el as HTMLElement).dataset.grimoireHead = "1";
|
|
592
|
+
document.head.appendChild(el);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
function handleClick(e: MouseEvent) {
|
|
598
|
+
// let the browser handle modified clicks (new tab/window/download)
|
|
599
|
+
if (e.defaultPrevented || e.button !== 0) return;
|
|
600
|
+
if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
|
|
601
|
+
const a = (e.target as Element).closest("a");
|
|
602
|
+
if (!a) return;
|
|
603
|
+
if (a.target && a.target !== "_self") return;
|
|
604
|
+
if (a.hasAttribute("download")) return;
|
|
605
|
+
if (a.getAttribute("rel")?.split(/\s+/).includes("external")) return;
|
|
606
|
+
const href = a.getAttribute("href");
|
|
607
|
+
if (!href) return;
|
|
608
|
+
if (/^(https?:\/\/|\/\/|#|mailto:|tel:)/.test(href)) return;
|
|
609
|
+
// Only intercept clicks for known page routes — let server routes
|
|
610
|
+
// (redirects, API endpoints, etc.) navigate normally so the browser
|
|
611
|
+
// follows the response without the extra fetch-then-redirect dance.
|
|
612
|
+
try {
|
|
613
|
+
const url = new URL(href, location.origin);
|
|
614
|
+
if (!findPattern(url.pathname)) return;
|
|
615
|
+
// same-page anchor: native behavior scrolls to it, no re-render needed
|
|
616
|
+
if (url.pathname === location.pathname && url.hash) return;
|
|
617
|
+
} catch {
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
e.preventDefault();
|
|
621
|
+
navigate(href);
|
|
622
|
+
}
|