@rangojs/router 0.0.0-experimental.b02a2fec → 0.0.0-experimental.b30bbf02
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/README.md +112 -17
- package/dist/vite/index.js +1338 -462
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +7 -5
- package/skills/breadcrumbs/SKILL.md +3 -1
- package/skills/handler-use/SKILL.md +362 -0
- package/skills/hooks/SKILL.md +33 -20
- package/skills/intercept/SKILL.md +20 -0
- package/skills/layout/SKILL.md +22 -0
- package/skills/links/SKILL.md +90 -16
- package/skills/loader/SKILL.md +70 -3
- package/skills/middleware/SKILL.md +34 -3
- package/skills/migrate-nextjs/SKILL.md +562 -0
- package/skills/migrate-react-router/SKILL.md +769 -0
- package/skills/parallel/SKILL.md +66 -0
- package/skills/rango/SKILL.md +25 -22
- package/skills/response-routes/SKILL.md +8 -0
- package/skills/route/SKILL.md +24 -0
- package/skills/server-actions/SKILL.md +739 -0
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/typesafety/SKILL.md +3 -1
- package/src/browser/app-shell.ts +52 -0
- package/src/browser/event-controller.ts +44 -4
- package/src/browser/navigation-bridge.ts +71 -5
- package/src/browser/navigation-client.ts +64 -13
- package/src/browser/navigation-store.ts +25 -1
- package/src/browser/partial-update.ts +34 -3
- package/src/browser/prefetch/cache.ts +129 -21
- package/src/browser/prefetch/fetch.ts +148 -16
- package/src/browser/prefetch/queue.ts +36 -5
- package/src/browser/rango-state.ts +53 -13
- package/src/browser/react/Link.tsx +30 -2
- package/src/browser/react/NavigationProvider.tsx +70 -18
- package/src/browser/react/filter-segment-order.ts +51 -7
- 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 +8 -1
- package/src/browser/react/use-segments.ts +11 -8
- package/src/browser/rsc-router.tsx +34 -6
- package/src/browser/segment-reconciler.ts +36 -14
- package/src/browser/types.ts +19 -0
- package/src/build/route-trie.ts +50 -24
- package/src/cache/cf/cf-cache-store.ts +5 -7
- package/src/client.tsx +82 -174
- package/src/index.rsc.ts +3 -0
- package/src/index.ts +40 -9
- package/src/outlet-context.ts +1 -1
- package/src/response-utils.ts +28 -0
- package/src/reverse.ts +7 -3
- package/src/route-definition/dsl-helpers.ts +175 -23
- package/src/route-definition/helpers-types.ts +63 -14
- package/src/route-definition/resolve-handler-use.ts +6 -0
- package/src/route-types.ts +7 -0
- package/src/router/handler-context.ts +24 -4
- package/src/router/lazy-includes.ts +6 -6
- package/src/router/loader-resolution.ts +3 -0
- package/src/router/manifest.ts +22 -13
- package/src/router/match-api.ts +4 -3
- package/src/router/match-handlers.ts +1 -0
- package/src/router/match-result.ts +21 -2
- package/src/router/middleware-types.ts +2 -22
- package/src/router/middleware.ts +54 -7
- package/src/router/pattern-matching.ts +87 -17
- package/src/router/revalidation.ts +15 -1
- package/src/router/segment-resolution/fresh.ts +8 -0
- package/src/router/segment-resolution/revalidation.ts +128 -100
- package/src/router/trie-matching.ts +18 -13
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +1 -2
- package/src/rsc/handler.ts +8 -4
- package/src/rsc/helpers.ts +69 -41
- package/src/rsc/progressive-enhancement.ts +4 -0
- package/src/rsc/response-route-handler.ts +14 -1
- package/src/rsc/rsc-rendering.ts +10 -0
- package/src/rsc/server-action.ts +4 -0
- package/src/rsc/types.ts +6 -0
- package/src/segment-content-promise.ts +67 -0
- package/src/segment-loader-promise.ts +122 -0
- package/src/segment-system.tsx +11 -61
- package/src/server/context.ts +26 -3
- package/src/server/request-context.ts +10 -42
- package/src/ssr/index.tsx +5 -1
- package/src/types/handler-context.ts +12 -39
- package/src/types/loader-types.ts +5 -6
- package/src/types/request-scope.ts +126 -0
- package/src/types/route-entry.ts +11 -0
- package/src/types/segments.ts +17 -1
- package/src/urls/include-helper.ts +24 -14
- package/src/urls/path-helper-types.ts +30 -4
- package/src/urls/response-types.ts +2 -10
- package/src/vite/debug.ts +184 -0
- package/src/vite/discovery/discover-routers.ts +31 -3
- package/src/vite/discovery/gate-state.ts +171 -0
- package/src/vite/discovery/prerender-collection.ts +48 -1
- package/src/vite/discovery/self-gen-tracking.ts +27 -1
- package/src/vite/plugins/cjs-to-esm.ts +5 -0
- package/src/vite/plugins/client-ref-dedup.ts +16 -0
- package/src/vite/plugins/client-ref-hashing.ts +16 -4
- 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 +52 -28
- package/src/vite/plugins/expose-ids/router-transform.ts +20 -3
- package/src/vite/plugins/expose-internal-ids.ts +516 -486
- package/src/vite/plugins/performance-tracks.ts +17 -9
- package/src/vite/plugins/use-cache-transform.ts +56 -43
- package/src/vite/plugins/version-injector.ts +37 -11
- package/src/vite/rango.ts +49 -14
- package/src/vite/router-discovery.ts +558 -53
- package/src/vite/utils/banner.ts +1 -1
- package/src/vite/utils/package-resolution.ts +41 -1
- package/src/vite/utils/prerender-utils.ts +20 -6
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
} from "./response-adapter.js";
|
|
20
20
|
import {
|
|
21
21
|
buildPrefetchKey,
|
|
22
|
+
buildSourceKey,
|
|
22
23
|
consumeInflightPrefetch,
|
|
23
24
|
consumePrefetch,
|
|
24
25
|
} from "./prefetch/cache.js";
|
|
@@ -30,8 +31,10 @@ import {
|
|
|
30
31
|
* deserializing the response using the RSC runtime.
|
|
31
32
|
*
|
|
32
33
|
* Checks the in-memory prefetch cache before making a network request.
|
|
33
|
-
*
|
|
34
|
-
*
|
|
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.
|
|
35
38
|
*
|
|
36
39
|
* @param deps - RSC browser dependencies (createFromFetch)
|
|
37
40
|
* @returns NavigationClient instance
|
|
@@ -93,18 +96,42 @@ export function createNavigationClient(
|
|
|
93
96
|
fetchUrl.searchParams.set("_rsc_rid", routerId);
|
|
94
97
|
}
|
|
95
98
|
|
|
96
|
-
// Check completed in-memory prefetch cache before making a network
|
|
97
|
-
//
|
|
98
|
-
//
|
|
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.
|
|
99
106
|
// Skip cache for stale revalidation (needs fresh data), HMR (needs
|
|
100
107
|
// fresh modules), and intercept contexts (source-dependent responses).
|
|
101
|
-
//
|
|
102
108
|
const canUsePrefetch = !staleRevalidation && !hmr && !interceptSourceUrl;
|
|
103
|
-
const
|
|
104
|
-
const
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
|
|
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
|
+
}
|
|
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
|
+
}
|
|
108
135
|
// Track when the stream completes
|
|
109
136
|
let resolveStreamComplete: () => void;
|
|
110
137
|
const streamComplete = new Promise<void>((resolve) => {
|
|
@@ -197,7 +224,10 @@ export function createNavigationClient(
|
|
|
197
224
|
|
|
198
225
|
if (cachedResponse) {
|
|
199
226
|
if (tx) {
|
|
200
|
-
browserDebugLog(tx, "prefetch cache hit", {
|
|
227
|
+
browserDebugLog(tx, "prefetch cache hit", {
|
|
228
|
+
key: hitKey,
|
|
229
|
+
wildcard: hitKey === wildcardKey,
|
|
230
|
+
});
|
|
201
231
|
}
|
|
202
232
|
responsePromise = Promise.resolve(cachedResponse).then((response) => {
|
|
203
233
|
const validated = validateRscHeaders(response, "prefetch cache");
|
|
@@ -214,8 +244,12 @@ export function createNavigationClient(
|
|
|
214
244
|
});
|
|
215
245
|
} else if (inflightResponsePromise) {
|
|
216
246
|
if (tx) {
|
|
217
|
-
browserDebugLog(tx, "reusing inflight prefetch", {
|
|
247
|
+
browserDebugLog(tx, "reusing inflight prefetch", {
|
|
248
|
+
key: hitKey,
|
|
249
|
+
wildcard: hitKey === wildcardKey,
|
|
250
|
+
});
|
|
218
251
|
}
|
|
252
|
+
const adoptedViaWildcard = hitKey === wildcardKey;
|
|
219
253
|
responsePromise = inflightResponsePromise.then(async (response) => {
|
|
220
254
|
if (!response) {
|
|
221
255
|
if (tx) {
|
|
@@ -224,6 +258,23 @@ export function createNavigationClient(
|
|
|
224
258
|
return doFreshFetch();
|
|
225
259
|
}
|
|
226
260
|
|
|
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
|
+
) {
|
|
269
|
+
if (tx) {
|
|
270
|
+
browserDebugLog(
|
|
271
|
+
tx,
|
|
272
|
+
"wildcard inflight turned out source-scoped, refetching",
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
return doFreshFetch();
|
|
276
|
+
}
|
|
277
|
+
|
|
227
278
|
const validated = validateRscHeaders(response, "inflight prefetch");
|
|
228
279
|
if (validated instanceof Promise) return validated;
|
|
229
280
|
|
|
@@ -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)
|
|
@@ -335,6 +338,18 @@ export function createNavigationStore(
|
|
|
335
338
|
clearPrefetchCache();
|
|
336
339
|
}
|
|
337
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
|
+
|
|
338
353
|
/**
|
|
339
354
|
* Mark all cache entries as stale (internal - does not broadcast)
|
|
340
355
|
*/
|
|
@@ -668,6 +683,15 @@ export function createNavigationStore(
|
|
|
668
683
|
clearCacheAndBroadcast();
|
|
669
684
|
},
|
|
670
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
|
+
|
|
671
695
|
/**
|
|
672
696
|
* Mark cache as stale and broadcast to other tabs
|
|
673
697
|
* Called after server actions - allows SWR pattern for popstate
|
|
@@ -41,6 +41,13 @@ export interface PartialUpdateConfig {
|
|
|
41
41
|
) => Promise<ReactNode> | ReactNode;
|
|
42
42
|
/** RSC version getter — returns the current version (may change after HMR) */
|
|
43
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;
|
|
44
51
|
}
|
|
45
52
|
|
|
46
53
|
/**
|
|
@@ -110,6 +117,7 @@ export function createPartialUpdater(
|
|
|
110
117
|
onUpdate,
|
|
111
118
|
renderSegments,
|
|
112
119
|
getVersion = () => undefined,
|
|
120
|
+
applyAppShell,
|
|
113
121
|
} = config;
|
|
114
122
|
|
|
115
123
|
/**
|
|
@@ -167,9 +175,16 @@ export function createPartialUpdater(
|
|
|
167
175
|
segments = segmentIds ?? segmentState.currentSegmentIds;
|
|
168
176
|
}
|
|
169
177
|
|
|
170
|
-
// 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.
|
|
171
184
|
const previousUrl =
|
|
172
|
-
|
|
185
|
+
mode.type === "leave-intercept"
|
|
186
|
+
? segmentState.currentUrl || tx.currentUrl
|
|
187
|
+
: interceptSourceUrl || tx.currentUrl || segmentState.currentUrl;
|
|
173
188
|
|
|
174
189
|
debugLog(`\n[Browser] >>> NAVIGATION`);
|
|
175
190
|
debugLog(`[Browser] From: ${previousUrl}`);
|
|
@@ -188,6 +203,11 @@ export function createPartialUpdater(
|
|
|
188
203
|
targetCache && targetCache.length > 0
|
|
189
204
|
? targetCache
|
|
190
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
|
+
);
|
|
191
211
|
|
|
192
212
|
// Fetch partial payload (no abort signal - RSC doesn't support it well)
|
|
193
213
|
let fetchResult: Awaited<ReturnType<NavigationClient["fetchPartial"]>>;
|
|
@@ -216,7 +236,12 @@ export function createPartialUpdater(
|
|
|
216
236
|
// Detect app switch: if routerId changed, the navigation crossed into
|
|
217
237
|
// a different router (e.g., via host router path mount). Downgrade
|
|
218
238
|
// partial to full so the entire tree is replaced without reconciliation
|
|
219
|
-
// against stale segments from the previous app
|
|
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.
|
|
220
245
|
if (payload.metadata?.routerId) {
|
|
221
246
|
const prevRouterId = store.getRouterId?.();
|
|
222
247
|
if (prevRouterId && prevRouterId !== payload.metadata.routerId) {
|
|
@@ -224,6 +249,12 @@ export function createPartialUpdater(
|
|
|
224
249
|
`[Browser] App switch detected (${prevRouterId} → ${payload.metadata.routerId}), forcing full update`,
|
|
225
250
|
);
|
|
226
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
|
+
});
|
|
227
258
|
}
|
|
228
259
|
store.setRouterId?.(payload.metadata.routerId);
|
|
229
260
|
}
|
|
@@ -2,13 +2,27 @@
|
|
|
2
2
|
* Prefetch Cache
|
|
3
3
|
*
|
|
4
4
|
* In-memory cache storing prefetched Response objects for instant cache hits
|
|
5
|
-
* on subsequent navigation.
|
|
6
|
-
*
|
|
7
|
-
*
|
|
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.
|
|
8
18
|
*
|
|
9
19
|
* Also tracks in-flight prefetch promises. Each promise resolves to the
|
|
10
20
|
* navigation branch of a tee'd Response, allowing navigation to adopt a
|
|
11
|
-
* still-downloading prefetch without reparsing or buffering the body.
|
|
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.
|
|
12
26
|
*
|
|
13
27
|
* Replaces the previous browser HTTP cache approach which was unreliable
|
|
14
28
|
* due to response draining race conditions and browser inconsistencies.
|
|
@@ -55,19 +69,71 @@ const inflight = new Set<string>();
|
|
|
55
69
|
*/
|
|
56
70
|
const inflightPromises = new Map<string, Promise<Response | null>>();
|
|
57
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[]>();
|
|
79
|
+
|
|
58
80
|
// Generation counter incremented on each clearPrefetchCache(). Fetches that
|
|
59
81
|
// started before a clear carry a stale generation and must not store their
|
|
60
82
|
// response (the data may be stale due to a server action invalidation).
|
|
61
83
|
let generation = 0;
|
|
62
84
|
|
|
63
85
|
/**
|
|
64
|
-
* Build a
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
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.
|
|
68
129
|
*/
|
|
69
|
-
|
|
70
|
-
|
|
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
|
+
}
|
|
71
137
|
}
|
|
72
138
|
|
|
73
139
|
/**
|
|
@@ -110,21 +176,27 @@ export function consumePrefetch(key: string): Response | null {
|
|
|
110
176
|
* in-flight for this key. The returned Promise resolves to the buffered
|
|
111
177
|
* Response (or null if the fetch failed/was aborted).
|
|
112
178
|
*
|
|
113
|
-
* One-time consumption: the promise entry is removed
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
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()`.
|
|
120
188
|
*/
|
|
121
189
|
export function consumeInflightPrefetch(
|
|
122
190
|
key: string,
|
|
123
191
|
): Promise<Response | null> | null {
|
|
124
192
|
const promise = inflightPromises.get(key);
|
|
125
193
|
if (!promise) return null;
|
|
126
|
-
// Remove the promise
|
|
127
|
-
|
|
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));
|
|
128
200
|
return promise;
|
|
129
201
|
}
|
|
130
202
|
|
|
@@ -183,9 +255,28 @@ export function setInflightPromise(
|
|
|
183
255
|
inflightPromises.set(key, promise);
|
|
184
256
|
}
|
|
185
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
|
+
|
|
186
274
|
export function clearPrefetchInflight(key: string): void {
|
|
187
|
-
|
|
188
|
-
|
|
275
|
+
forEachAlias(key, (k) => {
|
|
276
|
+
inflight.delete(k);
|
|
277
|
+
inflightPromises.delete(k);
|
|
278
|
+
inflightAliases.delete(k);
|
|
279
|
+
});
|
|
189
280
|
}
|
|
190
281
|
|
|
191
282
|
/**
|
|
@@ -200,7 +291,24 @@ export function clearPrefetchCache(): void {
|
|
|
200
291
|
generation++;
|
|
201
292
|
inflight.clear();
|
|
202
293
|
inflightPromises.clear();
|
|
294
|
+
inflightAliases.clear();
|
|
203
295
|
cache.clear();
|
|
204
296
|
abortAllPrefetches();
|
|
205
297
|
invalidateRangoState();
|
|
206
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
|
+
}
|