@krate/runtime 0.1.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/LICENSE +201 -0
- package/README.md +3 -0
- package/dist/context.d.ts +11 -0
- package/dist/context.js +56 -0
- package/dist/css-modules.d.ts +7 -0
- package/dist/css-modules.js +1 -0
- package/dist/dom.d.ts +29 -0
- package/dist/dom.js +212 -0
- package/dist/error-overlay.d.ts +6 -0
- package/dist/error-overlay.js +113 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +8 -0
- package/dist/jsx-dev-runtime.d.ts +8 -0
- package/dist/jsx-dev-runtime.js +5 -0
- package/dist/jsx-runtime.d.ts +8 -0
- package/dist/jsx-runtime.js +29 -0
- package/dist/jsx-types.d.ts +1023 -0
- package/dist/jsx-types.js +1 -0
- package/dist/krate-hydrate.js +2 -0
- package/dist/krate-runtime.js +2 -0
- package/dist/reconcile.d.ts +35 -0
- package/dist/reconcile.js +312 -0
- package/dist/resource.d.ts +13 -0
- package/dist/resource.js +73 -0
- package/dist/router.d.ts +3 -0
- package/dist/router.js +519 -0
- package/dist/server-jsx-runtime.d.ts +1 -0
- package/dist/server-jsx-runtime.js +4 -0
- package/dist/server-renderer.d.ts +1 -0
- package/dist/server-renderer.js +468 -0
- package/dist/server.d.ts +87 -0
- package/dist/server.js +214 -0
- package/dist/signal.d.ts +9 -0
- package/dist/signal.js +178 -0
- package/package.json +66 -0
package/dist/router.js
ADDED
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
import { reconcileTrees } from './reconcile.js';
|
|
2
|
+
let initialized = false;
|
|
3
|
+
let pendingAbort;
|
|
4
|
+
let routerCleanup = null;
|
|
5
|
+
/** Cache for prefetched page HTML keyed by URL (bounded LRU). */
|
|
6
|
+
const prefetchCache = new Map();
|
|
7
|
+
const PREFETCH_CACHE_MAX = 30;
|
|
8
|
+
/** Saved scroll positions for back/forward navigation restoration (bounded). */
|
|
9
|
+
const scrollPositions = new Map();
|
|
10
|
+
const SCROLL_HISTORY_MAX = 50;
|
|
11
|
+
/**
|
|
12
|
+
* The shared runtime chunk is loaded once and must persist across SPA
|
|
13
|
+
* navigations. Page-specific hydration scripts, in contrast, must be
|
|
14
|
+
* re-executed on every visit (against the freshly reconciled DOM), so they are
|
|
15
|
+
* removed before loading a new page and re-appended.
|
|
16
|
+
*/
|
|
17
|
+
const RUNTIME_CHUNK_RE = /\/chunks\/runtime\.[^/]+\.js$/;
|
|
18
|
+
export function initRouter() {
|
|
19
|
+
if (typeof document === 'undefined')
|
|
20
|
+
return;
|
|
21
|
+
// Fix 4.4: Guard against multiple initRouter calls
|
|
22
|
+
if (initialized)
|
|
23
|
+
return;
|
|
24
|
+
initialized = true;
|
|
25
|
+
const CONTENT_SEL = '.docs-content, main, #root';
|
|
26
|
+
const TRANSITION_MS = 150;
|
|
27
|
+
const PREFETCH_DELAY = 200;
|
|
28
|
+
let notFoundHTML = null;
|
|
29
|
+
let errorHTML = null;
|
|
30
|
+
let prefetchTimers = new Map();
|
|
31
|
+
let prefetchObserver = null;
|
|
32
|
+
function findContentRoot(doc) {
|
|
33
|
+
for (const sel of CONTENT_SEL.split(', ')) {
|
|
34
|
+
const el = doc.querySelector(sel.trim());
|
|
35
|
+
if (el)
|
|
36
|
+
return el;
|
|
37
|
+
}
|
|
38
|
+
return doc.body;
|
|
39
|
+
}
|
|
40
|
+
function isLocalLink(a) {
|
|
41
|
+
if (a.hasAttribute('data-krate-external'))
|
|
42
|
+
return false;
|
|
43
|
+
const href = a.getAttribute('href');
|
|
44
|
+
if (!href)
|
|
45
|
+
return false;
|
|
46
|
+
if (href.startsWith('#') || href.startsWith('javascript:'))
|
|
47
|
+
return false;
|
|
48
|
+
if (a.origin !== location.origin)
|
|
49
|
+
return false;
|
|
50
|
+
if (a.target === '_blank')
|
|
51
|
+
return false;
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
function prefetchPage(url) {
|
|
55
|
+
if (prefetchCache.has(url))
|
|
56
|
+
return;
|
|
57
|
+
fetch(url, { headers: { Accept: 'text/html' } })
|
|
58
|
+
.then((res) => {
|
|
59
|
+
if (res.ok)
|
|
60
|
+
return res.text();
|
|
61
|
+
return null;
|
|
62
|
+
})
|
|
63
|
+
.then((html) => {
|
|
64
|
+
if (html) {
|
|
65
|
+
// Bounded LRU: evict the oldest entry when the cache is full.
|
|
66
|
+
if (prefetchCache.size >= PREFETCH_CACHE_MAX) {
|
|
67
|
+
const oldest = prefetchCache.keys().next().value;
|
|
68
|
+
if (oldest !== undefined)
|
|
69
|
+
prefetchCache.delete(oldest);
|
|
70
|
+
}
|
|
71
|
+
prefetchCache.set(url, html);
|
|
72
|
+
}
|
|
73
|
+
})
|
|
74
|
+
.catch(() => { });
|
|
75
|
+
}
|
|
76
|
+
function setupPrefetch() {
|
|
77
|
+
// Hover/focus prefetching
|
|
78
|
+
document.addEventListener('mouseover', prefetchHoverHandler);
|
|
79
|
+
document.addEventListener('focusin', prefetchHoverHandler);
|
|
80
|
+
// Viewport prefetching via IntersectionObserver
|
|
81
|
+
if (typeof IntersectionObserver !== 'undefined') {
|
|
82
|
+
prefetchObserver = new IntersectionObserver((entries) => {
|
|
83
|
+
for (const entry of entries) {
|
|
84
|
+
if (entry.isIntersecting) {
|
|
85
|
+
const a = entry.target;
|
|
86
|
+
const href = a.getAttribute('href');
|
|
87
|
+
if (href && isLocalLink(a)) {
|
|
88
|
+
const url = new URL(href, location.origin).href;
|
|
89
|
+
prefetchPage(url);
|
|
90
|
+
}
|
|
91
|
+
prefetchObserver.unobserve(entry.target);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}, { rootMargin: '200px' });
|
|
95
|
+
observePrefetchLinks();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function observePrefetchLinks() {
|
|
99
|
+
if (!prefetchObserver)
|
|
100
|
+
return;
|
|
101
|
+
document.querySelectorAll('a[data-prefetch]').forEach((a) => {
|
|
102
|
+
prefetchObserver.observe(a);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
function cleanupPrefetch() {
|
|
106
|
+
document.removeEventListener('mouseover', prefetchHoverHandler);
|
|
107
|
+
document.removeEventListener('focusin', prefetchHoverHandler);
|
|
108
|
+
for (const timer of prefetchTimers.values()) {
|
|
109
|
+
clearTimeout(timer);
|
|
110
|
+
}
|
|
111
|
+
prefetchTimers.clear();
|
|
112
|
+
if (prefetchObserver) {
|
|
113
|
+
prefetchObserver.disconnect();
|
|
114
|
+
prefetchObserver = null;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function prefetchHoverHandler(e) {
|
|
118
|
+
const a = e.target.closest('a[data-prefetch]');
|
|
119
|
+
if (!a || !isLocalLink(a))
|
|
120
|
+
return;
|
|
121
|
+
const href = a.getAttribute('href');
|
|
122
|
+
if (!href)
|
|
123
|
+
return;
|
|
124
|
+
const url = new URL(href, location.origin).href;
|
|
125
|
+
if (prefetchCache.has(url))
|
|
126
|
+
return;
|
|
127
|
+
// Debounce: start prefetch after a short delay
|
|
128
|
+
const existing = prefetchTimers.get(a);
|
|
129
|
+
if (existing)
|
|
130
|
+
clearTimeout(existing);
|
|
131
|
+
const timer = setTimeout(() => {
|
|
132
|
+
prefetchTimers.delete(a);
|
|
133
|
+
prefetchPage(url);
|
|
134
|
+
}, PREFETCH_DELAY);
|
|
135
|
+
prefetchTimers.set(a, timer);
|
|
136
|
+
}
|
|
137
|
+
function stripHandlerProps(root) {
|
|
138
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
|
139
|
+
let node = walker.nextNode();
|
|
140
|
+
while (node) {
|
|
141
|
+
const el = node;
|
|
142
|
+
for (const key of Object.keys(el)) {
|
|
143
|
+
if (key.indexOf('__krate_') === 0) {
|
|
144
|
+
delete el[key];
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
node = walker.nextNode();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
// Begin the visual transition (fade out + optional loading overlay) while a
|
|
151
|
+
// page fetch is in flight, so navigation doesn't feel artificially delayed.
|
|
152
|
+
let loadingOverlay = null;
|
|
153
|
+
function beginTransition() {
|
|
154
|
+
const oldContent = findContentRoot(document);
|
|
155
|
+
const loadingTemplate = document.querySelector('template[data-krate-loading]');
|
|
156
|
+
if (loadingTemplate && loadingTemplate.content && oldContent) {
|
|
157
|
+
// Non-destructive loading UI: overlay the template without wiping the
|
|
158
|
+
// live DOM, so reconciliation still preserves node state.
|
|
159
|
+
loadingOverlay = loadingTemplate.content.cloneNode(true);
|
|
160
|
+
document.body.appendChild(loadingOverlay);
|
|
161
|
+
}
|
|
162
|
+
if (oldContent) {
|
|
163
|
+
oldContent.style.transition = `opacity ${TRANSITION_MS}ms ease`;
|
|
164
|
+
oldContent.style.opacity = '0';
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
function endTransition() {
|
|
168
|
+
if (loadingOverlay && loadingOverlay.parentNode) {
|
|
169
|
+
loadingOverlay.parentNode.removeChild(loadingOverlay);
|
|
170
|
+
}
|
|
171
|
+
loadingOverlay = null;
|
|
172
|
+
const oldContent = findContentRoot(document);
|
|
173
|
+
if (oldContent) {
|
|
174
|
+
oldContent.style.opacity = '1';
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function swapContent(html, url, scripts, opts = {}) {
|
|
178
|
+
const { replace = false, scroll = true, restoreScroll = false } = opts;
|
|
179
|
+
const parser = new DOMParser();
|
|
180
|
+
const doc = parser.parseFromString(html, 'text/html');
|
|
181
|
+
const newContent = findContentRoot(doc);
|
|
182
|
+
const oldContent = findContentRoot(document);
|
|
183
|
+
if (!newContent || !oldContent) {
|
|
184
|
+
location.href = url;
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const newTitle = doc.querySelector('title');
|
|
188
|
+
if (newTitle)
|
|
189
|
+
document.title = newTitle.textContent || '';
|
|
190
|
+
// Dispose all active effects before replacing DOM
|
|
191
|
+
if (typeof globalThis.disposeAll === 'function') {
|
|
192
|
+
globalThis.disposeAll();
|
|
193
|
+
}
|
|
194
|
+
// Strip stale hydration handler props so kept nodes don't retain
|
|
195
|
+
// closures bound to disposed signals; hydration re-sets them below.
|
|
196
|
+
stripHandlerProps(oldContent);
|
|
197
|
+
// Diff the live content against the parsed new page instead of wiping
|
|
198
|
+
// it via innerHTML, so unchanged nodes keep their state (focus, scroll,
|
|
199
|
+
// media playback, CSS animations) across navigation.
|
|
200
|
+
reconcileTrees(oldContent, newContent);
|
|
201
|
+
// Diff and remove old stylesheets not present in new page
|
|
202
|
+
const newStyles = doc.querySelectorAll('link[rel="stylesheet"]');
|
|
203
|
+
const newHrefs = new Set();
|
|
204
|
+
newStyles.forEach((s) => {
|
|
205
|
+
const href = s.getAttribute('href');
|
|
206
|
+
if (href)
|
|
207
|
+
newHrefs.add(href);
|
|
208
|
+
});
|
|
209
|
+
document.querySelectorAll('link[rel="stylesheet"]').forEach((l) => {
|
|
210
|
+
const href = l.getAttribute('href');
|
|
211
|
+
if (href && !newHrefs.has(href)) {
|
|
212
|
+
l.remove();
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
const existingHrefs = new Set();
|
|
216
|
+
document.querySelectorAll('link[rel="stylesheet"]').forEach((l) => {
|
|
217
|
+
existingHrefs.add(l.href);
|
|
218
|
+
});
|
|
219
|
+
newStyles.forEach((s) => {
|
|
220
|
+
const href = s.getAttribute('href');
|
|
221
|
+
if (href && !existingHrefs.has(href)) {
|
|
222
|
+
const link = document.createElement('link');
|
|
223
|
+
link.rel = 'stylesheet';
|
|
224
|
+
link.href = href;
|
|
225
|
+
document.head.appendChild(link);
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
// Load new scripts, then rehydrate
|
|
229
|
+
loadScripts(scripts, url).then(() => {
|
|
230
|
+
rehydrate();
|
|
231
|
+
// Re-observe new prefetch links after hydration
|
|
232
|
+
observePrefetchLinks();
|
|
233
|
+
});
|
|
234
|
+
endTransition();
|
|
235
|
+
// History: replace keeps the current entry; push appends one (unless we
|
|
236
|
+
// are restoring from a back/forward navigation).
|
|
237
|
+
const prevUrl = location.pathname + location.search;
|
|
238
|
+
if (replace) {
|
|
239
|
+
history.replaceState({}, '', url);
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
if (!restoreScroll) {
|
|
243
|
+
if (scrollPositions.size >= SCROLL_HISTORY_MAX) {
|
|
244
|
+
const oldest = scrollPositions.keys().next().value;
|
|
245
|
+
if (oldest !== undefined)
|
|
246
|
+
scrollPositions.delete(oldest);
|
|
247
|
+
}
|
|
248
|
+
scrollPositions.set(prevUrl, window.scrollY);
|
|
249
|
+
}
|
|
250
|
+
history.pushState({}, '', url);
|
|
251
|
+
}
|
|
252
|
+
applyScroll(url, { restoreScroll, scroll });
|
|
253
|
+
updateActiveLinks();
|
|
254
|
+
window.dispatchEvent(new CustomEvent('krate:navigate', { detail: { url } }));
|
|
255
|
+
}
|
|
256
|
+
function handleHashScroll(url) {
|
|
257
|
+
const hashIdx = url.indexOf('#');
|
|
258
|
+
if (hashIdx < 0 || hashIdx === url.length - 1)
|
|
259
|
+
return false;
|
|
260
|
+
const hash = url.slice(hashIdx);
|
|
261
|
+
try {
|
|
262
|
+
const el = document.querySelector(hash);
|
|
263
|
+
if (el) {
|
|
264
|
+
el.scrollIntoView({ behavior: 'smooth', block: 'start' });
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
catch {
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
function applyScroll(url, opts) {
|
|
274
|
+
if (opts.restoreScroll) {
|
|
275
|
+
const saved = scrollPositions.get(url);
|
|
276
|
+
if (saved !== undefined) {
|
|
277
|
+
window.scrollTo(0, saved);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
if (handleHashScroll(url))
|
|
282
|
+
return;
|
|
283
|
+
if (opts.scroll)
|
|
284
|
+
window.scrollTo(0, 0);
|
|
285
|
+
}
|
|
286
|
+
// Mark the link matching the current route with aria-current="page".
|
|
287
|
+
function updateActiveLinks() {
|
|
288
|
+
const path = location.pathname;
|
|
289
|
+
document.querySelectorAll('a[data-krate-link]').forEach((a) => {
|
|
290
|
+
const href = a.getAttribute('href');
|
|
291
|
+
if (!href) {
|
|
292
|
+
a.removeAttribute('aria-current');
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
let target = href;
|
|
296
|
+
try {
|
|
297
|
+
target = new URL(href, location.origin).pathname;
|
|
298
|
+
}
|
|
299
|
+
catch {
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
const active = target === path ||
|
|
303
|
+
(path === '/' && (target === '' || target === '/')) ||
|
|
304
|
+
(target !== '/' && target.length > 1 && path.startsWith(target));
|
|
305
|
+
if (active)
|
|
306
|
+
a.setAttribute('aria-current', 'page');
|
|
307
|
+
else
|
|
308
|
+
a.removeAttribute('aria-current');
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
function loadScripts(scripts, baseUrl) {
|
|
312
|
+
if (scripts.length === 0)
|
|
313
|
+
return Promise.resolve();
|
|
314
|
+
// Load all scripts in parallel (no serialization latency); hydration runs
|
|
315
|
+
// once every script is loaded.
|
|
316
|
+
return Promise.all(scripts.map((script) => {
|
|
317
|
+
const src = script.getAttribute('src');
|
|
318
|
+
return src ? loadScript(src, baseUrl) : Promise.resolve();
|
|
319
|
+
})).then(() => undefined);
|
|
320
|
+
}
|
|
321
|
+
function loadScript(src, baseUrl) {
|
|
322
|
+
return new Promise((resolve) => {
|
|
323
|
+
const s = document.createElement('script');
|
|
324
|
+
const resolved = resolveSrc(src, baseUrl);
|
|
325
|
+
s.src = resolved;
|
|
326
|
+
// Only page scripts are disposable across navigations — the shared
|
|
327
|
+
// runtime chunk stays in place.
|
|
328
|
+
if (!RUNTIME_CHUNK_RE.test(resolved))
|
|
329
|
+
s.setAttribute('data-krate-spa', '');
|
|
330
|
+
s.onload = () => resolve();
|
|
331
|
+
s.onerror = () => resolve();
|
|
332
|
+
document.body.appendChild(s);
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
// Resolve a script URL against the navigated-to page (not the current one),
|
|
336
|
+
// so relative srcs like "index.a1b2c3.js" load from the new page's directory.
|
|
337
|
+
function resolveSrc(src, baseUrl) {
|
|
338
|
+
if (/^(https?:)?\/\//.test(src) || src.startsWith('/')) {
|
|
339
|
+
return new URL(src, baseUrl).href;
|
|
340
|
+
}
|
|
341
|
+
// Relative path: resolve against the base page's directory, accounting for
|
|
342
|
+
// both "/about" and "/about/" style targets.
|
|
343
|
+
let base = baseUrl;
|
|
344
|
+
if (!base.endsWith('/')) {
|
|
345
|
+
const idx = base.lastIndexOf('/');
|
|
346
|
+
base = idx > 0 ? base.slice(0, idx + 1) : '/';
|
|
347
|
+
}
|
|
348
|
+
return new URL(src, base).href;
|
|
349
|
+
}
|
|
350
|
+
function rehydrate() {
|
|
351
|
+
if (typeof globalThis.__krate_hydrate === 'function') {
|
|
352
|
+
globalThis.__krate_hydrate();
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
function replacePage(html, url) {
|
|
356
|
+
// Dispose all active effects before full page replacement
|
|
357
|
+
if (typeof globalThis.disposeAll === 'function') {
|
|
358
|
+
globalThis.disposeAll();
|
|
359
|
+
}
|
|
360
|
+
history.pushState({}, '', url);
|
|
361
|
+
document.open();
|
|
362
|
+
document.write(html);
|
|
363
|
+
document.close();
|
|
364
|
+
}
|
|
365
|
+
function fetchNotFoundPage() {
|
|
366
|
+
if (notFoundHTML !== null)
|
|
367
|
+
return Promise.resolve(notFoundHTML);
|
|
368
|
+
return fetch('/404.html', { headers: { Accept: 'text/html' } })
|
|
369
|
+
.then((res) => {
|
|
370
|
+
if (!res.ok)
|
|
371
|
+
return null;
|
|
372
|
+
return res.text();
|
|
373
|
+
})
|
|
374
|
+
.then((html) => {
|
|
375
|
+
notFoundHTML = html;
|
|
376
|
+
return html;
|
|
377
|
+
})
|
|
378
|
+
.catch(() => null);
|
|
379
|
+
}
|
|
380
|
+
function fetchErrorPage() {
|
|
381
|
+
if (errorHTML !== null)
|
|
382
|
+
return Promise.resolve(errorHTML);
|
|
383
|
+
return fetch('/500.html', { headers: { Accept: 'text/html' } })
|
|
384
|
+
.then((res) => {
|
|
385
|
+
if (!res.ok)
|
|
386
|
+
return null;
|
|
387
|
+
return res.text();
|
|
388
|
+
})
|
|
389
|
+
.then((html) => {
|
|
390
|
+
errorHTML = html;
|
|
391
|
+
return html;
|
|
392
|
+
})
|
|
393
|
+
.catch(() => null);
|
|
394
|
+
}
|
|
395
|
+
function extractScripts(doc, baseUrl) {
|
|
396
|
+
// Remove previously SPA-loaded page scripts. They must re-execute on every
|
|
397
|
+
// visit (the reconciled DOM is new), and they'd otherwise accumulate.
|
|
398
|
+
document.querySelectorAll('script[data-krate-spa]').forEach((s) => s.remove());
|
|
399
|
+
const scripts = [];
|
|
400
|
+
const existingSrcs = new Set();
|
|
401
|
+
document.querySelectorAll('script[src]').forEach((s) => {
|
|
402
|
+
existingSrcs.add(s.src);
|
|
403
|
+
});
|
|
404
|
+
doc.querySelectorAll('script[src]').forEach((s) => {
|
|
405
|
+
const src = s.getAttribute('src');
|
|
406
|
+
if (src && !existingSrcs.has(resolveSrc(src, baseUrl))) {
|
|
407
|
+
scripts.push(s);
|
|
408
|
+
}
|
|
409
|
+
});
|
|
410
|
+
return scripts;
|
|
411
|
+
}
|
|
412
|
+
function fetchAndSwap(url, opts = {}) {
|
|
413
|
+
// Check prefetch cache first — already-loaded content swaps instantly.
|
|
414
|
+
const cached = prefetchCache.get(url);
|
|
415
|
+
if (cached) {
|
|
416
|
+
const parser = new DOMParser();
|
|
417
|
+
const doc = parser.parseFromString(cached, 'text/html');
|
|
418
|
+
swapContent(cached, url, extractScripts(doc, url), opts);
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
// Begin the fade/loading transition while the fetch is in flight.
|
|
422
|
+
beginTransition();
|
|
423
|
+
// Fix 4.6: Abort any in-flight navigation
|
|
424
|
+
if (pendingAbort) {
|
|
425
|
+
pendingAbort.abort();
|
|
426
|
+
}
|
|
427
|
+
pendingAbort = new AbortController();
|
|
428
|
+
const signal = pendingAbort.signal;
|
|
429
|
+
fetch(url, {
|
|
430
|
+
headers: { Accept: 'text/html' },
|
|
431
|
+
signal,
|
|
432
|
+
})
|
|
433
|
+
.then((res) => {
|
|
434
|
+
if (!res.ok) {
|
|
435
|
+
const page = res.status >= 500 ? fetchErrorPage() : fetchNotFoundPage();
|
|
436
|
+
return page.then((html) => {
|
|
437
|
+
if (html) {
|
|
438
|
+
// Error pages contain full layout — full page replacement
|
|
439
|
+
replacePage(html, url);
|
|
440
|
+
}
|
|
441
|
+
else {
|
|
442
|
+
location.href = url;
|
|
443
|
+
}
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
return res.text().then((html) => {
|
|
447
|
+
const parser = new DOMParser();
|
|
448
|
+
const doc = parser.parseFromString(html, 'text/html');
|
|
449
|
+
swapContent(html, url, extractScripts(doc, url), opts);
|
|
450
|
+
});
|
|
451
|
+
})
|
|
452
|
+
.catch((err) => {
|
|
453
|
+
// Ignore aborted requests; otherwise abort the transition so the page
|
|
454
|
+
// fades back in.
|
|
455
|
+
if (err?.name !== 'AbortError') {
|
|
456
|
+
endTransition();
|
|
457
|
+
location.href = url;
|
|
458
|
+
}
|
|
459
|
+
})
|
|
460
|
+
.finally(() => {
|
|
461
|
+
if (pendingAbort && !signal.aborted) {
|
|
462
|
+
pendingAbort = undefined;
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
const clickHandler = (e) => {
|
|
467
|
+
// Never intercept modified clicks (cmd/ctrl/shift/alt or middle button) —
|
|
468
|
+
// they should open in a new tab or trigger browser behavior.
|
|
469
|
+
if (e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
|
|
470
|
+
return;
|
|
471
|
+
const a = e.target.closest('a[data-krate-link]');
|
|
472
|
+
if (!a || !isLocalLink(a))
|
|
473
|
+
return;
|
|
474
|
+
e.preventDefault();
|
|
475
|
+
const href = a.getAttribute('href');
|
|
476
|
+
if (!href)
|
|
477
|
+
return;
|
|
478
|
+
const url = new URL(href, location.origin).href;
|
|
479
|
+
const replace = a.hasAttribute('data-krate-replace');
|
|
480
|
+
const scroll = a.getAttribute('data-krate-scroll') !== 'false';
|
|
481
|
+
// Same-URL navigation (e.g. hash change): just update history + scroll.
|
|
482
|
+
if (href === location.pathname + location.search + location.hash) {
|
|
483
|
+
if (replace)
|
|
484
|
+
history.replaceState({}, '', href);
|
|
485
|
+
applyScroll(url, { scroll });
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
fetchAndSwap(url, { replace, scroll });
|
|
489
|
+
};
|
|
490
|
+
const popstateHandler = () => {
|
|
491
|
+
fetchAndSwap(location.href, { restoreScroll: true });
|
|
492
|
+
};
|
|
493
|
+
document.addEventListener('click', clickHandler);
|
|
494
|
+
window.addEventListener('popstate', popstateHandler);
|
|
495
|
+
setupPrefetch();
|
|
496
|
+
updateActiveLinks();
|
|
497
|
+
// Mark the initially-loaded page scripts as disposable so they're removed and
|
|
498
|
+
// re-executed against the fresh DOM when the user navigates back to this
|
|
499
|
+
// page. The shared runtime chunk is the exception and persists.
|
|
500
|
+
document.querySelectorAll('script[src]').forEach((s) => {
|
|
501
|
+
if (!RUNTIME_CHUNK_RE.test(s.src)) {
|
|
502
|
+
s.setAttribute('data-krate-spa', '');
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
routerCleanup = () => {
|
|
506
|
+
document.removeEventListener('click', clickHandler);
|
|
507
|
+
window.removeEventListener('popstate', popstateHandler);
|
|
508
|
+
cleanupPrefetch();
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
/** Dispose old effects, re-register router listeners for new page. */
|
|
512
|
+
export function reinitRouter() {
|
|
513
|
+
if (routerCleanup) {
|
|
514
|
+
routerCleanup();
|
|
515
|
+
routerCleanup = null;
|
|
516
|
+
}
|
|
517
|
+
initialized = false;
|
|
518
|
+
initRouter();
|
|
519
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Fragment, jsx, jsxs } from './server.js';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// @krate/runtime/server/jsx-runtime — Server-compatible JSX runtime.
|
|
2
|
+
// The functions are re-exported from ./server.js (single source of truth) so
|
|
3
|
+
// the JSX transform and its types never drift between the two server subpaths.
|
|
4
|
+
export { Fragment, jsx, jsxs } from './server.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|