@krate/runtime 0.5.0-rc.6 → 0.5.0-rc.8

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.
@@ -0,0 +1,608 @@
1
+ // Island-preserving DOM swaps for the SPA router.
2
+ //
3
+ // A router swap (a navigation, router.refresh(), the refresh after a server
4
+ // action that revalidated) replaces server-rendered markup with the markup of
5
+ // the new document. Client islands in that markup are not replaced: an island
6
+ // mounted in the live DOM that has a counterpart in the new markup keeps its
7
+ // nodes and its mounted root, and is re-rendered in place with the new server
8
+ // props (state, DOM nodes and focus survive), as React keeps a mounted client
9
+ // component across a refresh. An island the swap really removes is unmounted
10
+ // before its markup goes, so its effects and portals are cleaned up.
11
+ //
12
+ // Islands are the ones the island loader lists in window.__krateIslands: a
13
+ // Preact island is the nodes between <!--krate-island:{m,e,s,p}--> and
14
+ // <!--/krate-island-->, a real-React island a <krate-island data-src> element
15
+ // (props in the JSON script before it). The loader puts each island's record
16
+ // on its open comment or element (__krateIsland); a mounted record carries the
17
+ // handle its chunk's mount returned: update(props, pool), unmount() and, for a
18
+ // Preact island, the server children (slots) it adopted from the document.
19
+ //
20
+ // Keying: islands are matched per container (the swapped region, or one
21
+ // server-children slot of a kept island), outermost islands only, in document
22
+ // order by module and export: the n-th island of a component in the live
23
+ // region pairs with the n-th one in the new markup. A navigation to another
24
+ // pathname only keeps an island whose server props are unchanged too (a
25
+ // layout's island); one whose props differ belongs to the other page and is
26
+ // mounted anew, as React remounts a page's components.
27
+ //
28
+ // The live ancestors of a kept island stay in place as long as the new markup
29
+ // has elements of the same tags on the same path (their attributes follow the
30
+ // new markup), so the island is never detached; everything else outside the
31
+ // islands is replaced by the new markup.
32
+ const OPEN = 'krate-island:';
33
+ const CLOSE = '/krate-island';
34
+ const OPEN_TAG = '<!--' + OPEN;
35
+ const CLOSE_TAG = '<!--' + CLOSE + '-->';
36
+ const SLOT_OPEN = /^krate-slot:([0-9a-z]+)$/;
37
+ function isOpen(n) {
38
+ return n.nodeType === 8 && n.data.indexOf(OPEN) === 0;
39
+ }
40
+ function isClose(n) {
41
+ return n.nodeType === 8 && n.data === CLOSE;
42
+ }
43
+ function isHost(n) {
44
+ return n.nodeType === 1 && n.localName === 'krate-island' && n.hasAttribute('data-src');
45
+ }
46
+ function closeOf(start) {
47
+ let d = 0;
48
+ for (let n = start.nextSibling; n; n = n.nextSibling) {
49
+ if (isOpen(n))
50
+ d++;
51
+ else if (isClose(n)) {
52
+ if (!d)
53
+ return n;
54
+ d--;
55
+ }
56
+ }
57
+ return null;
58
+ }
59
+ function rangeNodes(start, end) {
60
+ const out = [start];
61
+ for (let n = start.nextSibling; n && n !== end; n = n.nextSibling)
62
+ out.push(n);
63
+ out.push(end);
64
+ return out;
65
+ }
66
+ function nodesOf(i) {
67
+ return i.el ? [i.el] : rangeNodes(i.start, i.end);
68
+ }
69
+ function propsText(el) {
70
+ if (el.hasAttribute('data-props'))
71
+ return el.getAttribute('data-props') || '';
72
+ const s = el.previousElementSibling;
73
+ return s && s.localName === 'script' && s.hasAttribute('data-krate-props') ? s.textContent || '' : '';
74
+ }
75
+ /** The outermost islands in list, in document order. */
76
+ function scan(list, chain = [], out = []) {
77
+ for (let i = 0; i < list.length; i++) {
78
+ const n = list[i];
79
+ if (isOpen(n)) {
80
+ const end = closeOf(n);
81
+ let p = null;
82
+ try {
83
+ p = JSON.parse(n.data.slice(OPEN.length));
84
+ }
85
+ catch {
86
+ p = null;
87
+ }
88
+ if (!end || !p)
89
+ continue;
90
+ out.push({ key: p.m + '#' + p.e, start: n, end, json: n.data.slice(OPEN.length), chain, rec: n.__krateIsland });
91
+ while (i < list.length - 1 && list[i] !== end)
92
+ i++;
93
+ }
94
+ else if (isHost(n)) {
95
+ out.push({
96
+ key: n.getAttribute('data-module') + '#' + n.getAttribute('data-export'),
97
+ el: n,
98
+ json: propsText(n),
99
+ chain,
100
+ rec: n.__krateIsland,
101
+ });
102
+ }
103
+ else if (n.nodeType === 1) {
104
+ scan(n.childNodes, chain.concat(n), out);
105
+ }
106
+ }
107
+ return out;
108
+ }
109
+ function mounted(rec) {
110
+ const h = rec && rec.handle;
111
+ return !!rec && !rec.gone && rec.status === 'hydrated' && !!h && typeof h.update === 'function' && typeof h.unmount === 'function';
112
+ }
113
+ /** Calls fn on every slot id referenced in props (server children adopted from the document). */
114
+ function eachSlotRef(v, fn) {
115
+ if (Array.isArray(v))
116
+ return v.map((x) => eachSlotRef(x, fn));
117
+ if (!v || typeof v !== 'object')
118
+ return v;
119
+ const out = {};
120
+ for (const k in v) {
121
+ const x = v[k];
122
+ if (k === '__krateSlotRef' && typeof x === 'string')
123
+ out[k] = fn(x);
124
+ else if (k === 'r' && v.$ === 'h' && typeof x === 'string')
125
+ out[k] = fn(x);
126
+ else
127
+ out[k] = eachSlotRef(x, fn);
128
+ }
129
+ return out;
130
+ }
131
+ function parse(json) {
132
+ try {
133
+ return JSON.parse(json);
134
+ }
135
+ catch {
136
+ return undefined;
137
+ }
138
+ }
139
+ /** The server props of an island, with slot ids (random per render) blanked out. */
140
+ function propsKey(i) {
141
+ const v = parse(i.json);
142
+ return JSON.stringify(eachSlotRef(i.el ? v : v && v.p, () => ''));
143
+ }
144
+ /** Serializes nodes as the island chunks snapshot a slot's server HTML. */
145
+ export function nodesHTML(nodes) {
146
+ let s = '';
147
+ for (let i = 0; i < nodes.length; i++) {
148
+ const n = nodes[i];
149
+ s += n.nodeType === 1 ? n.outerHTML : n.nodeType === 8 ? '<!--' + n.data + '-->' : n.textContent || '';
150
+ }
151
+ return s;
152
+ }
153
+ /** The next real-React island element (one with data-src) in html from i, or -1. */
154
+ function hostAt(html, i) {
155
+ for (let h = html.indexOf('<krate-island ', i); h >= 0; h = html.indexOf('<krate-island ', h + 1)) {
156
+ if (/\sdata-src=/.test(html.slice(h, html.indexOf('>', h))))
157
+ return h;
158
+ }
159
+ return -1;
160
+ }
161
+ /**
162
+ * HTML with every island replaced by a marker naming its component, so two
163
+ * renders compare equal when only the islands (their props, or what they
164
+ * rendered since) differ.
165
+ */
166
+ export function maskIslands(html) {
167
+ let out = '';
168
+ let i = 0;
169
+ for (;;) {
170
+ const a = html.indexOf(OPEN_TAG, i);
171
+ const h = hostAt(html, i);
172
+ if (a < 0 && h < 0)
173
+ return out + html.slice(i);
174
+ if (h >= 0 && (a < 0 || h < a)) {
175
+ const e = html.indexOf('</krate-island>', h);
176
+ if (e < 0)
177
+ return out + html.slice(i);
178
+ out += html.slice(i, h) + '<!--i:' + (/data-module="([^"]*)"/.exec(html.slice(h, html.indexOf('>', h))) || ['', ''])[1] + '-->';
179
+ i = e + 15;
180
+ continue;
181
+ }
182
+ const headEnd = html.indexOf('-->', a);
183
+ if (headEnd < 0)
184
+ return out + html.slice(i);
185
+ const p = parse(html.slice(a + OPEN_TAG.length, headEnd));
186
+ let j = headEnd + 3;
187
+ let d = 0;
188
+ for (;;) {
189
+ const o = html.indexOf(OPEN_TAG, j);
190
+ const c = html.indexOf(CLOSE_TAG, j);
191
+ if (c < 0)
192
+ return out + html.slice(i);
193
+ if (o >= 0 && o < c) {
194
+ d++;
195
+ j = o + OPEN_TAG.length;
196
+ continue;
197
+ }
198
+ j = c + CLOSE_TAG.length;
199
+ if (!d)
200
+ break;
201
+ d--;
202
+ }
203
+ out += html.slice(i, a) + '<!--i:' + (p ? p.m + '#' + p.e : '') + '-->';
204
+ i = j;
205
+ }
206
+ }
207
+ /** Every island record in nodes (nested ones included), in document order. */
208
+ function recordsIn(nodes) {
209
+ const out = [];
210
+ const see = (n) => {
211
+ const r = n.__krateIsland;
212
+ if (r && (isOpen(n) || isHost(n)))
213
+ out.push(r);
214
+ };
215
+ for (let i = 0; i < nodes.length; i++) {
216
+ const n = nodes[i];
217
+ see(n);
218
+ if (n.nodeType !== 1)
219
+ continue;
220
+ const doc = n.ownerDocument || document;
221
+ const w = doc.createTreeWalker(n, 129 /* SHOW_ELEMENT | SHOW_COMMENT */);
222
+ for (let c = w.nextNode(); c; c = w.nextNode())
223
+ see(c);
224
+ }
225
+ return out;
226
+ }
227
+ /**
228
+ * Unmounts every island in nodes, innermost first, so their effects and
229
+ * portals are cleaned up before the nodes leave the document. An island still
230
+ * loading is marked gone (the loader unmounts it once its chunk mounted).
231
+ */
232
+ export function unmountIslandsIn(nodes) {
233
+ const recs = recordsIn(nodes);
234
+ for (let i = recs.length - 1; i >= 0; i--) {
235
+ const r = recs[i];
236
+ if (r.gone)
237
+ continue;
238
+ r.gone = true;
239
+ const h = r.handle;
240
+ if (!h || typeof h.unmount !== 'function')
241
+ continue;
242
+ try {
243
+ h.unmount();
244
+ }
245
+ catch (err) {
246
+ console.error(err);
247
+ }
248
+ }
249
+ }
250
+ function syncAttrs(live, next) {
251
+ for (const a of Array.from(live.attributes))
252
+ if (!next.hasAttribute(a.name))
253
+ live.removeAttribute(a.name);
254
+ for (const a of Array.from(next.attributes)) {
255
+ if (live.getAttribute(a.name) === a.value)
256
+ continue;
257
+ if (a.namespaceURI)
258
+ live.setAttributeNS(a.namespaceURI, a.name, a.value);
259
+ else
260
+ live.setAttribute(a.name, a.value);
261
+ }
262
+ }
263
+ function insert(parent, node, ref) {
264
+ // moveBefore keeps a connected node's state (focus, iframes, animations)
265
+ // where the browser supports it.
266
+ const mv = parent.moveBefore;
267
+ if (typeof mv === 'function' && node.isConnected && parent.isConnected && node.parentNode) {
268
+ try {
269
+ mv.call(parent, node, ref);
270
+ return;
271
+ }
272
+ catch {
273
+ /* fall back to a plain insert */
274
+ }
275
+ }
276
+ parent.insertBefore(node, ref);
277
+ }
278
+ /**
279
+ * Makes the children of parent between start and end (exclusive; the whole
280
+ * child list when both are null) the nodes of want, in order: nodes not in
281
+ * want are removed (and disposed), kept nodes stay where they are, the others
282
+ * are inserted around them.
283
+ */
284
+ function place(parent, want, start, end) {
285
+ const keep = new Set(want);
286
+ const dispose = globalThis.disposeNode;
287
+ for (let n = start ? start.nextSibling : parent.firstChild; n && n !== end;) {
288
+ const next = n.nextSibling;
289
+ // A kept node that moves elsewhere stays until it is moved, so it is
290
+ // never detached on the way (moveBefore keeps its focus).
291
+ if (!keep.has(n) && !(pinned && pinned.has(n))) {
292
+ parent.removeChild(n);
293
+ if (typeof dispose === 'function')
294
+ dispose(n);
295
+ }
296
+ n = next;
297
+ }
298
+ let cur = start ? start.nextSibling : parent.firstChild;
299
+ for (const n of want) {
300
+ while (cur && cur !== end && cur !== n && pinned && pinned.has(cur) && !keep.has(cur))
301
+ cur = cur.nextSibling;
302
+ if (n === cur)
303
+ cur = cur.nextSibling;
304
+ else
305
+ insert(parent, n, cur);
306
+ }
307
+ }
308
+ /** Live nodes a swap keeps (islands and their ancestors), while it runs. */
309
+ let pinned = null;
310
+ function withPinned(p, fn) {
311
+ const prev = pinned;
312
+ const set = new Set(prev || []);
313
+ p.pairs.forEach((l) => nodesOf(l).forEach((n) => set.add(n)));
314
+ p.map.forEach((l) => set.add(l));
315
+ pinned = set;
316
+ try {
317
+ fn();
318
+ }
319
+ finally {
320
+ pinned = prev;
321
+ }
322
+ }
323
+ function pairIslands(liveList, newList, allow) {
324
+ const live = scan(liveList);
325
+ const next = scan(newList);
326
+ const pairs = new Map();
327
+ let from = 0;
328
+ for (const n of next) {
329
+ for (let j = from; j < live.length; j++) {
330
+ if (live[j].key !== n.key)
331
+ continue;
332
+ from = j + 1;
333
+ if (mounted(live[j].rec) && (!allow || allow(live[j], n)))
334
+ pairs.set(n, live[j]);
335
+ break;
336
+ }
337
+ }
338
+ const kept = new Set(pairs.values());
339
+ const byStart = new Map();
340
+ pairs.forEach((_, n) => byStart.set(n.el || n.start, n));
341
+ return { pairs, byStart, map: new Map(), carry: new Set(), gone: live.filter((l) => !kept.has(l)), live, next };
342
+ }
343
+ /** Maps the ancestors of each paired new island onto the live ones (same tags, top down). */
344
+ function mapChains(p) {
345
+ const used = new Set(p.map.values());
346
+ p.pairs.forEach((l, n) => {
347
+ let ok = true;
348
+ n.chain.forEach((ne, i) => {
349
+ p.carry.add(ne);
350
+ if (!ok)
351
+ return;
352
+ const le = l.chain[i];
353
+ const cur = p.map.get(ne);
354
+ if (cur) {
355
+ ok = cur === le;
356
+ return;
357
+ }
358
+ if (le && le.localName === ne.localName && !used.has(le)) {
359
+ p.map.set(ne, le);
360
+ used.add(le);
361
+ }
362
+ else
363
+ ok = false;
364
+ });
365
+ });
366
+ }
367
+ /**
368
+ * The nodes that take the place of the new list: kept live islands for their
369
+ * counterparts, kept live ancestors (whose children are updated) for the new
370
+ * elements mapped onto them, and the new nodes otherwise.
371
+ */
372
+ function resolve(p, list) {
373
+ const out = [];
374
+ for (let i = 0; i < list.length; i++) {
375
+ const n = list[i];
376
+ const isl = p.byStart.get(n);
377
+ if (isl) {
378
+ out.push(...nodesOf(p.pairs.get(isl)));
379
+ if (isl.end)
380
+ while (i < list.length - 1 && list[i] !== isl.end)
381
+ i++;
382
+ continue;
383
+ }
384
+ const live = p.map.get(n);
385
+ if (live) {
386
+ syncAttrs(live, n);
387
+ place(live, resolve(p, Array.from(n.childNodes)), null, null);
388
+ out.push(live);
389
+ continue;
390
+ }
391
+ if (p.carry.has(n))
392
+ place(n, resolve(p, Array.from(n.childNodes)), null, null);
393
+ out.push(n);
394
+ }
395
+ return out;
396
+ }
397
+ /** Whether every island on both sides is paired (nothing to mount or unmount). */
398
+ function allPaired(p) {
399
+ return p.gone.length === 0 && p.pairs.size === p.next.length;
400
+ }
401
+ /**
402
+ * Replaces the live children of parent between start and end (exclusive;
403
+ * all children when both are null) with newNodes (imported into the
404
+ * document, detached), keeping matched islands mounted. Returns the pending
405
+ * work of island updates (a re-render that loads more chunks first).
406
+ */
407
+ export function swapPreservingIslands(parent, start, end, newNodes, opts = {}) {
408
+ const liveNodes = [];
409
+ for (let n = start ? start.nextSibling : parent.firstChild; n && n !== end; n = n.nextSibling)
410
+ liveNodes.push(n);
411
+ const jobs = [];
412
+ const p = pairIslands(liveNodes, newNodes, opts.sameProps ? (l, n) => propsKey(l) === propsKey(n) : null);
413
+ if (p.pairs.size > 0 && allPaired(p) && maskIslands(nodesHTML(liveNodes)) === maskIslands(nodesHTML(newNodes))) {
414
+ // Only the islands differ: the rest of the live DOM is left untouched.
415
+ p.pairs.forEach((l, n) => updateIsland(l, n, jobs));
416
+ return jobs;
417
+ }
418
+ for (const g of p.gone)
419
+ unmountIslandsIn(nodesOf(g));
420
+ mapChains(p);
421
+ withPinned(p, () => place(parent, resolve(p, newNodes), start, end));
422
+ p.pairs.forEach((l, n) => updateIsland(l, n, jobs));
423
+ return jobs;
424
+ }
425
+ /** The server-children slots directly in a new island's markup, in document order. */
426
+ function slotsOf(i) {
427
+ const out = [];
428
+ let depth = 0;
429
+ let inner = [];
430
+ const see = (c) => {
431
+ if (isOpen(c))
432
+ return void depth++;
433
+ if (isClose(c))
434
+ return void depth--;
435
+ if (depth || c.nodeType !== 8)
436
+ return;
437
+ const m = SLOT_OPEN.exec(c.data);
438
+ if (!m)
439
+ return;
440
+ // A slot inside a real-React island's element belongs to that island.
441
+ const host = c.parentElement && c.parentElement.closest('krate-island');
442
+ if (host && inner.some((x) => x === host || x.contains(host)))
443
+ return;
444
+ const close = '/krate-slot:' + m[1];
445
+ const nodes = [];
446
+ let n = c.nextSibling;
447
+ for (; n && !(n.nodeType === 8 && n.data === close); n = n.nextSibling)
448
+ nodes.push(n);
449
+ if (n)
450
+ out.push({ id: m[1], nodes });
451
+ };
452
+ inner = rangeNodes(i.start, i.end).slice(1, -1);
453
+ for (const n of inner) {
454
+ if (n.nodeType === 8) {
455
+ see(n);
456
+ continue;
457
+ }
458
+ if (n.nodeType !== 1)
459
+ continue;
460
+ const w = (n.ownerDocument || document).createTreeWalker(n, 128 /* SHOW_COMMENT */);
461
+ for (let c = w.nextNode(); c; c = w.nextNode())
462
+ see(c);
463
+ }
464
+ return out;
465
+ }
466
+ /**
467
+ * Brings a kept server-children slot up to date with the new markup without
468
+ * replacing its top-level nodes (the island renders them): text and comments
469
+ * take the new data, elements keep their identity and get the new attributes
470
+ * and children, nested islands are kept. Returns false (changing nothing)
471
+ * when the top-level shapes differ.
472
+ */
473
+ function morphSlot(set, nodes, jobs) {
474
+ const p = pairIslands(set, nodes, null);
475
+ const units = (list, isl) => {
476
+ const top = new Map();
477
+ for (const x of isl)
478
+ if (x.chain.length === 0)
479
+ top.set(x.el || x.start, x);
480
+ const out = [];
481
+ for (let i = 0; i < list.length; i++) {
482
+ const x = top.get(list[i]);
483
+ out.push({ node: list[i], isl: x });
484
+ if (x && x.end)
485
+ while (i < list.length - 1 && list[i] !== x.end)
486
+ i++;
487
+ }
488
+ return out;
489
+ };
490
+ const a = units(set, p.live);
491
+ const b = units(nodes, p.next);
492
+ if (a.length !== b.length)
493
+ return false;
494
+ for (let i = 0; i < a.length; i++) {
495
+ const x = a[i];
496
+ const y = b[i];
497
+ if (x.isl || y.isl) {
498
+ if (!x.isl || !y.isl || p.pairs.get(y.isl) !== x.isl)
499
+ return false;
500
+ }
501
+ else if (x.node.nodeType !== y.node.nodeType || (x.node.nodeType === 1 && x.node.localName !== y.node.localName)) {
502
+ return false;
503
+ }
504
+ }
505
+ for (const g of p.gone)
506
+ unmountIslandsIn(nodesOf(g));
507
+ for (let i = 0; i < a.length; i++)
508
+ if (!a[i].isl && a[i].node.nodeType === 1)
509
+ p.map.set(b[i].node, a[i].node);
510
+ mapChains(p);
511
+ withPinned(p, () => {
512
+ for (let i = 0; i < a.length; i++) {
513
+ const x = a[i].node;
514
+ const y = b[i].node;
515
+ if (a[i].isl)
516
+ continue;
517
+ if (x.nodeType === 1) {
518
+ syncAttrs(x, y);
519
+ place(x, resolve(p, Array.from(y.childNodes)), null, null);
520
+ }
521
+ else if (x.data !== y.data) {
522
+ x.data = y.data;
523
+ }
524
+ }
525
+ });
526
+ p.pairs.forEach((l, n) => updateIsland(l, n, jobs));
527
+ return true;
528
+ }
529
+ function escapeComment(json) {
530
+ return json.replace(/</g, '\\u003c').replace(/>/g, '\\u003e').replace(/--/g, '-\\u002d');
531
+ }
532
+ /**
533
+ * Brings a kept island up to date with its counterpart in the new markup:
534
+ * its server-children slots follow the new server HTML (in place where they
535
+ * can), and it re-renders with the new props when they changed.
536
+ */
537
+ function updateIsland(l, n, jobs) {
538
+ const h = l.rec.handle;
539
+ const raw = parse(n.json);
540
+ if (raw === undefined)
541
+ return;
542
+ if (l.el) {
543
+ if (n.json === l.json)
544
+ return;
545
+ try {
546
+ const r = h.update(raw);
547
+ if (r && typeof r.then === 'function')
548
+ jobs.push(r.catch((e) => console.error(e)));
549
+ }
550
+ catch (err) {
551
+ console.error(err);
552
+ }
553
+ return;
554
+ }
555
+ const rename = {};
556
+ const pool = {};
557
+ let retaken = false;
558
+ const olds = h.slots || [];
559
+ const news = slotsOf(n);
560
+ const slots = [];
561
+ news.forEach((s, i) => {
562
+ const o = olds.length === news.length ? olds[i] : undefined;
563
+ const html = nodesHTML(s.nodes);
564
+ if (o) {
565
+ if (maskIslands(html) === maskIslands(o.html)) {
566
+ const q = pairIslands(o.set, s.nodes, null);
567
+ if (allPaired(q)) {
568
+ q.pairs.forEach((ll, nn) => updateIsland(ll, nn, jobs));
569
+ rename[s.id] = o.id;
570
+ slots.push(o);
571
+ return;
572
+ }
573
+ }
574
+ if (morphSlot(o.set, s.nodes, jobs)) {
575
+ rename[s.id] = o.id;
576
+ o.html = html;
577
+ slots.push(o);
578
+ return;
579
+ }
580
+ }
581
+ // The slot is rendered anew from the new nodes: the islands in its old
582
+ // content go with it.
583
+ if (o)
584
+ unmountIslandsIn(o.set);
585
+ pool[s.id] = [s.nodes];
586
+ retaken = true;
587
+ slots.push({ id: s.id, html, set: s.nodes });
588
+ });
589
+ if (olds.length !== news.length)
590
+ for (const o of olds)
591
+ unmountIslandsIn(o.set);
592
+ h.slots = slots;
593
+ const props = eachSlotRef(raw.p || {}, (id) => rename[id] || id);
594
+ const old = parse(l.json);
595
+ const changed = retaken || JSON.stringify(props) !== JSON.stringify(old ? old.p || {} : null);
596
+ raw.p = props;
597
+ l.start.data = OPEN + escapeComment(JSON.stringify(raw));
598
+ if (!changed)
599
+ return;
600
+ try {
601
+ const r = retaken ? h.update(props, pool) : h.update(props);
602
+ if (r && typeof r.then === 'function')
603
+ jobs.push(r.catch((e) => console.error(e)));
604
+ }
605
+ catch (err) {
606
+ console.error(err);
607
+ }
608
+ }
@@ -1159,8 +1159,13 @@ declare global {
1159
1159
  */
1160
1160
  basePath?: false;
1161
1161
  children?: any;
1162
- /** Prefetch the target page on hover/focus/viewport entry (default: true). */
1163
- prefetch?: boolean;
1162
+ /**
1163
+ * Prefetch the target page. Default (`true`): on hover, focus or touch.
1164
+ * `"viewport"` also prefetches once the link scrolls near the viewport
1165
+ * (at most 5 per page). `false` never prefetches. Nothing is prefetched
1166
+ * when Save-Data is on or the connection is 2g.
1167
+ */
1168
+ prefetch?: boolean | "viewport";
1164
1169
  /** Replace the current history entry instead of pushing a new one. */
1165
1170
  replace?: boolean;
1166
1171
  /** Scroll to the top on navigation (default: true). */
@@ -1,2 +1,2 @@
1
- var __krate_exports=(()=>{var r=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var h=Object.getOwnPropertyNames;var g=Object.prototype.hasOwnProperty;var v=(e,t)=>{for(var n in t)r(e,n,{get:t[n],enumerable:!0})},_=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of h(t))!g.call(e,i)&&i!==n&&r(e,i,{get:()=>t[i],enumerable:!(o=p(t,i))||o.enumerable});return e};var m=e=>_(r({},"__esModule",{value:!0}),e);var R={};v(R,{destroyRouter:()=>C,getHistoryState:()=>b,getNavigationState:()=>f,initRouter:()=>x,invalidatePrefetchCache:()=>l,krateRouter:()=>P,navigate:()=>c,reinitRouter:()=>w,subscribeNavigation:()=>d});var y="__krate_cfg__";function u(){let e=globalThis;if(e.__KRATE_CFG__)return e.__KRATE_CFG__;let t=e.document,n=t&&typeof t.getElementById=="function"?t.getElementById(y):null;if(n)try{let o=JSON.parse(n.textContent||"{}");if(o&&typeof o=="object")return e.__KRATE_CFG__=o,o}catch{}return{}}function a(){let e=u(),t=e&&e.basePath||"";return!t||t==="/"?"":"/"+t.replace(/^\/+|\/+$/g,"")}function s(e){let t=a();return t&&e.charCodeAt(0)===47&&e.charCodeAt(1)!==47?t+e:e}function f(){if(typeof location>"u")return{url:"",pathname:"/",search:"",hash:"",status:"idle"};let e=a(),t=location.pathname;return e&&(t===e||t.startsWith(e+"/"))&&(t=t.slice(e.length)||"/"),{url:location.href,pathname:t,search:location.search,hash:location.hash,status:"idle"}}function d(e){if(typeof window>"u")return()=>{};let t=()=>e(f());return window.addEventListener("hashchange",t),()=>{window.removeEventListener("hashchange",t)}}function b(){return typeof history>"u"?null:history.state}function l(e){}function x(){}function w(){}function C(){}function c(e,t={}){return typeof location>"u"||(t.replace?location.replace(s(e)):location.assign(s(e))),!0}var P={push(e,t={}){return c(e,{...t,replace:!1}),Promise.resolve()},replace(e,t={}){return c(e,{...t,replace:!0}),Promise.resolve()},back(){typeof history<"u"&&history.back()},forward(){typeof history<"u"&&history.forward()},refresh(){return typeof location<"u"&&location.reload(),Promise.resolve()},prefetch(e){return Promise.resolve()},subscribe:d,getState:f,invalidate:l};typeof window<"u"&&window.addEventListener("krate:revalidate",()=>{typeof location<"u"&&location.reload()});return m(R);})();
1
+ var __krate_exports=(()=>{var r=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var h=Object.getOwnPropertyNames;var g=Object.prototype.hasOwnProperty;var v=(e,t)=>{for(var n in t)r(e,n,{get:t[n],enumerable:!0})},_=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of h(t))!g.call(e,i)&&i!==n&&r(e,i,{get:()=>t[i],enumerable:!(o=p(t,i))||o.enumerable});return e};var m=e=>_(r({},"__esModule",{value:!0}),e);var R={};v(R,{destroyRouter:()=>C,getHistoryState:()=>b,getNavigationState:()=>f,initRouter:()=>w,invalidatePrefetchCache:()=>l,krateRouter:()=>P,navigate:()=>c,reinitRouter:()=>x,subscribeNavigation:()=>d});var y="__krate_cfg__";function u(){let e=globalThis;if(e.__KRATE_CFG__)return e.__KRATE_CFG__;let t=e.document,n=t&&typeof t.getElementById=="function"?t.getElementById(y):null;if(n)try{let o=JSON.parse(n.textContent||"{}");if(o&&typeof o=="object")return e.__KRATE_CFG__=o,o}catch{}return{}}function a(){let e=u(),t=e&&e.basePath||"";return!t||t==="/"?"":"/"+t.replace(/^\/+|\/+$/g,"")}function s(e){let t=a();return t&&e.charCodeAt(0)===47&&e.charCodeAt(1)!==47?t+e:e}function f(){if(typeof location>"u")return{url:"",pathname:"/",search:"",hash:"",status:"idle"};let e=a(),t=location.pathname;return e&&(t===e||t.startsWith(e+"/"))&&(t=t.slice(e.length)||"/"),{url:location.href,pathname:t,search:location.search,hash:location.hash,status:"idle"}}function d(e){if(typeof window>"u")return()=>{};let t=()=>e(f());return window.addEventListener("hashchange",t),()=>{window.removeEventListener("hashchange",t)}}function b(){return typeof history>"u"?null:history.state}function l(e){}function w(){}function x(){}function C(){}function c(e,t={}){return typeof location>"u"||(t.replace?location.replace(s(e)):location.assign(s(e))),!0}var P={push(e,t={}){return c(e,{...t,replace:!1}),Promise.resolve()},replace(e,t={}){return c(e,{...t,replace:!0}),Promise.resolve()},back(){typeof history<"u"&&history.back()},forward(){typeof history<"u"&&history.forward()},refresh(){return typeof location<"u"&&location.reload(),Promise.resolve()},prefetch(e){return Promise.resolve()},subscribe:d,getState:f,invalidate:l};typeof window<"u"&&window.addEventListener("krate:revalidate",()=>{typeof location<"u"&&location.reload()});return m(R);})();
2
2
  for (const k in __krate_exports) { window[k] = __krate_exports[k]; }