@rangojs/router 0.0.0-experimental.8a4d0430 → 0.0.0-experimental.8bcfea43
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 +126 -38
- package/dist/bin/rango.js +138 -50
- package/dist/vite/index.js +1171 -461
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +19 -16
- package/skills/breadcrumbs/SKILL.md +3 -1
- package/skills/cache-guide/SKILL.md +32 -0
- package/skills/caching/SKILL.md +45 -4
- package/skills/handler-use/SKILL.md +362 -0
- package/skills/hooks/SKILL.md +28 -20
- 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 +88 -45
- 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 +110 -68
- package/skills/rango/SKILL.md +24 -22
- package/skills/response-routes/SKILL.md +8 -0
- package/skills/route/SKILL.md +55 -0
- package/skills/router-setup/SKILL.md +87 -2
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/typesafety/SKILL.md +13 -1
- package/src/__internal.ts +1 -1
- 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/navigation-bridge.ts +90 -16
- package/src/browser/navigation-client.ts +167 -59
- 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 +184 -16
- package/src/browser/prefetch/fetch.ts +180 -33
- 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 +81 -9
- 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 +168 -65
- 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 +49 -5
- package/src/build/generate-manifest.ts +6 -6
- package/src/build/generate-route-types.ts +3 -0
- package/src/build/route-trie.ts +50 -24
- 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.tsx +84 -230
- package/src/context-var.ts +72 -2
- package/src/debug.ts +2 -2
- package/src/handle.ts +40 -0
- package/src/index.rsc.ts +6 -1
- package/src/index.ts +49 -6
- package/src/outlet-context.ts +1 -1
- package/src/prerender/store.ts +5 -4
- 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 -0
- 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 +101 -25
- package/src/router/intercept-resolution.ts +11 -4
- package/src/router/lazy-includes.ts +10 -7
- package/src/router/loader-resolution.ts +159 -21
- package/src/router/logging.ts +5 -2
- package/src/router/manifest.ts +31 -16
- package/src/router/match-api.ts +127 -192
- 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 +104 -10
- package/src/router/metrics.ts +6 -1
- package/src/router/middleware-types.ts +8 -30
- package/src/router/middleware.ts +36 -10
- package/src/router/navigation-snapshot.ts +182 -0
- package/src/router/pattern-matching.ts +60 -9
- package/src/router/prerender-match.ts +110 -10
- package/src/router/preview-match.ts +30 -102
- package/src/router/request-classification.ts +310 -0
- package/src/router/route-snapshot.ts +245 -0
- package/src/router/router-context.ts +6 -1
- package/src/router/router-interfaces.ts +36 -4
- package/src/router/router-options.ts +37 -11
- package/src/router/segment-resolution/fresh.ts +198 -20
- package/src/router/segment-resolution/helpers.ts +29 -24
- package/src/router/segment-resolution/loader-cache.ts +1 -0
- package/src/router/segment-resolution/revalidation.ts +438 -300
- package/src/router/segment-wrappers.ts +2 -0
- package/src/router/trie-matching.ts +10 -4
- package/src/router/types.ts +1 -0
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +60 -8
- package/src/rsc/handler.ts +478 -374
- package/src/rsc/helpers.ts +69 -41
- package/src/rsc/loader-fetch.ts +23 -3
- package/src/rsc/manifest-init.ts +5 -1
- package/src/rsc/progressive-enhancement.ts +16 -2
- package/src/rsc/response-route-handler.ts +14 -1
- package/src/rsc/rsc-rendering.ts +19 -1
- package/src/rsc/server-action.ts +10 -0
- package/src/rsc/ssr-setup.ts +2 -2
- package/src/rsc/types.ts +9 -1
- 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 +166 -17
- package/src/server/handle-store.ts +19 -0
- package/src/server/loader-registry.ts +9 -8
- package/src/server/request-context.ts +194 -60
- package/src/ssr/index.tsx +4 -0
- package/src/static-handler.ts +18 -6
- package/src/types/cache-types.ts +4 -4
- package/src/types/handler-context.ts +137 -65
- package/src/types/loader-types.ts +41 -15
- package/src/types/request-scope.ts +126 -0
- package/src/types/route-entry.ts +19 -1
- package/src/types/segments.ts +2 -0
- 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/debug.ts +55 -0
- package/src/vite/discovery/bundle-postprocess.ts +30 -33
- package/src/vite/discovery/discover-routers.ts +5 -1
- package/src/vite/discovery/prerender-collection.ts +128 -74
- package/src/vite/discovery/state.ts +13 -6
- package/src/vite/index.ts +4 -0
- 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 +86 -0
- package/src/vite/plugins/refresh-cmd.ts +88 -26
- package/src/vite/plugins/version-plugin.ts +13 -1
- package/src/vite/rango.ts +204 -217
- package/src/vite/router-discovery.ts +335 -64
- package/src/vite/utils/banner.ts +4 -4
- package/src/vite/utils/package-resolution.ts +41 -1
- package/src/vite/utils/prerender-utils.ts +37 -5
- package/src/vite/utils/shared-utils.ts +3 -2
|
@@ -4,6 +4,9 @@ import type {
|
|
|
4
4
|
NavigateOptionsInternal,
|
|
5
5
|
ResolvedSegment,
|
|
6
6
|
} from "./types.js";
|
|
7
|
+
import { setAppVersion } from "./app-version.js";
|
|
8
|
+
import { setRangoStateLocal } from "./rango-state.js";
|
|
9
|
+
import type { AppShell, AppShellRef } from "./app-shell.js";
|
|
7
10
|
import * as React from "react";
|
|
8
11
|
import { startTransition } from "react";
|
|
9
12
|
import {
|
|
@@ -40,11 +43,6 @@ if (typeof Symbol.dispose === "undefined") {
|
|
|
40
43
|
(Symbol as any).dispose = Symbol("Symbol.dispose");
|
|
41
44
|
}
|
|
42
45
|
|
|
43
|
-
/** Get IDs of non-loader segments (layouts, routes, parallels). */
|
|
44
|
-
function getNonLoaderSegmentIds(segments: ResolvedSegment[]): string[] {
|
|
45
|
-
return segments.filter((s) => s.type !== "loader").map((s) => s.id);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
46
|
export { createNavigationTransaction };
|
|
49
47
|
|
|
50
48
|
/**
|
|
@@ -52,8 +50,13 @@ export { createNavigationTransaction };
|
|
|
52
50
|
*/
|
|
53
51
|
export interface NavigationBridgeConfigWithController extends NavigationBridgeConfig {
|
|
54
52
|
eventController: EventController;
|
|
55
|
-
/** RSC version from initial payload metadata */
|
|
53
|
+
/** RSC version from initial payload metadata (fallback when appShellRef is not provided) */
|
|
56
54
|
version?: string;
|
|
55
|
+
/**
|
|
56
|
+
* Live app-shell ref. When supplied, the bridge reads version/basename
|
|
57
|
+
* from this ref so cross-app navigations propagate correctly.
|
|
58
|
+
*/
|
|
59
|
+
appShellRef?: AppShellRef;
|
|
57
60
|
}
|
|
58
61
|
|
|
59
62
|
/**
|
|
@@ -72,8 +75,45 @@ export interface NavigationBridgeConfigWithController extends NavigationBridgeCo
|
|
|
72
75
|
export function createNavigationBridge(
|
|
73
76
|
config: NavigationBridgeConfigWithController,
|
|
74
77
|
): NavigationBridge {
|
|
75
|
-
const {
|
|
76
|
-
|
|
78
|
+
const {
|
|
79
|
+
store,
|
|
80
|
+
client,
|
|
81
|
+
eventController,
|
|
82
|
+
onUpdate,
|
|
83
|
+
renderSegments,
|
|
84
|
+
appShellRef,
|
|
85
|
+
} = config;
|
|
86
|
+
let version = config.version;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Replace the active app-shell snapshot atomically. Called by the partial
|
|
90
|
+
* updater when a response's routerId indicates the navigation crossed
|
|
91
|
+
* into a different app. Runs the local-only side-effects tied to
|
|
92
|
+
* app-shell fields (app version, rango-state namespace) so the new app
|
|
93
|
+
* owns them after the swap. Theme, warmup, and prefetch TTL are
|
|
94
|
+
* document-lifetime and are NOT touched here.
|
|
95
|
+
*/
|
|
96
|
+
function applyAppShell(next: AppShell): void {
|
|
97
|
+
if (appShellRef) {
|
|
98
|
+
appShellRef.update(next);
|
|
99
|
+
}
|
|
100
|
+
if (next.version !== undefined) {
|
|
101
|
+
version = next.version;
|
|
102
|
+
setAppVersion(next.version);
|
|
103
|
+
// Use the local-only setter — initRangoState writes the shared
|
|
104
|
+
// localStorage key and fires a storage event in other tabs still in
|
|
105
|
+
// the old app. setRangoStateLocal only mutates this tab's in-memory
|
|
106
|
+
// cache and rebinds it to the target app's routerId-scoped key,
|
|
107
|
+
// preserving the "local-only, no broadcast/rotation" contract for
|
|
108
|
+
// smooth app-switch transitions.
|
|
109
|
+
setRangoStateLocal(next.version, next.routerId);
|
|
110
|
+
}
|
|
111
|
+
// Cross-app: prior cache entries belong to a different app's segments.
|
|
112
|
+
// Drop them locally only — do NOT broadcast invalidation or rotate the
|
|
113
|
+
// shared X-Rango-State token, since other tabs still in the old app are
|
|
114
|
+
// unaffected by this tab's transition.
|
|
115
|
+
store.clearHistoryCacheLocal();
|
|
116
|
+
}
|
|
77
117
|
|
|
78
118
|
// Create shared partial updater
|
|
79
119
|
const fetchPartialUpdate = createPartialUpdater({
|
|
@@ -81,7 +121,8 @@ export function createNavigationBridge(
|
|
|
81
121
|
client,
|
|
82
122
|
onUpdate,
|
|
83
123
|
renderSegments,
|
|
84
|
-
version,
|
|
124
|
+
getVersion: () => version,
|
|
125
|
+
applyAppShell,
|
|
85
126
|
});
|
|
86
127
|
|
|
87
128
|
return {
|
|
@@ -265,18 +306,24 @@ export function createNavigationBridge(
|
|
|
265
306
|
// 2. routes that CAN be intercepted - we don't know if this navigation will intercept
|
|
266
307
|
// 3. when leaving intercept - we need fresh non-intercept segments from server
|
|
267
308
|
// 4. redirect-with-state - force re-render so hooks read fresh state
|
|
309
|
+
// 5. stale cache - server action invalidated it, need fresh data with loading state
|
|
268
310
|
const hasUsableCache =
|
|
269
311
|
cachedSegments &&
|
|
270
312
|
cachedSegments.length > 0 &&
|
|
271
313
|
!isInterceptOnlyCache(cachedSegments) &&
|
|
272
314
|
!hasInterceptCache &&
|
|
273
315
|
!isLeavingIntercept &&
|
|
316
|
+
!cached?.stale &&
|
|
274
317
|
!options?._skipCache;
|
|
275
318
|
|
|
319
|
+
// Forward navigations always await fetchPartialUpdate before rendering,
|
|
320
|
+
// so useNavigation should always report "loading". skipLoadingState is
|
|
321
|
+
// only used for popstate background revalidation (line ~526) where
|
|
322
|
+
// cached content renders instantly without a network wait.
|
|
276
323
|
const tx = createNavigationTransaction(store, eventController, url, {
|
|
277
324
|
...options,
|
|
278
325
|
state: resolvedState,
|
|
279
|
-
skipLoadingState:
|
|
326
|
+
skipLoadingState: false,
|
|
280
327
|
});
|
|
281
328
|
|
|
282
329
|
// REVALIDATE: Fetch fresh data from server
|
|
@@ -284,7 +331,7 @@ export function createNavigationBridge(
|
|
|
284
331
|
await fetchPartialUpdate(
|
|
285
332
|
url,
|
|
286
333
|
hasUsableCache
|
|
287
|
-
?
|
|
334
|
+
? cachedSegments!.map((s) => s.id)
|
|
288
335
|
: options?._skipCache
|
|
289
336
|
? [] // Action redirect: send no segments so server renders everything fresh
|
|
290
337
|
: undefined,
|
|
@@ -416,6 +463,15 @@ export function createNavigationBridge(
|
|
|
416
463
|
eventController.abortAllActions();
|
|
417
464
|
}
|
|
418
465
|
|
|
466
|
+
// Popstate that exits an intercept to a non-intercept destination. The
|
|
467
|
+
// fallback fetch path below needs `leave-intercept` mode so it filters
|
|
468
|
+
// the cached @modal segment from the request and forces a re-render —
|
|
469
|
+
// otherwise a cache-miss popstate whose server response has an empty
|
|
470
|
+
// diff hits the "no changes" branch in partial-update and the modal
|
|
471
|
+
// stays on screen.
|
|
472
|
+
const isLeavingIntercept =
|
|
473
|
+
!isIntercept && currentInterceptSource !== null;
|
|
474
|
+
|
|
419
475
|
// Compute history key from URL (with intercept suffix if applicable)
|
|
420
476
|
const historyKey = generateHistoryKey(url, { intercept: isIntercept });
|
|
421
477
|
|
|
@@ -452,6 +508,12 @@ export function createNavigationBridge(
|
|
|
452
508
|
store.setCurrentUrl(url);
|
|
453
509
|
store.setPath(new URL(url).pathname);
|
|
454
510
|
|
|
511
|
+
// Restore router identity from cache so subsequent navigations
|
|
512
|
+
// don't falsely detect an app switch.
|
|
513
|
+
if (cached?.routerId) {
|
|
514
|
+
store.setRouterId?.(cached.routerId);
|
|
515
|
+
}
|
|
516
|
+
|
|
455
517
|
// Render from cache - force await to skip loading fallbacks
|
|
456
518
|
try {
|
|
457
519
|
const root = await renderSegments(cachedSegments, {
|
|
@@ -477,6 +539,7 @@ export function createNavigationBridge(
|
|
|
477
539
|
cachedHandleData,
|
|
478
540
|
params: cachedParams,
|
|
479
541
|
},
|
|
542
|
+
scroll: { restore: true, isStreaming },
|
|
480
543
|
};
|
|
481
544
|
const hasTransition = cachedSegments.some((s) => s.transition);
|
|
482
545
|
if (hasTransition) {
|
|
@@ -490,14 +553,11 @@ export function createNavigationBridge(
|
|
|
490
553
|
onUpdate(popstateUpdate);
|
|
491
554
|
}
|
|
492
555
|
|
|
493
|
-
// Restore scroll position for back/forward navigation
|
|
494
|
-
handleNavigationEnd({ restore: true, isStreaming });
|
|
495
|
-
|
|
496
556
|
// SWR: If stale, trigger background revalidation
|
|
497
557
|
if (isStale) {
|
|
498
558
|
debugLog("[Browser] Cache is stale, background revalidating...");
|
|
499
559
|
// Background revalidation - don't await, just fire and forget
|
|
500
|
-
const segmentIds =
|
|
560
|
+
const segmentIds = cachedSegments.map((s) => s.id);
|
|
501
561
|
|
|
502
562
|
const tx = createNavigationTransaction(
|
|
503
563
|
store,
|
|
@@ -562,7 +622,11 @@ export function createNavigationBridge(
|
|
|
562
622
|
intercept: isIntercept,
|
|
563
623
|
interceptSourceUrl,
|
|
564
624
|
}),
|
|
565
|
-
isIntercept
|
|
625
|
+
isIntercept
|
|
626
|
+
? { type: "navigate", interceptSourceUrl }
|
|
627
|
+
: isLeavingIntercept
|
|
628
|
+
? { type: "leave-intercept" }
|
|
629
|
+
: undefined,
|
|
566
630
|
);
|
|
567
631
|
// Restore scroll position after fetch completes
|
|
568
632
|
handleNavigationEnd({ restore: true, isStreaming });
|
|
@@ -639,6 +703,16 @@ export function createNavigationBridge(
|
|
|
639
703
|
window.removeEventListener("pageshow", handlePageShow);
|
|
640
704
|
};
|
|
641
705
|
},
|
|
706
|
+
|
|
707
|
+
updateVersion(newVersion: string): void {
|
|
708
|
+
version = newVersion;
|
|
709
|
+
setAppVersion(newVersion);
|
|
710
|
+
store.clearHistoryCache();
|
|
711
|
+
},
|
|
712
|
+
|
|
713
|
+
updateAppShell(next: AppShell): void {
|
|
714
|
+
applyAppShell(next);
|
|
715
|
+
},
|
|
642
716
|
};
|
|
643
717
|
}
|
|
644
718
|
|
|
@@ -17,7 +17,12 @@ import {
|
|
|
17
17
|
emptyResponse,
|
|
18
18
|
teeWithCompletion,
|
|
19
19
|
} from "./response-adapter.js";
|
|
20
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
buildPrefetchKey,
|
|
22
|
+
buildSourceKey,
|
|
23
|
+
consumeInflightPrefetch,
|
|
24
|
+
consumePrefetch,
|
|
25
|
+
} from "./prefetch/cache.js";
|
|
21
26
|
|
|
22
27
|
/**
|
|
23
28
|
* Create a navigation client for fetching RSC payloads
|
|
@@ -26,8 +31,10 @@ import { buildPrefetchKey, consumePrefetch } from "./prefetch/cache.js";
|
|
|
26
31
|
* deserializing the response using the RSC runtime.
|
|
27
32
|
*
|
|
28
33
|
* Checks the in-memory prefetch cache before making a network request.
|
|
29
|
-
*
|
|
30
|
-
*
|
|
34
|
+
* Tries the source-scoped key first (populated when the server tagged
|
|
35
|
+
* the response as source-sensitive via `X-RSC-Prefetch-Scope: source`)
|
|
36
|
+
* and falls back to the Rango-state-keyed wildcard slot used for the
|
|
37
|
+
* common source-agnostic case.
|
|
31
38
|
*
|
|
32
39
|
* @param deps - RSC browser dependencies (createFromFetch)
|
|
33
40
|
* @returns NavigationClient instance
|
|
@@ -57,6 +64,7 @@ export function createNavigationClient(
|
|
|
57
64
|
staleRevalidation,
|
|
58
65
|
interceptSourceUrl,
|
|
59
66
|
version,
|
|
67
|
+
routerId,
|
|
60
68
|
hmr,
|
|
61
69
|
} = options;
|
|
62
70
|
|
|
@@ -84,50 +92,109 @@ export function createNavigationClient(
|
|
|
84
92
|
if (version) {
|
|
85
93
|
fetchUrl.searchParams.set("_rsc_v", version);
|
|
86
94
|
}
|
|
95
|
+
if (routerId) {
|
|
96
|
+
fetchUrl.searchParams.set("_rsc_rid", routerId);
|
|
97
|
+
}
|
|
87
98
|
|
|
88
|
-
// Check in-memory prefetch cache before making a network
|
|
89
|
-
//
|
|
90
|
-
//
|
|
99
|
+
// Check completed in-memory prefetch cache before making a network
|
|
100
|
+
// request. Try the source-scoped key first (populated when the server
|
|
101
|
+
// tagged the prefetch response as source-sensitive, e.g. intercepts,
|
|
102
|
+
// or when a Link opted in with `prefetchKey=":source"`), then fall
|
|
103
|
+
// back to the wildcard slot shared across source pages.
|
|
104
|
+
// Both keys embed the Rango state, so state rotation (deploy or
|
|
105
|
+
// server-action invalidation) auto-invalidates both scopes.
|
|
91
106
|
// Skip cache for stale revalidation (needs fresh data), HMR (needs
|
|
92
107
|
// fresh modules), and intercept contexts (source-dependent responses).
|
|
93
|
-
const
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
108
|
+
const canUsePrefetch = !staleRevalidation && !hmr && !interceptSourceUrl;
|
|
109
|
+
const rangoState = getRangoState();
|
|
110
|
+
const wildcardKey = buildPrefetchKey(rangoState, fetchUrl);
|
|
111
|
+
const cacheKey = buildSourceKey(rangoState, previousUrl, fetchUrl);
|
|
112
|
+
|
|
113
|
+
let cachedResponse: Response | null = null;
|
|
114
|
+
let hitKey: string | null = null;
|
|
115
|
+
if (canUsePrefetch) {
|
|
116
|
+
cachedResponse = consumePrefetch(cacheKey);
|
|
117
|
+
if (cachedResponse) {
|
|
118
|
+
hitKey = cacheKey;
|
|
119
|
+
} else {
|
|
120
|
+
cachedResponse = consumePrefetch(wildcardKey);
|
|
121
|
+
if (cachedResponse) hitKey = wildcardKey;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
98
124
|
|
|
125
|
+
let inflightResponsePromise: Promise<Response | null> | null = null;
|
|
126
|
+
if (canUsePrefetch && !cachedResponse) {
|
|
127
|
+
inflightResponsePromise = consumeInflightPrefetch(cacheKey);
|
|
128
|
+
if (inflightResponsePromise) {
|
|
129
|
+
hitKey = cacheKey;
|
|
130
|
+
} else {
|
|
131
|
+
inflightResponsePromise = consumeInflightPrefetch(wildcardKey);
|
|
132
|
+
if (inflightResponsePromise) hitKey = wildcardKey;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
99
135
|
// Track when the stream completes
|
|
100
136
|
let resolveStreamComplete: () => void;
|
|
101
137
|
const streamComplete = new Promise<void>((resolve) => {
|
|
102
138
|
resolveStreamComplete = resolve;
|
|
103
139
|
});
|
|
104
140
|
|
|
105
|
-
|
|
141
|
+
/**
|
|
142
|
+
* Validate RSC control headers on any response (fresh, cached, or
|
|
143
|
+
* in-flight). Handles version-mismatch reloads and server redirects.
|
|
144
|
+
* Returns the response unchanged when no control header is present.
|
|
145
|
+
*/
|
|
146
|
+
const validateRscHeaders = (
|
|
147
|
+
response: Response,
|
|
148
|
+
source: string,
|
|
149
|
+
): Response | Promise<Response> => {
|
|
150
|
+
// Version mismatch — server wants a full page reload
|
|
151
|
+
const reload = extractRscHeaderUrl(response, "X-RSC-Reload");
|
|
152
|
+
if (reload === "blocked") {
|
|
153
|
+
resolveStreamComplete();
|
|
154
|
+
return emptyResponse();
|
|
155
|
+
}
|
|
156
|
+
if (reload) {
|
|
157
|
+
if (tx) {
|
|
158
|
+
browserDebugLog(tx, `version mismatch, reloading (${source})`, {
|
|
159
|
+
reloadUrl: reload.url,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
window.location.href = reload.url;
|
|
163
|
+
// Block further processing — page is reloading
|
|
164
|
+
return new Promise<Response>(() => {});
|
|
165
|
+
}
|
|
106
166
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
167
|
+
// Server-side redirect without state: the server returned 204 with
|
|
168
|
+
// X-RSC-Redirect instead of a 3xx (which fetch would auto-follow
|
|
169
|
+
// to a URL rendering full HTML). Throw ServerRedirect so the
|
|
170
|
+
// navigation bridge catches it and re-navigates with _skipCache.
|
|
171
|
+
const redirect = extractRscHeaderUrl(response, "X-RSC-Redirect");
|
|
172
|
+
if (redirect === "blocked") {
|
|
173
|
+
resolveStreamComplete();
|
|
174
|
+
return emptyResponse();
|
|
110
175
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
176
|
+
if (redirect) {
|
|
177
|
+
if (tx) {
|
|
178
|
+
browserDebugLog(tx, `server redirect (${source})`, {
|
|
179
|
+
redirectUrl: redirect.url,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
resolveStreamComplete();
|
|
183
|
+
throw new ServerRedirect(redirect.url, undefined);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return response;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
/** Start a fresh navigation fetch (no cache / inflight hit). */
|
|
190
|
+
const doFreshFetch = (): Promise<Response> => {
|
|
124
191
|
if (tx) {
|
|
125
192
|
browserDebugLog(tx, "fetching", {
|
|
126
193
|
path: `${fetchUrl.pathname}${fetchUrl.search}`,
|
|
127
194
|
});
|
|
128
195
|
}
|
|
129
196
|
|
|
130
|
-
|
|
197
|
+
return fetch(fetchUrl, {
|
|
131
198
|
headers: {
|
|
132
199
|
"X-RSC-Router-Client-Path": previousUrl,
|
|
133
200
|
"X-Rango-State": getRangoState(),
|
|
@@ -139,55 +206,96 @@ export function createNavigationClient(
|
|
|
139
206
|
},
|
|
140
207
|
signal,
|
|
141
208
|
}).then((response) => {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
209
|
+
const validated = validateRscHeaders(response, "fetch");
|
|
210
|
+
if (validated instanceof Promise) return validated;
|
|
211
|
+
|
|
212
|
+
return teeWithCompletion(
|
|
213
|
+
validated,
|
|
214
|
+
() => {
|
|
215
|
+
if (tx) browserDebugLog(tx, "stream complete");
|
|
216
|
+
resolveStreamComplete();
|
|
217
|
+
},
|
|
218
|
+
signal,
|
|
219
|
+
);
|
|
220
|
+
});
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
let responsePromise: Promise<Response>;
|
|
224
|
+
|
|
225
|
+
if (cachedResponse) {
|
|
226
|
+
if (tx) {
|
|
227
|
+
browserDebugLog(tx, "prefetch cache hit", {
|
|
228
|
+
key: hitKey,
|
|
229
|
+
wildcard: hitKey === wildcardKey,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
responsePromise = Promise.resolve(cachedResponse).then((response) => {
|
|
233
|
+
const validated = validateRscHeaders(response, "prefetch cache");
|
|
234
|
+
if (validated instanceof Promise) return validated;
|
|
235
|
+
|
|
236
|
+
return teeWithCompletion(
|
|
237
|
+
validated,
|
|
238
|
+
() => {
|
|
239
|
+
if (tx) browserDebugLog(tx, "stream complete (from cache)");
|
|
240
|
+
resolveStreamComplete();
|
|
241
|
+
},
|
|
242
|
+
signal,
|
|
243
|
+
);
|
|
244
|
+
});
|
|
245
|
+
} else if (inflightResponsePromise) {
|
|
246
|
+
if (tx) {
|
|
247
|
+
browserDebugLog(tx, "reusing inflight prefetch", {
|
|
248
|
+
key: hitKey,
|
|
249
|
+
wildcard: hitKey === wildcardKey,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
const adoptedViaWildcard = hitKey === wildcardKey;
|
|
253
|
+
responsePromise = inflightResponsePromise.then(async (response) => {
|
|
254
|
+
if (!response) {
|
|
149
255
|
if (tx) {
|
|
150
|
-
browserDebugLog(tx, "
|
|
151
|
-
reloadUrl: reload.url,
|
|
152
|
-
});
|
|
256
|
+
browserDebugLog(tx, "inflight prefetch unavailable, refetching");
|
|
153
257
|
}
|
|
154
|
-
|
|
155
|
-
return new Promise<Response>(() => {});
|
|
258
|
+
return doFreshFetch();
|
|
156
259
|
}
|
|
157
260
|
|
|
158
|
-
//
|
|
159
|
-
//
|
|
160
|
-
//
|
|
161
|
-
//
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
}
|
|
167
|
-
if (redirect) {
|
|
261
|
+
// Cross-source safety: an inflight promise adopted via the
|
|
262
|
+
// wildcard key may turn out to be source-scoped (server emitted
|
|
263
|
+
// `X-RSC-Prefetch-Scope: source`), which means it was built for
|
|
264
|
+
// a different source page. Discard and refetch.
|
|
265
|
+
if (
|
|
266
|
+
adoptedViaWildcard &&
|
|
267
|
+
response.headers.get("x-rsc-prefetch-scope") === "source"
|
|
268
|
+
) {
|
|
168
269
|
if (tx) {
|
|
169
|
-
browserDebugLog(
|
|
170
|
-
|
|
171
|
-
|
|
270
|
+
browserDebugLog(
|
|
271
|
+
tx,
|
|
272
|
+
"wildcard inflight turned out source-scoped, refetching",
|
|
273
|
+
);
|
|
172
274
|
}
|
|
173
|
-
|
|
174
|
-
throw new ServerRedirect(redirect.url, undefined);
|
|
275
|
+
return doFreshFetch();
|
|
175
276
|
}
|
|
176
277
|
|
|
278
|
+
const validated = validateRscHeaders(response, "inflight prefetch");
|
|
279
|
+
if (validated instanceof Promise) return validated;
|
|
280
|
+
|
|
177
281
|
return teeWithCompletion(
|
|
178
|
-
|
|
282
|
+
validated,
|
|
179
283
|
() => {
|
|
180
|
-
if (tx)
|
|
284
|
+
if (tx) {
|
|
285
|
+
browserDebugLog(tx, "stream complete (from inflight prefetch)");
|
|
286
|
+
}
|
|
181
287
|
resolveStreamComplete();
|
|
182
288
|
},
|
|
183
289
|
signal,
|
|
184
290
|
);
|
|
185
291
|
});
|
|
292
|
+
} else {
|
|
293
|
+
responsePromise = doFreshFetch();
|
|
186
294
|
}
|
|
187
295
|
|
|
188
296
|
try {
|
|
189
|
-
// Deserialize RSC payload
|
|
190
297
|
const payload = await deps.createFromFetch<RscPayload>(responsePromise);
|
|
298
|
+
|
|
191
299
|
if (tx) {
|
|
192
300
|
browserDebugLog(tx, "response received", {
|
|
193
301
|
isPartial: payload.metadata?.isPartial,
|
|
@@ -12,7 +12,10 @@ import type {
|
|
|
12
12
|
ActionStateListener,
|
|
13
13
|
HandleData,
|
|
14
14
|
} from "./types.js";
|
|
15
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
clearPrefetchCache,
|
|
17
|
+
clearPrefetchCacheLocal,
|
|
18
|
+
} from "./prefetch/cache.js";
|
|
16
19
|
|
|
17
20
|
/**
|
|
18
21
|
* Default action state (idle with no payload)
|
|
@@ -28,9 +31,15 @@ const DEFAULT_ACTION_STATE: TrackedActionState = {
|
|
|
28
31
|
// Maximum number of history entries to cache (URLs visited)
|
|
29
32
|
const HISTORY_CACHE_SIZE = 20;
|
|
30
33
|
|
|
31
|
-
// Cache entry: [url-key, segments, stale, handleData?]
|
|
34
|
+
// Cache entry: [url-key, segments, stale, handleData?, routerId?]
|
|
32
35
|
// stale=true means the data may be outdated and should be revalidated on access
|
|
33
|
-
type HistoryCacheEntry = [
|
|
36
|
+
type HistoryCacheEntry = [
|
|
37
|
+
string,
|
|
38
|
+
ResolvedSegment[],
|
|
39
|
+
boolean,
|
|
40
|
+
HandleData?,
|
|
41
|
+
string?,
|
|
42
|
+
];
|
|
34
43
|
|
|
35
44
|
/**
|
|
36
45
|
* Shallow clone handleData to avoid reference sharing between cache entries.
|
|
@@ -258,6 +267,11 @@ export function createNavigationStore(
|
|
|
258
267
|
// Used to maintain intercept context during action revalidation
|
|
259
268
|
let interceptSourceUrl: string | null = null;
|
|
260
269
|
|
|
270
|
+
// Router identity - tracks which router is currently active.
|
|
271
|
+
// When this changes on a partial response, the client forces a full
|
|
272
|
+
// tree replacement instead of reconciling with stale segments.
|
|
273
|
+
let currentRouterId: string | undefined;
|
|
274
|
+
|
|
261
275
|
// Action state tracking (for useAction hook)
|
|
262
276
|
// Maps action function ID to its tracked state
|
|
263
277
|
const actionStates = new Map<string, TrackedActionState>();
|
|
@@ -324,6 +338,18 @@ export function createNavigationStore(
|
|
|
324
338
|
clearPrefetchCache();
|
|
325
339
|
}
|
|
326
340
|
|
|
341
|
+
/**
|
|
342
|
+
* Drop this tab's navigation + prefetch caches without broadcasting or
|
|
343
|
+
* rotating shared state. Used when the local session changes in a way that
|
|
344
|
+
* doesn't affect other tabs — e.g. this tab crosses into a different app
|
|
345
|
+
* via a cross-router navigation. Other tabs in the old app keep their
|
|
346
|
+
* caches and their X-Rango-State token.
|
|
347
|
+
*/
|
|
348
|
+
function clearCacheInternalLocal(): void {
|
|
349
|
+
historyCache.length = 0;
|
|
350
|
+
clearPrefetchCacheLocal();
|
|
351
|
+
}
|
|
352
|
+
|
|
327
353
|
/**
|
|
328
354
|
* Mark all cache entries as stale (internal - does not broadcast)
|
|
329
355
|
*/
|
|
@@ -571,10 +597,17 @@ export function createNavigationStore(
|
|
|
571
597
|
segments,
|
|
572
598
|
false,
|
|
573
599
|
clonedHandleData,
|
|
600
|
+
currentRouterId,
|
|
574
601
|
];
|
|
575
602
|
} else {
|
|
576
603
|
// Add new entry at the end (not stale)
|
|
577
|
-
historyCache.push([
|
|
604
|
+
historyCache.push([
|
|
605
|
+
historyKey,
|
|
606
|
+
segments,
|
|
607
|
+
false,
|
|
608
|
+
clonedHandleData,
|
|
609
|
+
currentRouterId,
|
|
610
|
+
]);
|
|
578
611
|
// Remove oldest entries if over limit
|
|
579
612
|
while (historyCache.length > cacheSize) {
|
|
580
613
|
historyCache.shift();
|
|
@@ -586,14 +619,22 @@ export function createNavigationStore(
|
|
|
586
619
|
* Get cached segments for a history entry
|
|
587
620
|
* Returns { segments, stale, handleData } or undefined if not cached
|
|
588
621
|
*/
|
|
589
|
-
getCachedSegments(
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
622
|
+
getCachedSegments(historyKey: string):
|
|
623
|
+
| {
|
|
624
|
+
segments: ResolvedSegment[];
|
|
625
|
+
stale: boolean;
|
|
626
|
+
handleData?: HandleData;
|
|
627
|
+
routerId?: string;
|
|
628
|
+
}
|
|
593
629
|
| undefined {
|
|
594
630
|
const entry = historyCache.find(([key]) => key === historyKey);
|
|
595
631
|
if (!entry) return undefined;
|
|
596
|
-
return {
|
|
632
|
+
return {
|
|
633
|
+
segments: entry[1],
|
|
634
|
+
stale: entry[2],
|
|
635
|
+
handleData: entry[3],
|
|
636
|
+
routerId: entry[4],
|
|
637
|
+
};
|
|
597
638
|
},
|
|
598
639
|
|
|
599
640
|
/**
|
|
@@ -621,6 +662,7 @@ export function createNavigationStore(
|
|
|
621
662
|
entry[1],
|
|
622
663
|
entry[2],
|
|
623
664
|
clonedHandleData,
|
|
665
|
+
entry[4], // preserve routerId
|
|
624
666
|
];
|
|
625
667
|
}
|
|
626
668
|
},
|
|
@@ -641,6 +683,15 @@ export function createNavigationStore(
|
|
|
641
683
|
clearCacheAndBroadcast();
|
|
642
684
|
},
|
|
643
685
|
|
|
686
|
+
/**
|
|
687
|
+
* Drop this tab's navigation + prefetch caches locally without
|
|
688
|
+
* broadcasting or rotating shared state. Intended for cross-app
|
|
689
|
+
* transitions where the session state diverges for this tab only.
|
|
690
|
+
*/
|
|
691
|
+
clearHistoryCacheLocal(): void {
|
|
692
|
+
clearCacheInternalLocal();
|
|
693
|
+
},
|
|
694
|
+
|
|
644
695
|
/**
|
|
645
696
|
* Mark cache as stale and broadcast to other tabs
|
|
646
697
|
* Called after server actions - allows SWR pattern for popstate
|
|
@@ -687,6 +738,14 @@ export function createNavigationStore(
|
|
|
687
738
|
interceptSourceUrl = url;
|
|
688
739
|
},
|
|
689
740
|
|
|
741
|
+
getRouterId(): string | undefined {
|
|
742
|
+
return currentRouterId;
|
|
743
|
+
},
|
|
744
|
+
|
|
745
|
+
setRouterId(id: string): void {
|
|
746
|
+
currentRouterId = id;
|
|
747
|
+
},
|
|
748
|
+
|
|
690
749
|
// ========================================================================
|
|
691
750
|
// UI Update Notifications
|
|
692
751
|
// ========================================================================
|