@rangojs/router 0.0.0-experimental.20 → 0.0.0-experimental.20dbba0c
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/AGENTS.md +4 -0
- package/README.md +172 -50
- package/dist/bin/rango.js +138 -50
- package/dist/vite/index.js +1160 -508
- package/dist/vite/index.js.bak +5448 -0
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +17 -16
- package/skills/breadcrumbs/SKILL.md +252 -0
- package/skills/cache-guide/SKILL.md +32 -0
- package/skills/caching/SKILL.md +49 -8
- package/skills/document-cache/SKILL.md +2 -2
- package/skills/handler-use/SKILL.md +362 -0
- package/skills/hooks/SKILL.md +61 -51
- package/skills/host-router/SKILL.md +218 -0
- package/skills/intercept/SKILL.md +20 -0
- package/skills/layout/SKILL.md +22 -0
- package/skills/links/SKILL.md +91 -17
- package/skills/loader/SKILL.md +107 -24
- package/skills/middleware/SKILL.md +34 -3
- package/skills/migrate-nextjs/SKILL.md +560 -0
- package/skills/migrate-react-router/SKILL.md +765 -0
- package/skills/parallel/SKILL.md +185 -0
- package/skills/prerender/SKILL.md +112 -70
- package/skills/rango/SKILL.md +24 -23
- package/skills/response-routes/SKILL.md +8 -0
- package/skills/route/SKILL.md +58 -4
- package/skills/router-setup/SKILL.md +95 -5
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/typesafety/SKILL.md +38 -24
- package/src/__internal.ts +92 -0
- package/src/browser/app-shell.ts +52 -0
- package/src/browser/app-version.ts +14 -0
- package/src/browser/event-controller.ts +5 -0
- package/src/browser/link-interceptor.ts +4 -0
- package/src/browser/navigation-bridge.ts +175 -17
- package/src/browser/navigation-client.ts +177 -44
- package/src/browser/navigation-store.ts +68 -9
- package/src/browser/navigation-transaction.ts +11 -9
- package/src/browser/partial-update.ts +113 -17
- package/src/browser/prefetch/cache.ts +275 -28
- package/src/browser/prefetch/fetch.ts +191 -46
- package/src/browser/prefetch/policy.ts +6 -0
- package/src/browser/prefetch/queue.ts +123 -20
- package/src/browser/prefetch/resource-ready.ts +77 -0
- package/src/browser/rango-state.ts +53 -13
- package/src/browser/react/Link.tsx +98 -14
- package/src/browser/react/NavigationProvider.tsx +89 -14
- package/src/browser/react/context.ts +7 -2
- package/src/browser/react/use-handle.ts +9 -58
- package/src/browser/react/use-navigation.ts +22 -2
- package/src/browser/react/use-params.ts +11 -1
- package/src/browser/react/use-router.ts +29 -9
- package/src/browser/rsc-router.tsx +177 -66
- package/src/browser/scroll-restoration.ts +41 -42
- package/src/browser/segment-reconciler.ts +36 -9
- package/src/browser/server-action-bridge.ts +8 -6
- package/src/browser/types.ts +73 -5
- package/src/build/generate-manifest.ts +6 -6
- package/src/build/generate-route-types.ts +3 -0
- package/src/build/route-trie.ts +67 -25
- package/src/build/route-types/include-resolution.ts +8 -1
- package/src/build/route-types/router-processing.ts +223 -74
- package/src/build/route-types/scan-filter.ts +8 -1
- package/src/cache/cache-runtime.ts +15 -11
- package/src/cache/cache-scope.ts +48 -7
- package/src/cache/cf/cf-cache-store.ts +455 -15
- package/src/cache/cf/index.ts +5 -1
- package/src/cache/document-cache.ts +17 -7
- package/src/cache/index.ts +1 -0
- package/src/cache/taint.ts +55 -0
- package/src/client.rsc.tsx +2 -1
- package/src/client.tsx +85 -276
- package/src/context-var.ts +72 -2
- package/src/debug.ts +2 -2
- package/src/handle.ts +40 -0
- package/src/handles/breadcrumbs.ts +66 -0
- package/src/handles/index.ts +1 -0
- package/src/host/index.ts +0 -3
- package/src/index.rsc.ts +9 -36
- package/src/index.ts +79 -70
- package/src/outlet-context.ts +1 -1
- package/src/prerender/store.ts +57 -15
- package/src/prerender.ts +138 -77
- package/src/response-utils.ts +28 -0
- package/src/reverse.ts +27 -2
- package/src/route-definition/dsl-helpers.ts +240 -40
- package/src/route-definition/helpers-types.ts +67 -19
- package/src/route-definition/index.ts +3 -3
- package/src/route-definition/redirect.ts +11 -3
- package/src/route-definition/resolve-handler-use.ts +155 -0
- package/src/route-map-builder.ts +7 -1
- package/src/route-types.ts +18 -0
- package/src/router/content-negotiation.ts +100 -1
- package/src/router/find-match.ts +4 -2
- package/src/router/handler-context.ts +129 -26
- package/src/router/intercept-resolution.ts +11 -4
- package/src/router/lazy-includes.ts +10 -7
- package/src/router/loader-resolution.ts +160 -22
- package/src/router/logging.ts +5 -2
- package/src/router/manifest.ts +31 -16
- package/src/router/match-api.ts +128 -193
- package/src/router/match-middleware/background-revalidation.ts +30 -2
- package/src/router/match-middleware/cache-lookup.ts +94 -17
- package/src/router/match-middleware/cache-store.ts +53 -10
- package/src/router/match-middleware/intercept-resolution.ts +9 -7
- package/src/router/match-middleware/segment-resolution.ts +61 -5
- package/src/router/match-result.ts +103 -18
- package/src/router/metrics.ts +238 -13
- package/src/router/middleware-types.ts +48 -27
- package/src/router/middleware.ts +201 -86
- package/src/router/navigation-snapshot.ts +182 -0
- package/src/router/pattern-matching.ts +77 -11
- package/src/router/prerender-match.ts +114 -10
- package/src/router/preview-match.ts +30 -102
- package/src/router/request-classification.ts +310 -0
- package/src/router/revalidation.ts +27 -7
- package/src/router/route-snapshot.ts +245 -0
- package/src/router/router-context.ts +6 -1
- package/src/router/router-interfaces.ts +50 -5
- package/src/router/router-options.ts +50 -19
- package/src/router/segment-resolution/fresh.ts +215 -19
- package/src/router/segment-resolution/helpers.ts +30 -25
- package/src/router/segment-resolution/loader-cache.ts +1 -0
- package/src/router/segment-resolution/revalidation.ts +454 -301
- package/src/router/segment-wrappers.ts +2 -0
- package/src/router/trie-matching.ts +30 -6
- package/src/router/types.ts +1 -0
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +89 -17
- package/src/rsc/handler.ts +563 -364
- package/src/rsc/helpers.ts +69 -41
- package/src/rsc/index.ts +0 -20
- package/src/rsc/loader-fetch.ts +23 -3
- package/src/rsc/manifest-init.ts +5 -1
- package/src/rsc/progressive-enhancement.ts +37 -10
- package/src/rsc/response-route-handler.ts +14 -1
- package/src/rsc/rsc-rendering.ts +47 -44
- package/src/rsc/server-action.ts +24 -10
- package/src/rsc/ssr-setup.ts +128 -0
- package/src/rsc/types.ts +11 -1
- package/src/search-params.ts +16 -13
- package/src/segment-content-promise.ts +67 -0
- package/src/segment-loader-promise.ts +122 -0
- package/src/segment-system.tsx +109 -23
- package/src/server/context.ts +174 -19
- package/src/server/handle-store.ts +19 -0
- package/src/server/loader-registry.ts +9 -8
- package/src/server/request-context.ts +218 -65
- package/src/server.ts +6 -0
- package/src/ssr/index.tsx +4 -0
- package/src/static-handler.ts +18 -6
- package/src/theme/index.ts +4 -13
- package/src/types/cache-types.ts +4 -4
- package/src/types/handler-context.ts +140 -72
- package/src/types/loader-types.ts +41 -15
- package/src/types/request-scope.ts +126 -0
- package/src/types/route-config.ts +17 -8
- package/src/types/route-entry.ts +19 -1
- package/src/types/segments.ts +2 -5
- package/src/urls/include-helper.ts +24 -14
- package/src/urls/path-helper-types.ts +39 -6
- package/src/urls/path-helper.ts +48 -13
- package/src/urls/pattern-types.ts +12 -0
- package/src/urls/response-types.ts +18 -16
- package/src/use-loader.tsx +77 -5
- package/src/vite/discovery/bundle-postprocess.ts +61 -89
- package/src/vite/discovery/discover-routers.ts +7 -4
- package/src/vite/discovery/prerender-collection.ts +162 -88
- package/src/vite/discovery/state.ts +17 -13
- package/src/vite/index.ts +8 -3
- package/src/vite/plugin-types.ts +51 -79
- package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
- package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
- package/src/vite/plugins/expose-action-id.ts +1 -3
- package/src/vite/plugins/expose-id-utils.ts +12 -0
- package/src/vite/plugins/expose-ids/handler-transform.ts +30 -0
- package/src/vite/plugins/expose-internal-ids.ts +257 -40
- package/src/vite/plugins/performance-tracks.ts +88 -0
- package/src/vite/plugins/refresh-cmd.ts +127 -0
- package/src/vite/plugins/version-plugin.ts +13 -1
- package/src/vite/rango.ts +190 -217
- package/src/vite/router-discovery.ts +241 -45
- package/src/vite/utils/banner.ts +4 -4
- package/src/vite/utils/package-resolution.ts +34 -1
- package/src/vite/utils/prerender-utils.ts +97 -5
- package/src/vite/utils/shared-utils.ts +3 -2
- package/skills/testing/SKILL.md +0 -226
- package/src/route-definition/route-function.ts +0 -119
|
@@ -19,6 +19,14 @@ import type { BoundTransaction } from "./navigation-transaction.js";
|
|
|
19
19
|
import { ServerRedirect } from "../errors.js";
|
|
20
20
|
import { debugLog } from "./logging.js";
|
|
21
21
|
import { validateRedirectOrigin } from "./validate-redirect-origin.js";
|
|
22
|
+
import type { NavigationUpdate } from "./types.js";
|
|
23
|
+
|
|
24
|
+
/** Build a scroll payload from the commit's scroll option */
|
|
25
|
+
function toScrollPayload(
|
|
26
|
+
scroll: boolean | undefined,
|
|
27
|
+
): NonNullable<NavigationUpdate["scroll"]> {
|
|
28
|
+
return { enabled: scroll !== false ? scroll : false };
|
|
29
|
+
}
|
|
22
30
|
|
|
23
31
|
/**
|
|
24
32
|
* Configuration for creating a partial updater
|
|
@@ -31,8 +39,15 @@ export interface PartialUpdateConfig {
|
|
|
31
39
|
segments: ResolvedSegment[],
|
|
32
40
|
options?: RenderSegmentsOptions,
|
|
33
41
|
) => Promise<ReactNode> | ReactNode;
|
|
34
|
-
/** RSC version
|
|
35
|
-
|
|
42
|
+
/** RSC version getter — returns the current version (may change after HMR) */
|
|
43
|
+
getVersion?: () => string | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* Replace the active app-shell when a cross-app navigation is detected.
|
|
46
|
+
* Called before the full-update tree replacement renders, so the new
|
|
47
|
+
* payload's rootLayout, basename, and version are picked up. Theme,
|
|
48
|
+
* warmup, and prefetch TTL are not part of the shell — see AppShell.
|
|
49
|
+
*/
|
|
50
|
+
applyAppShell?: (next: import("./app-shell.js").AppShell) => void;
|
|
36
51
|
}
|
|
37
52
|
|
|
38
53
|
/**
|
|
@@ -96,7 +111,14 @@ export type PartialUpdater = (
|
|
|
96
111
|
export function createPartialUpdater(
|
|
97
112
|
config: PartialUpdateConfig,
|
|
98
113
|
): PartialUpdater {
|
|
99
|
-
const {
|
|
114
|
+
const {
|
|
115
|
+
store,
|
|
116
|
+
client,
|
|
117
|
+
onUpdate,
|
|
118
|
+
renderSegments,
|
|
119
|
+
getVersion = () => undefined,
|
|
120
|
+
applyAppShell,
|
|
121
|
+
} = config;
|
|
100
122
|
|
|
101
123
|
/**
|
|
102
124
|
* Get current page's cached segments as an array
|
|
@@ -153,9 +175,16 @@ export function createPartialUpdater(
|
|
|
153
175
|
segments = segmentIds ?? segmentState.currentSegmentIds;
|
|
154
176
|
}
|
|
155
177
|
|
|
156
|
-
// For intercept revalidation, use the intercept source URL as previousUrl
|
|
178
|
+
// For intercept revalidation, use the intercept source URL as previousUrl.
|
|
179
|
+
// For leave-intercept, tx.currentUrl captures window.location.href at tx
|
|
180
|
+
// creation, which on popstate is already the destination URL and would
|
|
181
|
+
// tell the server "from == to". segmentState.currentUrl still points at
|
|
182
|
+
// the URL the cached segments render (the intercept URL), which is the
|
|
183
|
+
// correct "from" for the server's diff computation.
|
|
157
184
|
const previousUrl =
|
|
158
|
-
|
|
185
|
+
mode.type === "leave-intercept"
|
|
186
|
+
? segmentState.currentUrl || tx.currentUrl
|
|
187
|
+
: interceptSourceUrl || tx.currentUrl || segmentState.currentUrl;
|
|
159
188
|
|
|
160
189
|
debugLog(`\n[Browser] >>> NAVIGATION`);
|
|
161
190
|
debugLog(`[Browser] From: ${previousUrl}`);
|
|
@@ -174,6 +203,11 @@ export function createPartialUpdater(
|
|
|
174
203
|
targetCache && targetCache.length > 0
|
|
175
204
|
? targetCache
|
|
176
205
|
: getCurrentCachedSegments();
|
|
206
|
+
const cachedSegsSource =
|
|
207
|
+
targetCache && targetCache.length > 0 ? "history-cache" : "current-page";
|
|
208
|
+
debugLog(
|
|
209
|
+
`[Browser] cachedSegs source: ${cachedSegsSource} (${cachedSegs.length} segments: ${cachedSegs.map((s) => s.id).join(", ")})`,
|
|
210
|
+
);
|
|
177
211
|
|
|
178
212
|
// Fetch partial payload (no abort signal - RSC doesn't support it well)
|
|
179
213
|
let fetchResult: Awaited<ReturnType<NavigationClient["fetchPartial"]>>;
|
|
@@ -185,7 +219,8 @@ export function createPartialUpdater(
|
|
|
185
219
|
// (action redirect sends empty segments for a fresh render).
|
|
186
220
|
staleRevalidation:
|
|
187
221
|
mode.type === "stale-revalidation" || segments.length === 0,
|
|
188
|
-
version,
|
|
222
|
+
version: getVersion(),
|
|
223
|
+
routerId: store.getRouterId?.(),
|
|
189
224
|
});
|
|
190
225
|
// Mark navigation as streaming (response received, now parsing RSC).
|
|
191
226
|
// Called after fetchPartial so pendingUrl stays set during the network wait,
|
|
@@ -198,6 +233,32 @@ export function createPartialUpdater(
|
|
|
198
233
|
streamingToken.end();
|
|
199
234
|
});
|
|
200
235
|
|
|
236
|
+
// Detect app switch: if routerId changed, the navigation crossed into
|
|
237
|
+
// a different router (e.g., via host router path mount). Downgrade
|
|
238
|
+
// partial to full so the entire tree is replaced without reconciliation
|
|
239
|
+
// against stale segments from the previous app, and replace the app
|
|
240
|
+
// shell (rootLayout, basename, version) so the target app's document
|
|
241
|
+
// and router config take effect instead of remaining captured from the
|
|
242
|
+
// initial load. Theme, warmup, and prefetch TTL are intentionally
|
|
243
|
+
// document-lifetime (see AppShell doc); a new document navigation
|
|
244
|
+
// applies them.
|
|
245
|
+
if (payload.metadata?.routerId) {
|
|
246
|
+
const prevRouterId = store.getRouterId?.();
|
|
247
|
+
if (prevRouterId && prevRouterId !== payload.metadata.routerId) {
|
|
248
|
+
debugLog(
|
|
249
|
+
`[Browser] App switch detected (${prevRouterId} → ${payload.metadata.routerId}), forcing full update`,
|
|
250
|
+
);
|
|
251
|
+
payload.metadata.isPartial = false;
|
|
252
|
+
applyAppShell?.({
|
|
253
|
+
routerId: payload.metadata.routerId,
|
|
254
|
+
rootLayout: payload.metadata.rootLayout,
|
|
255
|
+
basename: payload.metadata.basename,
|
|
256
|
+
version: payload.metadata.version,
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
store.setRouterId?.(payload.metadata.routerId);
|
|
260
|
+
}
|
|
261
|
+
|
|
201
262
|
// Handle server-side redirect with state
|
|
202
263
|
if (payload.metadata?.redirect) {
|
|
203
264
|
if (signal?.aborted) {
|
|
@@ -246,7 +307,21 @@ export function createPartialUpdater(
|
|
|
246
307
|
forceAwait: true,
|
|
247
308
|
});
|
|
248
309
|
|
|
249
|
-
tx.commit(
|
|
310
|
+
const { scroll: commitScroll } = tx.commit(
|
|
311
|
+
matchedIds,
|
|
312
|
+
existingSegments,
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
// tx.commit() cached the source page's handleData because
|
|
316
|
+
// eventController hasn't been updated yet. Overwrite with the
|
|
317
|
+
// correct cached handleData to prevent cache corruption on
|
|
318
|
+
// subsequent navigations to this same URL.
|
|
319
|
+
if (mode.targetCacheHandleData) {
|
|
320
|
+
store.updateCacheHandleData(
|
|
321
|
+
store.getHistoryKey(),
|
|
322
|
+
mode.targetCacheHandleData,
|
|
323
|
+
);
|
|
324
|
+
}
|
|
250
325
|
|
|
251
326
|
// Include cachedHandleData in metadata so NavigationProvider can restore
|
|
252
327
|
// breadcrumbs and other handle data from cache.
|
|
@@ -260,6 +335,7 @@ export function createPartialUpdater(
|
|
|
260
335
|
...metadataWithoutHandles,
|
|
261
336
|
cachedHandleData: mode.targetCacheHandleData,
|
|
262
337
|
},
|
|
338
|
+
scroll: toScrollPayload(commitScroll),
|
|
263
339
|
};
|
|
264
340
|
|
|
265
341
|
const cachedHasTransition = existingSegments.some(
|
|
@@ -290,11 +366,15 @@ export function createPartialUpdater(
|
|
|
290
366
|
forceAwait: true,
|
|
291
367
|
});
|
|
292
368
|
|
|
293
|
-
tx.commit(
|
|
369
|
+
const { scroll: leaveScroll } = tx.commit(
|
|
370
|
+
matchedIds,
|
|
371
|
+
existingSegments,
|
|
372
|
+
);
|
|
294
373
|
|
|
295
374
|
onUpdate({
|
|
296
375
|
root: newTree,
|
|
297
376
|
metadata: payload.metadata,
|
|
377
|
+
scroll: toScrollPayload(leaveScroll),
|
|
298
378
|
});
|
|
299
379
|
|
|
300
380
|
debugLog("[Browser] Navigation complete (left intercept)");
|
|
@@ -411,8 +491,10 @@ export function createPartialUpdater(
|
|
|
411
491
|
}
|
|
412
492
|
}
|
|
413
493
|
|
|
414
|
-
// Commit navigation -
|
|
415
|
-
|
|
494
|
+
// Commit navigation - use server's matched as the authoritative segment ID list.
|
|
495
|
+
// reconciled.segments may be missing IDs (e.g., loader segments not in diff or cache)
|
|
496
|
+
// but the server's matched always includes all expected segment IDs.
|
|
497
|
+
const allSegmentIds = matchedIds;
|
|
416
498
|
const serverLocationState = payload.metadata?.locationState;
|
|
417
499
|
const overrides: CommitOverrides | undefined = isInterceptResponse
|
|
418
500
|
? {
|
|
@@ -424,7 +506,11 @@ export function createPartialUpdater(
|
|
|
424
506
|
: serverLocationState
|
|
425
507
|
? { serverState: serverLocationState }
|
|
426
508
|
: undefined;
|
|
427
|
-
tx.commit(
|
|
509
|
+
const { scroll: navScroll } = tx.commit(
|
|
510
|
+
allSegmentIds,
|
|
511
|
+
reconciled.segments,
|
|
512
|
+
overrides,
|
|
513
|
+
);
|
|
428
514
|
|
|
429
515
|
// For stale revalidation: verify history key hasn't changed before updating UI
|
|
430
516
|
if (mode.type === "stale-revalidation") {
|
|
@@ -439,8 +525,10 @@ export function createPartialUpdater(
|
|
|
439
525
|
|
|
440
526
|
debugLog("[partial-update] updating document");
|
|
441
527
|
|
|
442
|
-
// Emit update to trigger React render
|
|
528
|
+
// Emit update to trigger React render.
|
|
529
|
+
// Scroll info is included so NavigationProvider applies it after React commits.
|
|
443
530
|
const hasTransition = reconciled.mainSegments.some((s) => s.transition);
|
|
531
|
+
const scrollPayload = toScrollPayload(navScroll);
|
|
444
532
|
|
|
445
533
|
if (mode.type === "action" || mode.type === "stale-revalidation") {
|
|
446
534
|
startTransition(() => {
|
|
@@ -450,6 +538,7 @@ export function createPartialUpdater(
|
|
|
450
538
|
onUpdate({
|
|
451
539
|
root: newTree,
|
|
452
540
|
metadata: payload.metadata!,
|
|
541
|
+
scroll: scrollPayload,
|
|
453
542
|
});
|
|
454
543
|
});
|
|
455
544
|
} else if (hasTransition) {
|
|
@@ -460,12 +549,14 @@ export function createPartialUpdater(
|
|
|
460
549
|
onUpdate({
|
|
461
550
|
root: newTree,
|
|
462
551
|
metadata: payload.metadata!,
|
|
552
|
+
scroll: scrollPayload,
|
|
463
553
|
});
|
|
464
554
|
});
|
|
465
555
|
} else {
|
|
466
556
|
onUpdate({
|
|
467
557
|
root: newTree,
|
|
468
558
|
metadata: payload.metadata!,
|
|
559
|
+
scroll: scrollPayload,
|
|
469
560
|
});
|
|
470
561
|
}
|
|
471
562
|
|
|
@@ -492,15 +583,16 @@ export function createPartialUpdater(
|
|
|
492
583
|
}
|
|
493
584
|
|
|
494
585
|
const fullUpdateServerState = payload.metadata?.locationState;
|
|
495
|
-
|
|
496
|
-
tx.commit(segmentIds, segments, {
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
586
|
+
const { scroll: fullScroll } = fullUpdateServerState
|
|
587
|
+
? tx.commit(segmentIds, segments, {
|
|
588
|
+
serverState: fullUpdateServerState,
|
|
589
|
+
})
|
|
590
|
+
: tx.commit(segmentIds, segments);
|
|
500
591
|
|
|
501
592
|
const fullHasTransition = segments.some(
|
|
502
593
|
(s: ResolvedSegment) => s.transition,
|
|
503
594
|
);
|
|
595
|
+
const fullScrollPayload = toScrollPayload(fullScroll);
|
|
504
596
|
|
|
505
597
|
if (mode.type === "stale-revalidation") {
|
|
506
598
|
await rawStreamComplete;
|
|
@@ -511,6 +603,7 @@ export function createPartialUpdater(
|
|
|
511
603
|
onUpdate({
|
|
512
604
|
root: newTree,
|
|
513
605
|
metadata: payload.metadata!,
|
|
606
|
+
scroll: fullScrollPayload,
|
|
514
607
|
});
|
|
515
608
|
});
|
|
516
609
|
} else if (mode.type === "action") {
|
|
@@ -521,6 +614,7 @@ export function createPartialUpdater(
|
|
|
521
614
|
onUpdate({
|
|
522
615
|
root: newTree,
|
|
523
616
|
metadata: payload.metadata!,
|
|
617
|
+
scroll: fullScrollPayload,
|
|
524
618
|
});
|
|
525
619
|
});
|
|
526
620
|
} else if (fullHasTransition) {
|
|
@@ -531,12 +625,14 @@ export function createPartialUpdater(
|
|
|
531
625
|
onUpdate({
|
|
532
626
|
root: newTree,
|
|
533
627
|
metadata: payload.metadata!,
|
|
628
|
+
scroll: fullScrollPayload,
|
|
534
629
|
});
|
|
535
630
|
});
|
|
536
631
|
} else {
|
|
537
632
|
onUpdate({
|
|
538
633
|
root: newTree,
|
|
539
634
|
metadata: payload.metadata!,
|
|
635
|
+
scroll: fullScrollPayload,
|
|
540
636
|
});
|
|
541
637
|
}
|
|
542
638
|
|
|
@@ -1,67 +1,314 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Prefetch
|
|
2
|
+
* Prefetch Cache
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* In-memory cache storing prefetched Response objects for instant cache hits
|
|
5
|
+
* on subsequent navigation. Two key scopes are in play:
|
|
6
|
+
* - Wildcard (default): built by `buildPrefetchKey(rangoState, target)` —
|
|
7
|
+
* shape `rangoState\0/target?...`. Shared across all source pages and
|
|
8
|
+
* invalidated automatically when Rango state bumps (deploy or
|
|
9
|
+
* server-action invalidation).
|
|
10
|
+
* - Source-scoped: built by `buildSourceKey(rangoState, sourceHref, target)`
|
|
11
|
+
* — shape `rangoState\0sourceHref\0/target?...`. Embeds the Rango state
|
|
12
|
+
* (so rotation invalidates source-scoped entries too) plus the source
|
|
13
|
+
* href (so each originating page gets its own slot). Populated when the
|
|
14
|
+
* server tags a response with `X-RSC-Prefetch-Scope: source` (intercept
|
|
15
|
+
* modals etc.), OR when a Link opts in with `prefetchKey=":source"` — in
|
|
16
|
+
* both cases so source-sensitive responses cannot bleed into navigations
|
|
17
|
+
* from other pages.
|
|
18
|
+
*
|
|
19
|
+
* Also tracks in-flight prefetch promises. Each promise resolves to the
|
|
20
|
+
* navigation branch of a tee'd Response, allowing navigation to adopt a
|
|
21
|
+
* still-downloading prefetch without reparsing or buffering the body. A
|
|
22
|
+
* single promise can be registered under multiple alias keys (see
|
|
23
|
+
* `setInflightPromiseWithAliases`) so same-source navigations adopt via
|
|
24
|
+
* their source key while cross-source ones fall through to the wildcard
|
|
25
|
+
* alias — with consume/clear atomically removing every alias.
|
|
26
|
+
*
|
|
27
|
+
* Replaces the previous browser HTTP cache approach which was unreliable
|
|
28
|
+
* due to response draining race conditions and browser inconsistencies.
|
|
7
29
|
*/
|
|
8
30
|
|
|
9
|
-
import {
|
|
31
|
+
import { abortAllPrefetches } from "./queue.js";
|
|
10
32
|
import { invalidateRangoState } from "../rango-state.js";
|
|
11
33
|
|
|
34
|
+
// Default TTL: 5 minutes. Overridden by initPrefetchCache() with
|
|
35
|
+
// the server-configured prefetchCacheTTL from router options.
|
|
36
|
+
// 0 disables the in-memory cache entirely.
|
|
37
|
+
let cacheTTL = 300_000;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Initialize the prefetch cache with the configured TTL.
|
|
41
|
+
* Called once at app startup with the value from server metadata.
|
|
42
|
+
* A TTL of 0 disables the in-memory cache and all prefetching.
|
|
43
|
+
*/
|
|
44
|
+
export function initPrefetchCache(ttlMs: number): void {
|
|
45
|
+
cacheTTL = ttlMs;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Check if the prefetch cache is disabled (TTL <= 0).
|
|
50
|
+
* When disabled, no prefetch requests should be issued.
|
|
51
|
+
*/
|
|
52
|
+
export function isPrefetchCacheDisabled(): boolean {
|
|
53
|
+
return cacheTTL <= 0;
|
|
54
|
+
}
|
|
55
|
+
const MAX_PREFETCH_CACHE_SIZE = 50;
|
|
56
|
+
|
|
57
|
+
interface PrefetchCacheEntry {
|
|
58
|
+
response: Response;
|
|
59
|
+
timestamp: number;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const cache = new Map<string, PrefetchCacheEntry>();
|
|
12
63
|
const inflight = new Set<string>();
|
|
13
|
-
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* In-flight promise map. When a prefetch fetch is in progress, its
|
|
67
|
+
* Promise<Response | null> is stored here so navigation can await
|
|
68
|
+
* it instead of starting a duplicate request.
|
|
69
|
+
*/
|
|
70
|
+
const inflightPromises = new Map<string, Promise<Response | null>>();
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Alias map for in-flight promises registered under multiple keys (see
|
|
74
|
+
* dual inflight in prefetch/fetch.ts). Records each key's sibling set so
|
|
75
|
+
* that consuming or clearing any one key atomically removes every alias —
|
|
76
|
+
* guaranteeing a single consumer for the shared Response stream.
|
|
77
|
+
*/
|
|
78
|
+
const inflightAliases = new Map<string, string[]>();
|
|
14
79
|
|
|
15
80
|
// Generation counter incremented on each clearPrefetchCache(). Fetches that
|
|
16
|
-
// started before a clear carry a stale generation and must not
|
|
17
|
-
//
|
|
18
|
-
// due to Rango-State rotation).
|
|
81
|
+
// started before a clear carry a stale generation and must not store their
|
|
82
|
+
// response (the data may be stale due to a server action invalidation).
|
|
19
83
|
let generation = 0;
|
|
20
84
|
|
|
21
85
|
/**
|
|
22
|
-
*
|
|
86
|
+
* Build a cache key by combining a scope prefix with the target URL.
|
|
87
|
+
*
|
|
88
|
+
* Low-level primitive — callers that want a specific scope should use
|
|
89
|
+
* one of:
|
|
90
|
+
* - Wildcard (source-agnostic): prefix is the Rango state value from
|
|
91
|
+
* `getRangoState()`. Shared across all source pages. Invalidated
|
|
92
|
+
* automatically when Rango state bumps (deploy or server-action).
|
|
93
|
+
* Key shape: `rangoState\0/target?...`.
|
|
94
|
+
* - Source-scoped: use `buildSourceKey()`. Key shape:
|
|
95
|
+
* `rangoState\0sourceHref\0/target?...` — embeds the Rango state so
|
|
96
|
+
* rotation invalidates source-scoped entries alongside wildcard ones,
|
|
97
|
+
* plus the source page href so the key is unique per originating page.
|
|
98
|
+
* Populated either when the server tags a response with
|
|
99
|
+
* `X-RSC-Prefetch-Scope: source` (intercept modals, etc.) or when a
|
|
100
|
+
* Link opts in via `prefetchKey=":source"`.
|
|
101
|
+
*
|
|
102
|
+
* The `_rsc_segments` query param that travels in the target URL means
|
|
103
|
+
* clients with different mounted segment trees naturally get different
|
|
104
|
+
* keys — so segment-level diffs remain consistent across both scopes.
|
|
105
|
+
*/
|
|
106
|
+
export function buildPrefetchKey(prefix: string, targetUrl: URL): string {
|
|
107
|
+
return prefix + "\0" + targetUrl.pathname + targetUrl.search;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Build a source-scoped cache key. Key shape:
|
|
112
|
+
* `rangoState\0sourceHref\0/target?...`.
|
|
113
|
+
*
|
|
114
|
+
* - `rangoState` is included so state rotation invalidates source-scoped
|
|
115
|
+
* entries alongside wildcard ones.
|
|
116
|
+
* - `sourceHref` makes the key unique per originating page.
|
|
117
|
+
*/
|
|
118
|
+
export function buildSourceKey(
|
|
119
|
+
rangoState: string,
|
|
120
|
+
sourceHref: string,
|
|
121
|
+
targetUrl: URL,
|
|
122
|
+
): string {
|
|
123
|
+
return buildPrefetchKey(rangoState + "\0" + sourceHref, targetUrl);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Walk an inflight key plus any sibling aliases registered via
|
|
128
|
+
* `setInflightPromiseWithAliases`, invoking `fn` for each.
|
|
129
|
+
*/
|
|
130
|
+
function forEachAlias(key: string, fn: (k: string) => void): void {
|
|
131
|
+
const aliases = inflightAliases.get(key);
|
|
132
|
+
if (aliases) {
|
|
133
|
+
for (const k of aliases) fn(k);
|
|
134
|
+
} else {
|
|
135
|
+
fn(key);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Check if a prefetch is already cached, in-flight, or queued for the given key.
|
|
23
141
|
*/
|
|
24
142
|
export function hasPrefetch(key: string): boolean {
|
|
25
|
-
|
|
143
|
+
if (inflight.has(key)) return true;
|
|
144
|
+
if (cacheTTL <= 0) return false;
|
|
145
|
+
const entry = cache.get(key);
|
|
146
|
+
if (!entry) return false;
|
|
147
|
+
if (Date.now() - entry.timestamp > cacheTTL) {
|
|
148
|
+
cache.delete(key);
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
return true;
|
|
26
152
|
}
|
|
27
153
|
|
|
28
154
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
155
|
+
* Consume a cached prefetch response. Returns null if not found or expired.
|
|
156
|
+
* One-time consumption: the entry is deleted after retrieval.
|
|
157
|
+
* Returns null when caching is disabled (TTL <= 0).
|
|
158
|
+
*
|
|
159
|
+
* Does NOT check in-flight prefetches — use consumeInflightPrefetch()
|
|
160
|
+
* for that (returns a Promise instead of a Response).
|
|
31
161
|
*/
|
|
32
|
-
export function
|
|
33
|
-
return
|
|
162
|
+
export function consumePrefetch(key: string): Response | null {
|
|
163
|
+
if (cacheTTL <= 0) return null;
|
|
164
|
+
const entry = cache.get(key);
|
|
165
|
+
if (!entry) return null;
|
|
166
|
+
if (Date.now() - entry.timestamp > cacheTTL) {
|
|
167
|
+
cache.delete(key);
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
cache.delete(key);
|
|
171
|
+
return entry.response;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Consume an in-flight prefetch promise. Returns null if no prefetch is
|
|
176
|
+
* in-flight for this key. The returned Promise resolves to the buffered
|
|
177
|
+
* Response (or null if the fetch failed/was aborted).
|
|
178
|
+
*
|
|
179
|
+
* One-time consumption: the promise entry is removed (along with any
|
|
180
|
+
* sibling aliases registered via `setInflightPromiseWithAliases`) so a
|
|
181
|
+
* second call on any alias returns null — only one caller can adopt the
|
|
182
|
+
* shared Response stream. The `inflight` set entry is intentionally
|
|
183
|
+
* kept so that `hasPrefetch()` continues to return true while the
|
|
184
|
+
* underlying fetch is still downloading — this prevents
|
|
185
|
+
* `prefetchDirect()` or other callers from starting a duplicate request
|
|
186
|
+
* during the handoff window. The inflight flag is cleaned up naturally
|
|
187
|
+
* by `clearPrefetchInflight()` in the fetch's `.finally()`.
|
|
188
|
+
*/
|
|
189
|
+
export function consumeInflightPrefetch(
|
|
190
|
+
key: string,
|
|
191
|
+
): Promise<Response | null> | null {
|
|
192
|
+
const promise = inflightPromises.get(key);
|
|
193
|
+
if (!promise) return null;
|
|
194
|
+
// Remove the promise under every alias so a second consumer cannot
|
|
195
|
+
// adopt the same stream and race on the body. `inflightAliases` is
|
|
196
|
+
// intentionally preserved — `clearPrefetchInflight()` in the fetch's
|
|
197
|
+
// `.finally()` still needs it to clear every inflight flag; deleting
|
|
198
|
+
// here would strand the sibling's flag forever.
|
|
199
|
+
forEachAlias(key, (k) => inflightPromises.delete(k));
|
|
200
|
+
return promise;
|
|
34
201
|
}
|
|
35
202
|
|
|
36
203
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
204
|
+
* Store a prefetch response in the in-memory cache.
|
|
205
|
+
* The response should be a clone() of the original so the caller can
|
|
206
|
+
* still consume the body. The clone's body streams independently.
|
|
207
|
+
*
|
|
208
|
+
* Skips storage if the generation has changed since the fetch started
|
|
209
|
+
* (a server action invalidated the cache mid-flight).
|
|
40
210
|
*/
|
|
41
|
-
export function
|
|
42
|
-
|
|
43
|
-
|
|
211
|
+
export function storePrefetch(
|
|
212
|
+
key: string,
|
|
213
|
+
response: Response,
|
|
214
|
+
fetchGeneration: number,
|
|
215
|
+
): void {
|
|
216
|
+
if (cacheTTL <= 0) return;
|
|
217
|
+
if (fetchGeneration !== generation) return;
|
|
218
|
+
|
|
219
|
+
// Evict expired entries
|
|
220
|
+
const now = Date.now();
|
|
221
|
+
for (const [k, entry] of cache) {
|
|
222
|
+
if (now - entry.timestamp > cacheTTL) {
|
|
223
|
+
cache.delete(k);
|
|
224
|
+
}
|
|
44
225
|
}
|
|
226
|
+
|
|
227
|
+
// FIFO eviction if at capacity
|
|
228
|
+
if (cache.size >= MAX_PREFETCH_CACHE_SIZE) {
|
|
229
|
+
const oldest = cache.keys().next().value;
|
|
230
|
+
if (oldest) cache.delete(oldest);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
cache.set(key, { response, timestamp: now });
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Capture the current generation. The returned value is passed to
|
|
238
|
+
* storePrefetch so it can detect stale completions.
|
|
239
|
+
*/
|
|
240
|
+
export function currentGeneration(): number {
|
|
241
|
+
return generation;
|
|
45
242
|
}
|
|
46
243
|
|
|
47
244
|
export function markPrefetchInflight(key: string): void {
|
|
48
245
|
inflight.add(key);
|
|
49
246
|
}
|
|
50
247
|
|
|
248
|
+
/**
|
|
249
|
+
* Store the in-flight Promise for a prefetch so navigation can reuse it.
|
|
250
|
+
*/
|
|
251
|
+
export function setInflightPromise(
|
|
252
|
+
key: string,
|
|
253
|
+
promise: Promise<Response | null>,
|
|
254
|
+
): void {
|
|
255
|
+
inflightPromises.set(key, promise);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Store the same in-flight Promise under multiple keys, recording them
|
|
260
|
+
* as sibling aliases. Consuming or clearing any one alias atomically
|
|
261
|
+
* removes every entry, guaranteeing the shared Response stream has a
|
|
262
|
+
* single consumer even when navigation looks up either key.
|
|
263
|
+
*/
|
|
264
|
+
export function setInflightPromiseWithAliases(
|
|
265
|
+
keys: string[],
|
|
266
|
+
promise: Promise<Response | null>,
|
|
267
|
+
): void {
|
|
268
|
+
for (const k of keys) {
|
|
269
|
+
inflightPromises.set(k, promise);
|
|
270
|
+
inflightAliases.set(k, keys);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
51
274
|
export function clearPrefetchInflight(key: string): void {
|
|
52
|
-
|
|
275
|
+
forEachAlias(key, (k) => {
|
|
276
|
+
inflight.delete(k);
|
|
277
|
+
inflightPromises.delete(k);
|
|
278
|
+
inflightAliases.delete(k);
|
|
279
|
+
});
|
|
53
280
|
}
|
|
54
281
|
|
|
55
282
|
/**
|
|
56
|
-
* Invalidate prefetch state. Called when server actions mutate data.
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
283
|
+
* Invalidate all prefetch state. Called when server actions mutate data.
|
|
284
|
+
* Clears the in-memory cache, cancels in-flight prefetches, and rotates
|
|
285
|
+
* the Rango state key so CDN-cached responses are also invalidated.
|
|
286
|
+
*
|
|
287
|
+
* Uses abortAllPrefetches (hard cancel) because in-flight responses
|
|
288
|
+
* may contain stale data after a mutation.
|
|
60
289
|
*/
|
|
61
290
|
export function clearPrefetchCache(): void {
|
|
62
291
|
generation++;
|
|
63
292
|
inflight.clear();
|
|
64
|
-
|
|
65
|
-
|
|
293
|
+
inflightPromises.clear();
|
|
294
|
+
inflightAliases.clear();
|
|
295
|
+
cache.clear();
|
|
296
|
+
abortAllPrefetches();
|
|
66
297
|
invalidateRangoState();
|
|
67
298
|
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Drop all in-memory prefetch state for this tab without rotating rango-state.
|
|
302
|
+
*
|
|
303
|
+
* Use for local-only invalidations (e.g. app switch in this tab) where
|
|
304
|
+
* other tabs should NOT observe a state rotation. Unlike clearPrefetchCache,
|
|
305
|
+
* does not call invalidateRangoState, so the shared X-Rango-State token
|
|
306
|
+
* stays intact and siblings in the old app keep their prefetches.
|
|
307
|
+
*/
|
|
308
|
+
export function clearPrefetchCacheLocal(): void {
|
|
309
|
+
generation++;
|
|
310
|
+
inflight.clear();
|
|
311
|
+
inflightPromises.clear();
|
|
312
|
+
cache.clear();
|
|
313
|
+
abortAllPrefetches();
|
|
314
|
+
}
|