@krate/runtime 0.5.0-rc.6 → 0.5.0-rc.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/dist/handler-routing.d.ts +2 -23
- package/dist/handler-routing.js +4 -188
- package/dist/head-tags.js +5 -2
- package/dist/highlight.d.ts +3 -2
- package/dist/highlight.js +12 -6
- package/dist/island-swap.d.ts +45 -0
- package/dist/island-swap.js +608 -0
- package/dist/jsx-types.d.ts +7 -2
- package/dist/krate-f-router-lite.js +1 -1
- package/dist/krate-f-router.js +1 -1
- package/dist/krate-features.json +428 -0
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-island-swap.js +1 -0
- package/dist/krate-runtime.js +5 -5
- package/dist/react-compat.d.ts +13 -0
- package/dist/react-compat.js +67 -0
- package/dist/request-extras.d.ts +4 -0
- package/dist/request-extras.js +51 -0
- package/dist/route-source.d.ts +39 -0
- package/dist/route-source.js +532 -0
- package/dist/router.js +293 -23
- package/dist/server-handler.d.ts +20 -1
- package/dist/server-handler.js +140 -92
- package/dist/server.d.ts +10 -10
- package/dist/server.js +43 -15
- package/dist/site-config.d.ts +2 -0
- package/dist/testing.d.ts +4 -1
- package/dist/testing.js +3 -1
- package/package.json +1 -1
package/dist/router.js
CHANGED
|
@@ -14,6 +14,12 @@ import { siteConfig } from './site-config.js';
|
|
|
14
14
|
* removed before loading a new page and re-appended.
|
|
15
15
|
*/
|
|
16
16
|
const RUNTIME_CHUNK_RE = /\/chunks\/runtime\.[^/]+\.js$/;
|
|
17
|
+
/**
|
|
18
|
+
* Scripts that stay loaded across navigations: the runtime and the shared
|
|
19
|
+
* component chunks (chunks/c/), which only register the hydration code page
|
|
20
|
+
* scripts call. Each runs once per document.
|
|
21
|
+
*/
|
|
22
|
+
const PERSISTENT_SCRIPT_RE = /\/chunks\/(?:runtime\.[^/]+|c\/[^/]+)\.js$/;
|
|
17
23
|
function runtimeConfig() {
|
|
18
24
|
return siteConfig();
|
|
19
25
|
}
|
|
@@ -88,6 +94,45 @@ export function pageAssetURLs(html, baseUrl, loaded) {
|
|
|
88
94
|
}
|
|
89
95
|
// Router state (module scope, survives reinitRouter).
|
|
90
96
|
let initialized = false;
|
|
97
|
+
// Island bundles carry their own copy of this module, which is never
|
|
98
|
+
// initialized: the page's router registers itself here, and an uninitialized
|
|
99
|
+
// copy hands navigations to it instead of loading a new document.
|
|
100
|
+
const LIVE_ROUTER = Symbol.for('krate.router');
|
|
101
|
+
function liveRouter() {
|
|
102
|
+
if (initialized)
|
|
103
|
+
return null;
|
|
104
|
+
const g = globalThis;
|
|
105
|
+
let r = g[LIVE_ROUTER];
|
|
106
|
+
// The page runtime is loaded but its router has not started yet (a page
|
|
107
|
+
// with islands only, before the runtime starts it): start it here. The
|
|
108
|
+
// check compares krateRouter, not initRouter, so an island's copy does not
|
|
109
|
+
// keep its own (unused) router start code.
|
|
110
|
+
if (!r && typeof document !== 'undefined' && otherRuntime() && typeof g.initRouter === 'function') {
|
|
111
|
+
try {
|
|
112
|
+
g.initRouter();
|
|
113
|
+
}
|
|
114
|
+
catch (err) {
|
|
115
|
+
console.error(err);
|
|
116
|
+
}
|
|
117
|
+
r = g[LIVE_ROUTER];
|
|
118
|
+
}
|
|
119
|
+
return r && r !== krateRouter ? r : null;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* The page runtime's navigate when this is an island's copy of the router
|
|
123
|
+
* and the page has no registered router (an older runtime): it still owns
|
|
124
|
+
* the history, so a navigation goes through it before falling back to a
|
|
125
|
+
* full load.
|
|
126
|
+
*/
|
|
127
|
+
function pageNavigate() {
|
|
128
|
+
const nav = globalThis.navigate;
|
|
129
|
+
return otherRuntime() && typeof nav === 'function' ? nav : null;
|
|
130
|
+
}
|
|
131
|
+
/** True in a copy of this module other than the one the page runtime exposes. */
|
|
132
|
+
function otherRuntime() {
|
|
133
|
+
const pageRouter = globalThis.krateRouter;
|
|
134
|
+
return !!pageRouter && pageRouter !== krateRouter;
|
|
135
|
+
}
|
|
91
136
|
let navSeq = 0;
|
|
92
137
|
let pendingAbort;
|
|
93
138
|
let keySeq = 0;
|
|
@@ -171,6 +216,9 @@ function snapshot() {
|
|
|
171
216
|
}
|
|
172
217
|
/** Current router state (pathname without base path, search, hash, status). */
|
|
173
218
|
export function getNavigationState() {
|
|
219
|
+
const live = liveRouter();
|
|
220
|
+
if (live)
|
|
221
|
+
return live.getState();
|
|
174
222
|
if (typeof location === 'undefined') {
|
|
175
223
|
return { url: '', pathname: '/', search: '', hash: '', status: 'idle' };
|
|
176
224
|
}
|
|
@@ -202,6 +250,9 @@ function setStatus(next) {
|
|
|
202
250
|
* with getNavigationState().
|
|
203
251
|
*/
|
|
204
252
|
export function subscribeNavigation(fn) {
|
|
253
|
+
const live = liveRouter();
|
|
254
|
+
if (live)
|
|
255
|
+
return live.subscribe(fn);
|
|
205
256
|
listeners.add(fn);
|
|
206
257
|
return () => {
|
|
207
258
|
listeners.delete(fn);
|
|
@@ -298,6 +349,23 @@ function prefetchAsset(url) {
|
|
|
298
349
|
link.href = url;
|
|
299
350
|
document.head.appendChild(link);
|
|
300
351
|
}
|
|
352
|
+
/**
|
|
353
|
+
* The scripts this document already has: its script tags, and every resource
|
|
354
|
+
* it fetched, which includes the island modules an earlier client-side
|
|
355
|
+
* navigation imported and that the current head no longer lists.
|
|
356
|
+
*/
|
|
357
|
+
function loadedAssetURLs() {
|
|
358
|
+
const loaded = new Set();
|
|
359
|
+
document.querySelectorAll('script[src]').forEach((s) => loaded.add(s.src));
|
|
360
|
+
try {
|
|
361
|
+
for (const e of performance.getEntriesByType('resource'))
|
|
362
|
+
loaded.add(e.name);
|
|
363
|
+
}
|
|
364
|
+
catch {
|
|
365
|
+
// No resource timing: the script tags are all we know.
|
|
366
|
+
}
|
|
367
|
+
return loaded;
|
|
368
|
+
}
|
|
301
369
|
function prefetchPage(url) {
|
|
302
370
|
const key = cacheKey(url);
|
|
303
371
|
const cached = prefetchCache.get(key);
|
|
@@ -322,9 +390,7 @@ function prefetchPage(url) {
|
|
|
322
390
|
if (prefetchInflight.get(key) !== p)
|
|
323
391
|
return;
|
|
324
392
|
cachePut(key, { html, url: finalUrl, status: res.status, at: now() });
|
|
325
|
-
const
|
|
326
|
-
document.querySelectorAll('script[src]').forEach((s) => loaded.add(s.src));
|
|
327
|
-
for (const asset of pageAssetURLs(html, finalUrl, loaded))
|
|
393
|
+
for (const asset of pageAssetURLs(html, finalUrl, loadedAssetURLs()))
|
|
328
394
|
prefetchAsset(asset);
|
|
329
395
|
})
|
|
330
396
|
.catch(() => {
|
|
@@ -364,6 +430,24 @@ function shouldPrefetch(a) {
|
|
|
364
430
|
const v = a.getAttribute('data-prefetch');
|
|
365
431
|
return a.hasAttribute('data-prefetch') && v !== 'false' && v !== '0';
|
|
366
432
|
}
|
|
433
|
+
/** Most links per page that viewport prefetch will fetch. */
|
|
434
|
+
const VIEWPORT_PREFETCH_MAX = 5;
|
|
435
|
+
let viewportPrefetched = 0;
|
|
436
|
+
/** Whether the connection asks for no speculative traffic (Save-Data, 2g). */
|
|
437
|
+
function savingData() {
|
|
438
|
+
const c = navigator.connection;
|
|
439
|
+
if (!c)
|
|
440
|
+
return false;
|
|
441
|
+
return c.saveData === true || c.effectiveType === 'slow-2g' || c.effectiveType === '2g';
|
|
442
|
+
}
|
|
443
|
+
// Viewport prefetch is opt-in: prefetch="viewport" on the link, or the site-wide
|
|
444
|
+
// `prefetch: "viewport"` default. Everything else prefetches on intent only.
|
|
445
|
+
function wantsViewportPrefetch(a) {
|
|
446
|
+
if (!shouldPrefetch(a))
|
|
447
|
+
return false;
|
|
448
|
+
const v = a.getAttribute('data-prefetch');
|
|
449
|
+
return v === 'viewport' || (runtimeConfig().prefetch === 'viewport' && v !== 'intent');
|
|
450
|
+
}
|
|
367
451
|
// Above-the-fold/viewport prefetch is deferred to idle time so it never
|
|
368
452
|
// competes with first paint; hover/focus keeps the 200ms intent debounce.
|
|
369
453
|
function idle(fn) {
|
|
@@ -378,7 +462,7 @@ function prefetchHoverHandler(e) {
|
|
|
378
462
|
if (!t || typeof t.closest !== 'function')
|
|
379
463
|
return;
|
|
380
464
|
const a = t.closest('a[data-prefetch]');
|
|
381
|
-
if (!a || !shouldPrefetch(a) || !isLocalLink(a))
|
|
465
|
+
if (!a || !shouldPrefetch(a) || !isLocalLink(a) || savingData())
|
|
382
466
|
return;
|
|
383
467
|
const href = a.getAttribute('href');
|
|
384
468
|
if (!href)
|
|
@@ -396,10 +480,11 @@ function prefetchHoverHandler(e) {
|
|
|
396
480
|
prefetchTimers.set(a, timer);
|
|
397
481
|
}
|
|
398
482
|
function observePrefetchLinks() {
|
|
399
|
-
|
|
483
|
+
viewportPrefetched = 0;
|
|
484
|
+
if (!prefetchObserver || savingData())
|
|
400
485
|
return;
|
|
401
486
|
document.querySelectorAll('a[data-prefetch]').forEach((a) => {
|
|
402
|
-
if (
|
|
487
|
+
if (wantsViewportPrefetch(a))
|
|
403
488
|
prefetchObserver.observe(a);
|
|
404
489
|
});
|
|
405
490
|
}
|
|
@@ -413,10 +498,12 @@ function setupPrefetch() {
|
|
|
413
498
|
continue;
|
|
414
499
|
const a = entry.target;
|
|
415
500
|
const href = a.getAttribute('href');
|
|
416
|
-
if (href && isLocalLink(a) &&
|
|
501
|
+
if (href && isLocalLink(a) && wantsViewportPrefetch(a) && viewportPrefetched < VIEWPORT_PREFETCH_MAX && !savingData()) {
|
|
417
502
|
const url = new URL(href, location.href).href;
|
|
418
|
-
if (pathOf(url) !== renderedPath)
|
|
503
|
+
if (pathOf(url) !== renderedPath) {
|
|
504
|
+
viewportPrefetched++;
|
|
419
505
|
idle(() => void prefetchPage(url));
|
|
506
|
+
}
|
|
420
507
|
}
|
|
421
508
|
prefetchObserver.unobserve(entry.target);
|
|
422
509
|
}
|
|
@@ -437,6 +524,8 @@ function setupSpeculationRules() {
|
|
|
437
524
|
// is used instead of emitting a blocked script.
|
|
438
525
|
if (document.querySelector('meta[http-equiv="Content-Security-Policy" i]'))
|
|
439
526
|
return;
|
|
527
|
+
if (savingData())
|
|
528
|
+
return;
|
|
440
529
|
try {
|
|
441
530
|
const script = document.createElement('script');
|
|
442
531
|
script.type = 'speculationrules';
|
|
@@ -693,9 +782,13 @@ function loadScript(script) {
|
|
|
693
782
|
s.src = script.src;
|
|
694
783
|
if (script.module)
|
|
695
784
|
s.type = 'module';
|
|
785
|
+
// Classic scripts run in document order, as on a full load: a shared
|
|
786
|
+
// component chunk must register before the page script that calls it.
|
|
787
|
+
else
|
|
788
|
+
s.async = false;
|
|
696
789
|
// Only page scripts are disposable across navigations; the shared runtime
|
|
697
|
-
//
|
|
698
|
-
if (!
|
|
790
|
+
// and component chunks stay in place.
|
|
791
|
+
if (!PERSISTENT_SCRIPT_RE.test(script.src))
|
|
699
792
|
s.setAttribute('data-krate-spa', '');
|
|
700
793
|
s.onload = () => resolve(true);
|
|
701
794
|
s.onerror = () => resolve(false);
|
|
@@ -758,6 +851,71 @@ function applyScroll(url, req) {
|
|
|
758
851
|
window.scrollTo(0, 0);
|
|
759
852
|
}
|
|
760
853
|
let focusedRoot = null;
|
|
854
|
+
// Attributes that tell a focused control apart from its siblings, in addition
|
|
855
|
+
// to its tag, when it has no id.
|
|
856
|
+
const FOCUS_SIG_ATTRS = ['name', 'type', 'role', 'aria-label', 'placeholder', 'data-testid', 'data-slot', 'href'];
|
|
857
|
+
function focusSignature(el) {
|
|
858
|
+
let s = el.tagName;
|
|
859
|
+
for (const a of FOCUS_SIG_ATTRS) {
|
|
860
|
+
const v = el.getAttribute(a);
|
|
861
|
+
if (v !== null)
|
|
862
|
+
s += '\u0000' + a + '=' + v;
|
|
863
|
+
}
|
|
864
|
+
return s;
|
|
865
|
+
}
|
|
866
|
+
function sameSignature(root, sig, tag) {
|
|
867
|
+
return Array.from(root.getElementsByTagName(tag)).filter((e) => focusSignature(e) === sig);
|
|
868
|
+
}
|
|
869
|
+
/** Whether the swap of plan replaces el (it sits in the swapped region or a swapped slot). */
|
|
870
|
+
function replacedBySwap(el, root, plan) {
|
|
871
|
+
if (!root.contains(el))
|
|
872
|
+
return false;
|
|
873
|
+
if (!plan.region)
|
|
874
|
+
return true;
|
|
875
|
+
const within = (pair) => !!(pair[0].compareDocumentPosition(el) & Node.DOCUMENT_POSITION_FOLLOWING) &&
|
|
876
|
+
!!(pair[1].compareDocumentPosition(el) & Node.DOCUMENT_POSITION_PRECEDING);
|
|
877
|
+
return within(plan.region.live) || plan.slots.some((sl) => within(sl.live));
|
|
878
|
+
}
|
|
879
|
+
function captureFocus(root, plan) {
|
|
880
|
+
const el = document.activeElement;
|
|
881
|
+
if (!el || el === document.body || el === root || !replacedBySwap(el, root, plan))
|
|
882
|
+
return null;
|
|
883
|
+
const sig = focusSignature(el);
|
|
884
|
+
const field = el;
|
|
885
|
+
let start = null;
|
|
886
|
+
let end = null;
|
|
887
|
+
try {
|
|
888
|
+
start = typeof field.selectionStart === 'number' ? field.selectionStart : null;
|
|
889
|
+
end = typeof field.selectionEnd === 'number' ? field.selectionEnd : null;
|
|
890
|
+
}
|
|
891
|
+
catch {
|
|
892
|
+
/* input types without a selection */
|
|
893
|
+
}
|
|
894
|
+
return { id: el.id, sig, index: sameSignature(root, sig, el.tagName).indexOf(el), start, end };
|
|
895
|
+
}
|
|
896
|
+
function hasFocusWithin(root) {
|
|
897
|
+
const a = document.activeElement;
|
|
898
|
+
return !!a && a !== document.body && a !== root && root.contains(a);
|
|
899
|
+
}
|
|
900
|
+
function restoreFocus(root, mark) {
|
|
901
|
+
const tag = mark.sig.split('\u0000')[0];
|
|
902
|
+
let el = mark.id ? document.getElementById(mark.id) : null;
|
|
903
|
+
if (!el || !root.contains(el))
|
|
904
|
+
el = sameSignature(root, mark.sig, tag)[mark.index] ?? null;
|
|
905
|
+
if (!el || typeof el.focus !== 'function')
|
|
906
|
+
return;
|
|
907
|
+
try {
|
|
908
|
+
el.focus({ preventScroll: true });
|
|
909
|
+
const field = el;
|
|
910
|
+
if (mark.start !== null && mark.end !== null && document.activeElement === el && typeof field.setSelectionRange === 'function') {
|
|
911
|
+
const n = field.value.length;
|
|
912
|
+
field.setSelectionRange(Math.min(mark.start, n), Math.min(mark.end, n));
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
catch {
|
|
916
|
+
/* ignore */
|
|
917
|
+
}
|
|
918
|
+
}
|
|
761
919
|
/** Move focus to the content root after navigation (a11y). */
|
|
762
920
|
function focusContent() {
|
|
763
921
|
const active = document.activeElement;
|
|
@@ -979,7 +1137,20 @@ async function commitDocument(loaded, req, seq) {
|
|
|
979
1137
|
sheets.abort();
|
|
980
1138
|
return;
|
|
981
1139
|
}
|
|
1140
|
+
// A navigation that stays on the page (a query change, router.refresh())
|
|
1141
|
+
// keeps the page's islands mounted and their DOM in place; a control
|
|
1142
|
+
// focused in one of them keeps the focus. When the swap replaces the focused
|
|
1143
|
+
// element, focus moves to the same element in the new DOM.
|
|
1144
|
+
const samePage = normPath(new URL(url, location.href).pathname) === normPath(location.pathname);
|
|
1145
|
+
let focusMark = null;
|
|
1146
|
+
let focused = null;
|
|
1147
|
+
let islandJobs = [];
|
|
982
1148
|
const mutate = () => {
|
|
1149
|
+
if (samePage)
|
|
1150
|
+
focusMark = captureFocus(oldRoot, plan);
|
|
1151
|
+
const active = document.activeElement;
|
|
1152
|
+
focused = active && active !== document.body && oldRoot.contains(active) ? active : null;
|
|
1153
|
+
const sel = focused ? selectionOf(focused) : null;
|
|
983
1154
|
const title = doc.querySelector('title');
|
|
984
1155
|
if (title)
|
|
985
1156
|
document.title = title.textContent || '';
|
|
@@ -988,32 +1159,47 @@ async function commitDocument(loaded, req, seq) {
|
|
|
988
1159
|
g.krateDisposeExcept(plan.keep);
|
|
989
1160
|
else if (typeof g.disposeAll === 'function')
|
|
990
1161
|
g.disposeAll();
|
|
1162
|
+
// Islands in the new markup that are mounted already stay mounted; a
|
|
1163
|
+
// navigation to another page keeps only those with the same props.
|
|
1164
|
+
const opts = { sameProps: !samePage };
|
|
1165
|
+
// Imported into one fragment, so the nodes keep their siblings (an
|
|
1166
|
+
// island's open and close comments find each other).
|
|
1167
|
+
const imported = (nodes) => {
|
|
1168
|
+
const f = document.createDocumentFragment();
|
|
1169
|
+
for (const n of nodes)
|
|
1170
|
+
f.appendChild(document.importNode(n, true));
|
|
1171
|
+
return Array.from(f.childNodes);
|
|
1172
|
+
};
|
|
991
1173
|
if (plan.region) {
|
|
992
1174
|
// Only the deepest boundary that differs is replaced; the kept layouts'
|
|
993
1175
|
// DOM (and its state, focus and scroll containers) stays in place.
|
|
994
1176
|
const { live, next } = plan.region;
|
|
995
|
-
|
|
996
|
-
const nodes = between(next[0], next[1]).map((n) => document.importNode(n, true));
|
|
997
|
-
for (const n of nodes)
|
|
998
|
-
live[1].parentNode.insertBefore(n, live[1]);
|
|
1177
|
+
islandJobs = swapNodes(live[0].parentNode, live[0], live[1], imported(between(next[0], next[1])), opts);
|
|
999
1178
|
for (const sl of plan.slots) {
|
|
1000
|
-
|
|
1001
|
-
for (const n of between(sl.next[0], sl.next[1]))
|
|
1002
|
-
sl.live[1].parentNode.insertBefore(document.importNode(n, true), sl.live[1]);
|
|
1179
|
+
islandJobs.push(...swapNodes(sl.live[0].parentNode, sl.live[0], sl.live[1], imported(between(sl.next[0], sl.next[1])), opts));
|
|
1003
1180
|
}
|
|
1004
1181
|
syncLoadingTemplate(oldRoot, newRoot);
|
|
1005
1182
|
}
|
|
1006
1183
|
else {
|
|
1007
|
-
|
|
1008
|
-
oldRoot.replaceChildren(...nodes);
|
|
1184
|
+
islandJobs = swapNodes(oldRoot, null, null, imported(Array.from(newRoot.childNodes)), opts);
|
|
1009
1185
|
}
|
|
1010
1186
|
currentChain = plan.chain;
|
|
1011
1187
|
// Detached slot content of earlier intercepts belonged to the old page.
|
|
1188
|
+
interceptSnapshots.forEach((snap) => unmountIslands(snap.nodes));
|
|
1012
1189
|
interceptSnapshots.clear();
|
|
1013
1190
|
sheets.commit();
|
|
1014
1191
|
reconcileHeadMeta(doc);
|
|
1015
1192
|
reconcileDocAttrs(doc);
|
|
1016
1193
|
applyViewTransitionNames(document);
|
|
1194
|
+
if (focused && focused.isConnected) {
|
|
1195
|
+
// The focused element was kept: it keeps the focus (and its selection)
|
|
1196
|
+
// even when it had to move.
|
|
1197
|
+
if (document.activeElement !== focused)
|
|
1198
|
+
refocus(focused, sel);
|
|
1199
|
+
focusMark = null;
|
|
1200
|
+
}
|
|
1201
|
+
else if (focusMark)
|
|
1202
|
+
restoreFocus(oldRoot, focusMark);
|
|
1017
1203
|
};
|
|
1018
1204
|
await runMutation(mutate);
|
|
1019
1205
|
if (seq !== navSeq)
|
|
@@ -1069,6 +1255,68 @@ async function commitDocument(loaded, req, seq) {
|
|
|
1069
1255
|
}
|
|
1070
1256
|
refreshPageBindings();
|
|
1071
1257
|
dispatch('krate:navigate-end', { url: location.href, status: loaded.status });
|
|
1258
|
+
// An island that rendered its element anew while mounting took the focus
|
|
1259
|
+
// with it; look the element up again once the islands mounted.
|
|
1260
|
+
const mark = focusMark;
|
|
1261
|
+
if (mark && !hasFocusWithin(oldRoot)) {
|
|
1262
|
+
await islandsSettled(islandJobs);
|
|
1263
|
+
if (seq === navSeq && !hasFocusWithin(oldRoot))
|
|
1264
|
+
restoreFocus(oldRoot, mark);
|
|
1265
|
+
}
|
|
1266
|
+
}
|
|
1267
|
+
/** The island-preserving swap (island-swap.ts), which the island loader chunk installs on pages with islands. */
|
|
1268
|
+
function islandSwap() {
|
|
1269
|
+
const s = globalThis.__krateIslandSwap;
|
|
1270
|
+
return s && typeof s.swap === 'function' && typeof s.unmount === 'function' ? s : null;
|
|
1271
|
+
}
|
|
1272
|
+
/**
|
|
1273
|
+
* Replaces the children of parent between start and end (all of them when
|
|
1274
|
+
* both are null) with nodes, keeping the page's mounted islands when the page
|
|
1275
|
+
* has any.
|
|
1276
|
+
*/
|
|
1277
|
+
function swapNodes(parent, start, end, nodes, opts) {
|
|
1278
|
+
const s = islandSwap();
|
|
1279
|
+
if (s)
|
|
1280
|
+
return s.swap(parent, start, end, nodes, opts);
|
|
1281
|
+
if (start && end) {
|
|
1282
|
+
clearBetween(start, end);
|
|
1283
|
+
for (const n of nodes)
|
|
1284
|
+
parent.insertBefore(n, end);
|
|
1285
|
+
}
|
|
1286
|
+
else
|
|
1287
|
+
parent.replaceChildren(...nodes);
|
|
1288
|
+
return [];
|
|
1289
|
+
}
|
|
1290
|
+
function unmountIslands(nodes) {
|
|
1291
|
+
const s = islandSwap();
|
|
1292
|
+
if (s)
|
|
1293
|
+
s.unmount(nodes);
|
|
1294
|
+
}
|
|
1295
|
+
/** Waits (at most a few seconds) for the islands the island loader is mounting and for island updates. */
|
|
1296
|
+
function islandsSettled(jobs) {
|
|
1297
|
+
const ready = globalThis.__krateIslandsReady;
|
|
1298
|
+
const all = Promise.all([...jobs, ready && typeof ready.then === 'function' ? ready : null]);
|
|
1299
|
+
return Promise.race([all, new Promise((r) => setTimeout(r, 3000))]);
|
|
1300
|
+
}
|
|
1301
|
+
function selectionOf(el) {
|
|
1302
|
+
const field = el;
|
|
1303
|
+
try {
|
|
1304
|
+
return typeof field.selectionStart === 'number' && typeof field.selectionEnd === 'number' ? [field.selectionStart, field.selectionEnd] : null;
|
|
1305
|
+
}
|
|
1306
|
+
catch {
|
|
1307
|
+
return null;
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
function refocus(el, sel) {
|
|
1311
|
+
try {
|
|
1312
|
+
el.focus({ preventScroll: true });
|
|
1313
|
+
if (sel && document.activeElement === el && typeof el.setSelectionRange === 'function') {
|
|
1314
|
+
el.setSelectionRange(sel[0], sel[1]);
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
catch {
|
|
1318
|
+
/* ignore */
|
|
1319
|
+
}
|
|
1072
1320
|
}
|
|
1073
1321
|
/**
|
|
1074
1322
|
* Runs the DOM mutation, inside a View Transition when supported. Resolves
|
|
@@ -1151,6 +1399,7 @@ function between(start, end) {
|
|
|
1151
1399
|
}
|
|
1152
1400
|
function clearBetween(start, end) {
|
|
1153
1401
|
const dispose = globalThis.disposeNode;
|
|
1402
|
+
unmountIslands(between(start, end));
|
|
1154
1403
|
for (const n of between(start, end)) {
|
|
1155
1404
|
n.parentNode.removeChild(n);
|
|
1156
1405
|
if (typeof dispose === 'function')
|
|
@@ -1730,12 +1979,15 @@ function refreshPageBindings() {
|
|
|
1730
1979
|
// Mark the page scripts as disposable so they're removed and re-executed
|
|
1731
1980
|
// against the fresh DOM on the next navigation.
|
|
1732
1981
|
document.querySelectorAll('script[src]').forEach((s) => {
|
|
1733
|
-
if (!
|
|
1982
|
+
if (!PERSISTENT_SCRIPT_RE.test(s.src))
|
|
1734
1983
|
s.setAttribute('data-krate-spa', '');
|
|
1735
1984
|
});
|
|
1736
1985
|
}
|
|
1737
1986
|
// Public API
|
|
1738
1987
|
function refresh() {
|
|
1988
|
+
const live = liveRouter();
|
|
1989
|
+
if (live)
|
|
1990
|
+
return live.refresh();
|
|
1739
1991
|
if (!initialized || typeof location === 'undefined') {
|
|
1740
1992
|
if (typeof location !== 'undefined')
|
|
1741
1993
|
location.reload();
|
|
@@ -1752,7 +2004,13 @@ function refresh() {
|
|
|
1752
2004
|
}
|
|
1753
2005
|
function push(url, opts = {}) {
|
|
1754
2006
|
if (!initialized) {
|
|
1755
|
-
|
|
2007
|
+
const live = liveRouter();
|
|
2008
|
+
if (live)
|
|
2009
|
+
return live.push(url, opts);
|
|
2010
|
+
const nav = pageNavigate();
|
|
2011
|
+
if (nav)
|
|
2012
|
+
nav(url, { ...opts, replace: false });
|
|
2013
|
+
else if (typeof location !== 'undefined')
|
|
1756
2014
|
location.assign(withBase(url));
|
|
1757
2015
|
return Promise.resolve();
|
|
1758
2016
|
}
|
|
@@ -1760,7 +2018,13 @@ function push(url, opts = {}) {
|
|
|
1760
2018
|
}
|
|
1761
2019
|
function replace(url, opts = {}) {
|
|
1762
2020
|
if (!initialized) {
|
|
1763
|
-
|
|
2021
|
+
const live = liveRouter();
|
|
2022
|
+
if (live)
|
|
2023
|
+
return live.replace(url, opts);
|
|
2024
|
+
const nav = pageNavigate();
|
|
2025
|
+
if (nav)
|
|
2026
|
+
nav(url, { ...opts, replace: true });
|
|
2027
|
+
else if (typeof location !== 'undefined')
|
|
1764
2028
|
location.replace(withBase(url));
|
|
1765
2029
|
return Promise.resolve();
|
|
1766
2030
|
}
|
|
@@ -1789,6 +2053,9 @@ export const krateRouter = {
|
|
|
1789
2053
|
},
|
|
1790
2054
|
refresh,
|
|
1791
2055
|
prefetch(url) {
|
|
2056
|
+
const live = liveRouter();
|
|
2057
|
+
if (live)
|
|
2058
|
+
return live.prefetch(url);
|
|
1792
2059
|
if (!initialized || typeof location === 'undefined')
|
|
1793
2060
|
return Promise.resolve();
|
|
1794
2061
|
let u;
|
|
@@ -1812,7 +2079,7 @@ export const krateRouter = {
|
|
|
1812
2079
|
* client-side, true when a full navigation was triggered.
|
|
1813
2080
|
*/
|
|
1814
2081
|
export function navigate(url, opts = {}) {
|
|
1815
|
-
if (initialized) {
|
|
2082
|
+
if (initialized || liveRouter()) {
|
|
1816
2083
|
void (opts.replace ? replace(url, opts) : push(url, opts));
|
|
1817
2084
|
return false;
|
|
1818
2085
|
}
|
|
@@ -1832,6 +2099,7 @@ export function initRouter() {
|
|
|
1832
2099
|
return;
|
|
1833
2100
|
}
|
|
1834
2101
|
initialized = true;
|
|
2102
|
+
globalThis[LIVE_ROUTER] = krateRouter;
|
|
1835
2103
|
// The first document ran its own boot scripts.
|
|
1836
2104
|
document.querySelectorAll('script[data-krate-boot]').forEach((s) => bootRan.add(s.textContent || ''));
|
|
1837
2105
|
renderedPath = pathOf(location.href);
|
|
@@ -1875,6 +2143,8 @@ export function destroyRouter() {
|
|
|
1875
2143
|
if (!initialized)
|
|
1876
2144
|
return;
|
|
1877
2145
|
initialized = false;
|
|
2146
|
+
if (globalThis[LIVE_ROUTER] === krateRouter)
|
|
2147
|
+
delete globalThis[LIVE_ROUTER];
|
|
1878
2148
|
document.removeEventListener('click', clickHandler);
|
|
1879
2149
|
document.removeEventListener('submit', submitHandler);
|
|
1880
2150
|
window.removeEventListener('popstate', popstateHandler);
|
package/dist/server-handler.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type IsrStore, type RegionMeta } from "./ssr-core.js";
|
|
2
2
|
import { isDynamicPattern } from "./handler-routing.js";
|
|
3
|
+
import { type RouteCondition } from "./route-source.js";
|
|
3
4
|
import { type SegmentEntry } from "./handler-segments.js";
|
|
4
5
|
import { type CacheAdapter, type CacheSpec } from "./cache-adapter.js";
|
|
5
6
|
export type DeployTarget = "node" | "vercel" | "netlify" | "cloudflare";
|
|
@@ -24,15 +25,25 @@ export interface HandlerConfig {
|
|
|
24
25
|
source: string;
|
|
25
26
|
destination: string;
|
|
26
27
|
permanent?: boolean;
|
|
28
|
+
statusCode?: number;
|
|
29
|
+
has?: RouteCondition[];
|
|
30
|
+
missing?: RouteCondition[];
|
|
27
31
|
basePath?: boolean;
|
|
28
32
|
}>;
|
|
29
33
|
rewrites: Array<{
|
|
30
34
|
source: string;
|
|
31
35
|
destination: string;
|
|
36
|
+
has?: RouteCondition[];
|
|
37
|
+
missing?: RouteCondition[];
|
|
32
38
|
}>;
|
|
33
39
|
headers: Array<{
|
|
34
40
|
source: string;
|
|
35
|
-
headers: Record<string, string
|
|
41
|
+
headers: Record<string, string> | Array<{
|
|
42
|
+
key: string;
|
|
43
|
+
value: string;
|
|
44
|
+
}>;
|
|
45
|
+
has?: RouteCondition[];
|
|
46
|
+
missing?: RouteCondition[];
|
|
36
47
|
}>;
|
|
37
48
|
/** Resolved security headers; an empty string means "do not send". */
|
|
38
49
|
security: {
|
|
@@ -204,6 +215,14 @@ export type KrateHandler = (request: Request, ctx?: HandlerContext) => Promise<R
|
|
|
204
215
|
* Cloudflare Worker wraps the exports of src/cloudflare.ts with it.
|
|
205
216
|
*/
|
|
206
217
|
export declare function runWithPlatform<R>(target: DeployTarget, ctx: HandlerContext, fn: () => R): R;
|
|
218
|
+
/**
|
|
219
|
+
* The name/value pairs of a headers rule: an object, or Next's array of
|
|
220
|
+
* { key, value } where a repeated name (any case) keeps its last value.
|
|
221
|
+
*/
|
|
222
|
+
export declare function ruleHeaderPairs(h: Record<string, string> | Array<{
|
|
223
|
+
key: string;
|
|
224
|
+
value: string;
|
|
225
|
+
}>): Array<[string, string]>;
|
|
207
226
|
/**
|
|
208
227
|
* Opens the closure token of an inline server action: AES-256-GCM over the
|
|
209
228
|
* JSON of the captured values, authenticated against the action id, with the
|