@rangojs/router 0.0.0-experimental.dfdb0387 → 0.0.0-experimental.e16b7c00
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 +120 -25
- package/dist/bin/rango.js +147 -57
- package/dist/vite/index.js +2106 -842
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +13 -8
- package/skills/breadcrumbs/SKILL.md +3 -1
- package/skills/bundle-analysis/SKILL.md +159 -0
- package/skills/cache-guide/SKILL.md +222 -30
- package/skills/caching/SKILL.md +188 -8
- package/skills/composability/SKILL.md +27 -2
- package/skills/document-cache/SKILL.md +78 -55
- package/skills/handler-use/SKILL.md +364 -0
- package/skills/hooks/SKILL.md +229 -20
- package/skills/host-router/SKILL.md +45 -20
- package/skills/i18n/SKILL.md +276 -0
- package/skills/intercept/SKILL.md +46 -4
- package/skills/layout/SKILL.md +28 -7
- package/skills/links/SKILL.md +247 -17
- package/skills/loader/SKILL.md +219 -9
- package/skills/middleware/SKILL.md +47 -12
- package/skills/migrate-nextjs/SKILL.md +582 -0
- package/skills/migrate-react-router/SKILL.md +769 -0
- package/skills/mime-routes/SKILL.md +27 -0
- package/skills/observability/SKILL.md +137 -0
- package/skills/parallel/SKILL.md +71 -6
- package/skills/prerender/SKILL.md +14 -33
- package/skills/rango/SKILL.md +236 -22
- package/skills/react-compiler/SKILL.md +168 -0
- package/skills/response-routes/SKILL.md +66 -9
- package/skills/route/SKILL.md +57 -4
- package/skills/router-setup/SKILL.md +3 -3
- package/skills/server-actions/SKILL.md +751 -0
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/typesafety/SKILL.md +319 -27
- package/skills/use-cache/SKILL.md +36 -5
- package/skills/view-transitions/SKILL.md +294 -0
- package/src/__augment-tests__/augment.ts +81 -0
- package/src/__augment-tests__/augmented.check.ts +117 -0
- package/src/browser/action-coordinator.ts +53 -36
- package/src/browser/app-shell.ts +52 -0
- package/src/browser/event-controller.ts +86 -70
- package/src/browser/history-state.ts +21 -0
- package/src/browser/index.ts +3 -3
- package/src/browser/navigation-bridge.ts +86 -11
- package/src/browser/navigation-client.ts +45 -25
- package/src/browser/navigation-store.ts +32 -9
- package/src/browser/navigation-transaction.ts +10 -28
- package/src/browser/partial-update.ts +61 -28
- package/src/browser/prefetch/cache.ts +124 -26
- package/src/browser/prefetch/fetch.ts +129 -37
- package/src/browser/prefetch/queue.ts +36 -5
- package/src/browser/rango-state.ts +53 -13
- package/src/browser/react/Link.tsx +18 -13
- package/src/browser/react/NavigationProvider.tsx +72 -31
- package/src/browser/react/filter-segment-order.ts +51 -7
- package/src/browser/react/index.ts +3 -0
- package/src/browser/react/location-state-shared.ts +175 -4
- package/src/browser/react/location-state.ts +39 -13
- package/src/browser/react/use-handle.ts +17 -9
- package/src/browser/react/use-navigation.ts +22 -2
- package/src/browser/react/use-params.ts +20 -8
- package/src/browser/react/use-reverse.ts +106 -0
- package/src/browser/react/use-router.ts +22 -2
- package/src/browser/react/use-segments.ts +11 -8
- package/src/browser/response-adapter.ts +25 -0
- package/src/browser/rsc-router.tsx +64 -22
- package/src/browser/scroll-restoration.ts +22 -14
- package/src/browser/segment-reconciler.ts +10 -14
- package/src/browser/segment-structure-assert.ts +2 -2
- package/src/browser/server-action-bridge.ts +23 -30
- package/src/browser/types.ts +21 -0
- package/src/build/collect-fallback-refs.ts +107 -0
- package/src/build/generate-manifest.ts +60 -35
- package/src/build/generate-route-types.ts +2 -0
- package/src/build/index.ts +2 -0
- package/src/build/route-trie.ts +52 -25
- package/src/build/route-types/codegen.ts +4 -4
- package/src/build/route-types/include-resolution.ts +1 -1
- package/src/build/route-types/per-module-writer.ts +7 -4
- package/src/build/route-types/router-processing.ts +55 -14
- package/src/build/route-types/scan-filter.ts +1 -1
- package/src/build/route-types/source-scan.ts +118 -0
- package/src/build/runtime-discovery.ts +9 -20
- package/src/cache/cache-error.ts +104 -0
- package/src/cache/cache-policy.ts +95 -1
- package/src/cache/cache-runtime.ts +79 -13
- package/src/cache/cache-scope.ts +77 -46
- package/src/cache/cache-tag.ts +135 -0
- package/src/cache/cf/cf-cache-store.ts +1067 -176
- package/src/cache/cf/index.ts +4 -1
- package/src/cache/document-cache.ts +59 -7
- package/src/cache/index.ts +6 -0
- package/src/cache/memory-segment-store.ts +158 -14
- package/src/cache/tag-invalidation.ts +206 -0
- package/src/cache/types.ts +27 -0
- package/src/client.rsc.tsx +3 -0
- package/src/client.tsx +92 -182
- package/src/context-var.ts +5 -5
- package/src/decode-loader-results.ts +36 -0
- package/src/errors.ts +30 -1
- package/src/handle.ts +4 -6
- package/src/host/index.ts +2 -2
- package/src/host/router.ts +129 -57
- package/src/host/types.ts +31 -2
- package/src/host/utils.ts +1 -1
- package/src/href-client.ts +140 -20
- package/src/index.rsc.ts +16 -4
- package/src/index.ts +65 -15
- package/src/loader-store.ts +500 -0
- package/src/loader.rsc.ts +2 -5
- package/src/loader.ts +3 -10
- package/src/missing-id-error.ts +68 -0
- package/src/outlet-context.ts +1 -1
- package/src/prerender.ts +4 -4
- package/src/response-utils.ts +37 -0
- package/src/reverse.ts +65 -36
- package/src/route-content-wrapper.tsx +6 -28
- package/src/route-definition/dsl-helpers.ts +384 -257
- package/src/route-definition/helper-factories.ts +29 -139
- package/src/route-definition/helpers-types.ts +100 -28
- package/src/route-definition/resolve-handler-use.ts +6 -0
- package/src/route-definition/use-item-types.ts +32 -0
- package/src/route-types.ts +26 -41
- package/src/router/content-negotiation.ts +15 -2
- package/src/router/error-handling.ts +1 -1
- package/src/router/handler-context.ts +21 -38
- package/src/router/intercept-resolution.ts +4 -18
- package/src/router/lazy-includes.ts +8 -8
- package/src/router/loader-resolution.ts +19 -2
- 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-middleware/cache-lookup.ts +46 -92
- package/src/router/match-middleware/cache-store.ts +3 -2
- package/src/router/match-result.ts +53 -32
- package/src/router/metrics.ts +1 -1
- package/src/router/middleware-types.ts +15 -26
- package/src/router/middleware.ts +99 -84
- package/src/router/pattern-matching.ts +101 -17
- package/src/router/prerender-match.ts +3 -1
- package/src/router/preview-match.ts +3 -1
- package/src/router/request-classification.ts +4 -28
- package/src/router/revalidation.ts +58 -2
- package/src/router/router-interfaces.ts +45 -28
- package/src/router/router-options.ts +25 -1
- package/src/router/router-registry.ts +2 -5
- package/src/router/segment-resolution/fresh.ts +27 -6
- package/src/router/segment-resolution/loader-cache.ts +8 -17
- package/src/router/segment-resolution/revalidation.ts +147 -106
- package/src/router/segment-resolution/view-transition-default.ts +36 -0
- package/src/router/substitute-pattern-params.ts +56 -0
- package/src/router/trie-matching.ts +18 -13
- package/src/router/types.ts +8 -0
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +23 -18
- package/src/rsc/handler-context.ts +2 -2
- package/src/rsc/handler.ts +38 -70
- package/src/rsc/helpers.ts +72 -43
- package/src/rsc/index.ts +1 -1
- package/src/rsc/origin-guard.ts +28 -10
- package/src/rsc/progressive-enhancement.ts +4 -0
- package/src/rsc/response-route-handler.ts +54 -54
- package/src/rsc/rsc-rendering.ts +35 -51
- package/src/rsc/runtime-warnings.ts +9 -10
- package/src/rsc/server-action.ts +17 -37
- package/src/rsc/ssr-setup.ts +16 -0
- package/src/rsc/types.ts +8 -2
- package/src/search-params.ts +4 -4
- package/src/segment-content-promise.ts +67 -0
- package/src/segment-loader-promise.ts +122 -0
- package/src/segment-system.tsx +132 -116
- package/src/serialize.ts +243 -0
- package/src/server/context.ts +143 -53
- package/src/server/cookie-store.ts +28 -4
- package/src/server/request-context.ts +46 -44
- package/src/ssr/index.tsx +5 -1
- package/src/static-handler.ts +1 -1
- package/src/types/cache-types.ts +13 -4
- package/src/types/error-types.ts +5 -1
- package/src/types/global-namespace.ts +39 -26
- package/src/types/handler-context.ts +68 -50
- package/src/types/index.ts +1 -0
- 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 +35 -2
- package/src/urls/include-helper.ts +34 -67
- package/src/urls/index.ts +0 -3
- package/src/urls/path-helper-types.ts +41 -7
- package/src/urls/path-helper.ts +17 -52
- package/src/urls/pattern-types.ts +36 -19
- package/src/urls/response-types.ts +22 -29
- package/src/urls/type-extraction.ts +26 -116
- package/src/urls/urls-function.ts +1 -5
- package/src/use-loader.tsx +413 -42
- package/src/vite/debug.ts +185 -0
- package/src/vite/discovery/bundle-postprocess.ts +6 -6
- package/src/vite/discovery/discover-routers.ts +101 -51
- package/src/vite/discovery/discovery-errors.ts +194 -0
- package/src/vite/discovery/gate-state.ts +171 -0
- package/src/vite/discovery/prerender-collection.ts +67 -26
- package/src/vite/discovery/route-types-writer.ts +40 -84
- package/src/vite/discovery/self-gen-tracking.ts +27 -1
- package/src/vite/discovery/state.ts +33 -0
- package/src/vite/discovery/virtual-module-codegen.ts +13 -23
- package/src/vite/index.ts +2 -0
- package/src/vite/plugin-types.ts +67 -0
- package/src/vite/plugins/cjs-to-esm.ts +8 -7
- package/src/vite/plugins/client-ref-dedup.ts +16 -0
- package/src/vite/plugins/client-ref-hashing.ts +28 -5
- 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 +54 -30
- package/src/vite/plugins/expose-id-utils.ts +12 -8
- package/src/vite/plugins/expose-ids/export-analysis.ts +100 -20
- package/src/vite/plugins/expose-ids/handler-transform.ts +8 -61
- package/src/vite/plugins/expose-ids/loader-transform.ts +3 -5
- package/src/vite/plugins/expose-ids/router-transform.ts +20 -3
- package/src/vite/plugins/expose-internal-ids.ts +496 -486
- package/src/vite/plugins/performance-tracks.ts +29 -25
- package/src/vite/plugins/use-cache-transform.ts +65 -50
- package/src/vite/plugins/version-injector.ts +39 -23
- package/src/vite/plugins/version-plugin.ts +59 -2
- package/src/vite/plugins/virtual-entries.ts +2 -2
- package/src/vite/rango.ts +116 -29
- package/src/vite/router-discovery.ts +750 -100
- package/src/vite/utils/ast-handler-extract.ts +15 -15
- package/src/vite/utils/banner.ts +1 -1
- package/src/vite/utils/bundle-analysis.ts +4 -2
- package/src/vite/utils/client-chunks.ts +190 -0
- package/src/vite/utils/forward-user-plugins.ts +193 -0
- package/src/vite/utils/manifest-utils.ts +21 -5
- package/src/vite/utils/package-resolution.ts +41 -1
- package/src/vite/utils/prerender-utils.ts +21 -6
- package/src/vite/utils/shared-utils.ts +107 -26
- package/src/browser/action-response-classifier.ts +0 -99
|
@@ -13,9 +13,10 @@
|
|
|
13
13
|
|
|
14
14
|
import {
|
|
15
15
|
buildPrefetchKey,
|
|
16
|
+
buildSourceKey,
|
|
16
17
|
hasPrefetch,
|
|
17
18
|
markPrefetchInflight,
|
|
18
|
-
|
|
19
|
+
setInflightPromiseWithAliases,
|
|
19
20
|
storePrefetch,
|
|
20
21
|
clearPrefetchInflight,
|
|
21
22
|
currentGeneration,
|
|
@@ -25,6 +26,23 @@ import { enqueuePrefetch } from "./queue.js";
|
|
|
25
26
|
import { shouldPrefetch } from "./policy.js";
|
|
26
27
|
import { debugLog } from "../logging.js";
|
|
27
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Check if a URL resolves to the current page (same pathname + search).
|
|
31
|
+
* Used to prevent same-page prefetching, which produces a trivial diff
|
|
32
|
+
* that would corrupt the (default wildcard) prefetch cache entry.
|
|
33
|
+
*/
|
|
34
|
+
function isSamePage(url: string): boolean {
|
|
35
|
+
try {
|
|
36
|
+
const target = new URL(url, window.location.origin);
|
|
37
|
+
return (
|
|
38
|
+
target.pathname + target.search ===
|
|
39
|
+
window.location.pathname + window.location.search
|
|
40
|
+
);
|
|
41
|
+
} catch {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
28
46
|
/**
|
|
29
47
|
* Build an RSC partial URL for prefetching.
|
|
30
48
|
* Includes _rsc_segments so the server can diff against currently mounted
|
|
@@ -64,14 +82,33 @@ function buildPrefetchUrl(
|
|
|
64
82
|
* one branch in the in-memory cache. The returned Promise resolves to the
|
|
65
83
|
* sibling navigation branch (or null on failure) so navigation can safely
|
|
66
84
|
* reuse an in-flight prefetch via consumeInflightPrefetch().
|
|
85
|
+
*
|
|
86
|
+
* Inflight + storage key selection:
|
|
87
|
+
*
|
|
88
|
+
* - `forceSourceScope` (Link opted in with `prefetchKey=":source"`): single
|
|
89
|
+
* inflight registration under `sourceKey`; response stored under
|
|
90
|
+
* `sourceKey`. No wildcard leak is possible.
|
|
91
|
+
*
|
|
92
|
+
* - Otherwise: dual inflight registration under both `wildcardKey` and
|
|
93
|
+
* `sourceKey` so same-source navigations adopt directly via their own
|
|
94
|
+
* source key. Storage key is chosen at response time from the
|
|
95
|
+
* `X-RSC-Prefetch-Scope` header — `"source"` → `sourceKey` (intercept
|
|
96
|
+
* modals etc.), anything else → `wildcardKey`. Cross-source navigations
|
|
97
|
+
* that adopted via `wildcardKey` must bail out in `navigation-client.ts`
|
|
98
|
+
* if the adopted response turns out to be source-scoped.
|
|
67
99
|
*/
|
|
68
100
|
function executePrefetchFetch(
|
|
69
|
-
|
|
101
|
+
wildcardKey: string,
|
|
102
|
+
sourceKey: string,
|
|
70
103
|
fetchUrl: string,
|
|
104
|
+
forceSourceScope: boolean,
|
|
71
105
|
signal?: AbortSignal,
|
|
72
106
|
): Promise<Response | null> {
|
|
73
107
|
const gen = currentGeneration();
|
|
74
|
-
|
|
108
|
+
const inflightKeys = forceSourceScope
|
|
109
|
+
? [sourceKey]
|
|
110
|
+
: [wildcardKey, sourceKey];
|
|
111
|
+
for (const k of inflightKeys) markPrefetchInflight(k);
|
|
75
112
|
|
|
76
113
|
const promise: Promise<Response | null> = fetch(fetchUrl, {
|
|
77
114
|
priority: "low" as RequestPriority,
|
|
@@ -93,98 +130,153 @@ function executePrefetchFetch(
|
|
|
93
130
|
status: response.status,
|
|
94
131
|
statusText: response.statusText,
|
|
95
132
|
};
|
|
96
|
-
|
|
133
|
+
let storageKey: string;
|
|
134
|
+
if (forceSourceScope) {
|
|
135
|
+
storageKey = sourceKey;
|
|
136
|
+
} else {
|
|
137
|
+
const scope = response.headers.get("x-rsc-prefetch-scope");
|
|
138
|
+
storageKey = scope === "source" ? sourceKey : wildcardKey;
|
|
139
|
+
}
|
|
140
|
+
storePrefetch(storageKey, new Response(cacheStream, responseInit), gen);
|
|
97
141
|
return new Response(navStream, responseInit);
|
|
98
142
|
})
|
|
99
143
|
.catch(() => null)
|
|
100
144
|
.finally(() => {
|
|
101
|
-
clearPrefetchInflight(
|
|
145
|
+
clearPrefetchInflight(inflightKeys[0]!);
|
|
102
146
|
});
|
|
103
147
|
|
|
104
|
-
|
|
148
|
+
setInflightPromiseWithAliases(inflightKeys, promise);
|
|
105
149
|
return promise;
|
|
106
150
|
}
|
|
107
151
|
|
|
152
|
+
/**
|
|
153
|
+
* Dedup check for prefetch entry presence.
|
|
154
|
+
*
|
|
155
|
+
* Forced `:source` must NOT dedupe against a pre-existing wildcard entry —
|
|
156
|
+
* otherwise the source slot would stay unpopulated and navigation from
|
|
157
|
+
* this source would fall through to the (potentially wrong) wildcard
|
|
158
|
+
* response, defeating the opt-out.
|
|
159
|
+
*/
|
|
160
|
+
function hasPrefetchHit(
|
|
161
|
+
forceSourceScope: boolean,
|
|
162
|
+
wildcardKey: string,
|
|
163
|
+
sourceKey: string,
|
|
164
|
+
): boolean {
|
|
165
|
+
return forceSourceScope
|
|
166
|
+
? hasPrefetch(sourceKey)
|
|
167
|
+
: hasPrefetch(wildcardKey) || hasPrefetch(sourceKey);
|
|
168
|
+
}
|
|
169
|
+
|
|
108
170
|
/**
|
|
109
171
|
* Prefetch (direct): fetch with low priority and store in in-memory cache.
|
|
110
172
|
* Used by hover strategy -- fires immediately without queueing.
|
|
173
|
+
*
|
|
174
|
+
* By default the wildcard key (Rango-state-keyed) is used for inflight
|
|
175
|
+
* dedup and for responses that are not source-sensitive; source-scoped
|
|
176
|
+
* storage is automatic when the server emits `X-RSC-Prefetch-Scope: source`.
|
|
177
|
+
*
|
|
178
|
+
* Pass `prefetchKey=":source"` to force source-scoped inflight + storage
|
|
179
|
+
* (e.g. when the target uses a custom `revalidate()` that reads
|
|
180
|
+
* `currentUrl` and the wildcard slot would serve the wrong diff).
|
|
111
181
|
*/
|
|
112
182
|
export function prefetchDirect(
|
|
113
183
|
url: string,
|
|
114
184
|
segmentIds: string[],
|
|
115
185
|
version?: string,
|
|
116
186
|
routerId?: string,
|
|
117
|
-
prefetchKey?:
|
|
187
|
+
prefetchKey?: ":source",
|
|
118
188
|
): void {
|
|
119
189
|
if (!shouldPrefetch()) return;
|
|
120
190
|
|
|
121
191
|
const targetUrl = buildPrefetchUrl(url, segmentIds, version, routerId);
|
|
122
192
|
if (!targetUrl) return;
|
|
123
|
-
|
|
124
|
-
//
|
|
125
|
-
|
|
193
|
+
const forceSourceScope = prefetchKey === ":source";
|
|
194
|
+
// Skip same-page prefetch — a same-page diff is trivial and would corrupt
|
|
195
|
+
// the wildcard cache entry used for cross-page navigation.
|
|
196
|
+
// When `:source` is forced the entry is source-scoped (single-aliased to
|
|
197
|
+
// itself), so it cannot poison any shared slot — allow it.
|
|
198
|
+
if (!forceSourceScope && isSamePage(url)) {
|
|
126
199
|
return;
|
|
127
200
|
}
|
|
128
|
-
const
|
|
129
|
-
|
|
201
|
+
const sourceHref = window.location.href;
|
|
202
|
+
const rangoState = getRangoState();
|
|
203
|
+
const wildcardKey = buildPrefetchKey(rangoState, targetUrl);
|
|
204
|
+
const sourceKey = buildSourceKey(rangoState, sourceHref, targetUrl);
|
|
205
|
+
if (hasPrefetchHit(forceSourceScope, wildcardKey, sourceKey)) {
|
|
130
206
|
debugLog("[prefetch] direct dedup (key already exists)", {
|
|
131
207
|
url,
|
|
132
|
-
|
|
133
|
-
|
|
208
|
+
wildcardKey,
|
|
209
|
+
sourceKey,
|
|
210
|
+
forceSourceScope,
|
|
134
211
|
});
|
|
135
212
|
return;
|
|
136
213
|
}
|
|
137
214
|
debugLog("[prefetch] direct fetch", {
|
|
138
215
|
url,
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
216
|
+
wildcardKey,
|
|
217
|
+
sourceKey,
|
|
218
|
+
source: sourceHref,
|
|
219
|
+
forceSourceScope,
|
|
142
220
|
});
|
|
143
|
-
executePrefetchFetch(
|
|
221
|
+
executePrefetchFetch(
|
|
222
|
+
wildcardKey,
|
|
223
|
+
sourceKey,
|
|
224
|
+
targetUrl.toString(),
|
|
225
|
+
forceSourceScope,
|
|
226
|
+
);
|
|
144
227
|
}
|
|
145
228
|
|
|
146
229
|
/**
|
|
147
230
|
* Prefetch (queued): goes through the concurrency-limited queue.
|
|
148
231
|
* Used by viewport/render strategies to avoid flooding the server.
|
|
149
|
-
* Returns the
|
|
232
|
+
* Returns the inflight key (wildcard by default, source-scoped when
|
|
233
|
+
* `prefetchKey=":source"` is passed).
|
|
150
234
|
*/
|
|
151
235
|
export function prefetchQueued(
|
|
152
236
|
url: string,
|
|
153
237
|
segmentIds: string[],
|
|
154
238
|
version?: string,
|
|
155
239
|
routerId?: string,
|
|
156
|
-
prefetchKey?:
|
|
240
|
+
prefetchKey?: ":source",
|
|
157
241
|
): string {
|
|
158
242
|
if (!shouldPrefetch()) return "";
|
|
159
243
|
const targetUrl = buildPrefetchUrl(url, segmentIds, version, routerId);
|
|
160
244
|
if (!targetUrl) return "";
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (prefetchKey != null && targetUrl.pathname === window.location.pathname) {
|
|
245
|
+
const forceSourceScope = prefetchKey === ":source";
|
|
246
|
+
if (!forceSourceScope && isSamePage(url)) {
|
|
164
247
|
return "";
|
|
165
248
|
}
|
|
166
|
-
const
|
|
167
|
-
|
|
249
|
+
const sourceHref = window.location.href;
|
|
250
|
+
const rangoState = getRangoState();
|
|
251
|
+
const wildcardKey = buildPrefetchKey(rangoState, targetUrl);
|
|
252
|
+
const sourceKey = buildSourceKey(rangoState, sourceHref, targetUrl);
|
|
253
|
+
const queueKey = forceSourceScope ? sourceKey : wildcardKey;
|
|
254
|
+
if (hasPrefetchHit(forceSourceScope, wildcardKey, sourceKey)) {
|
|
168
255
|
debugLog("[prefetch] queued dedup (key already exists)", {
|
|
169
256
|
url,
|
|
170
|
-
|
|
171
|
-
|
|
257
|
+
wildcardKey,
|
|
258
|
+
sourceKey,
|
|
259
|
+
forceSourceScope,
|
|
172
260
|
});
|
|
173
|
-
return
|
|
261
|
+
return queueKey;
|
|
174
262
|
}
|
|
175
263
|
const fetchUrlStr = targetUrl.toString();
|
|
176
|
-
|
|
177
|
-
enqueuePrefetch(key, (signal) => {
|
|
264
|
+
enqueuePrefetch(queueKey, (signal) => {
|
|
178
265
|
// Re-check at execution time: a hover-triggered prefetchDirect may
|
|
179
266
|
// have started or completed this key while the item sat in the queue.
|
|
180
|
-
if (
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
if (prefetchKey != null && targetPathname === window.location.pathname) {
|
|
267
|
+
if (hasPrefetchHit(forceSourceScope, wildcardKey, sourceKey)) {
|
|
268
|
+
return Promise.resolve();
|
|
269
|
+
}
|
|
270
|
+
if (!forceSourceScope && isSamePage(url)) {
|
|
185
271
|
return Promise.resolve();
|
|
186
272
|
}
|
|
187
|
-
return executePrefetchFetch(
|
|
273
|
+
return executePrefetchFetch(
|
|
274
|
+
wildcardKey,
|
|
275
|
+
sourceKey,
|
|
276
|
+
fetchUrlStr,
|
|
277
|
+
forceSourceScope,
|
|
278
|
+
signal,
|
|
279
|
+
).then(() => {});
|
|
188
280
|
});
|
|
189
|
-
return
|
|
281
|
+
return queueKey;
|
|
190
282
|
}
|
|
@@ -108,10 +108,29 @@ export function enqueuePrefetch(
|
|
|
108
108
|
scheduleDrain();
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Normalize a URL-like string for keep-alive matching: parse against a
|
|
113
|
+
* placeholder origin and strip internal `_rsc_*` query params. Returns
|
|
114
|
+
* `pathname + search` so comparisons ignore hash and the internal params
|
|
115
|
+
* that prefetch appends to targets (`_rsc_partial`, `_rsc_segments`,
|
|
116
|
+
* `_rsc_v`, `_rsc_rid`, `_rsc_stale`).
|
|
117
|
+
*/
|
|
118
|
+
function normalizeForMatch(urlish: string): string {
|
|
119
|
+
try {
|
|
120
|
+
const u = new URL(urlish, "http://placeholder");
|
|
121
|
+
for (const k of [...u.searchParams.keys()]) {
|
|
122
|
+
if (k.startsWith("_rsc_")) u.searchParams.delete(k);
|
|
123
|
+
}
|
|
124
|
+
return u.pathname + u.search;
|
|
125
|
+
} catch {
|
|
126
|
+
return urlish;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
111
130
|
/**
|
|
112
131
|
* Cancel queued prefetches and abort in-flight ones that don't match
|
|
113
132
|
* the current navigation target. If `keepUrl` is provided, the
|
|
114
|
-
* executing prefetch whose key
|
|
133
|
+
* executing prefetch whose key targets that URL is kept alive so
|
|
115
134
|
* navigation can reuse its response via consumeInflightPrefetch.
|
|
116
135
|
*
|
|
117
136
|
* Called when a navigation starts via the NavigationProvider's
|
|
@@ -124,11 +143,23 @@ export function cancelAllPrefetches(keepUrl?: string | null): void {
|
|
|
124
143
|
drainGeneration++;
|
|
125
144
|
|
|
126
145
|
// Abort in-flight prefetches that aren't for the navigation target.
|
|
127
|
-
//
|
|
128
|
-
//
|
|
146
|
+
// Key shapes (see prefetch/cache.ts buildPrefetchKey):
|
|
147
|
+
// wildcard: "rangoState\0/target?..."
|
|
148
|
+
// source-scoped: "rangoState\0sourceHref\0/target?..."
|
|
149
|
+
// The target portion is always the final \0-delimited segment and
|
|
150
|
+
// includes internal `_rsc_*` params (from buildPrefetchUrl); keepUrl
|
|
151
|
+
// comes from NavigationProvider's pendingUrl which is the bare
|
|
152
|
+
// navigation target. Normalize both sides before comparing.
|
|
153
|
+
const normalizedKeep = keepUrl ? normalizeForMatch(keepUrl) : null;
|
|
129
154
|
for (const [key, ac] of abortControllers) {
|
|
130
|
-
const
|
|
131
|
-
|
|
155
|
+
const lastNul = key.lastIndexOf("\0");
|
|
156
|
+
const target = lastNul >= 0 ? key.substring(lastNul + 1) : "";
|
|
157
|
+
if (
|
|
158
|
+
normalizedKeep &&
|
|
159
|
+
target &&
|
|
160
|
+
normalizeForMatch(target) === normalizedKeep
|
|
161
|
+
)
|
|
162
|
+
continue;
|
|
132
163
|
ac.abort();
|
|
133
164
|
abortControllers.delete(key);
|
|
134
165
|
if (executing.delete(key)) {
|
|
@@ -6,21 +6,37 @@
|
|
|
6
6
|
* navigation requests. The server responds with `Vary: X-Rango-State`,
|
|
7
7
|
* so the browser HTTP cache keys responses by (URL, X-Rango-State value).
|
|
8
8
|
*
|
|
9
|
-
*
|
|
9
|
+
* Value format: `{buildVersion}:{invalidationTimestamp}`
|
|
10
10
|
* - Build version changes on deploy, busting all cached prefetches.
|
|
11
11
|
* - Timestamp changes on server action invalidation.
|
|
12
12
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
13
|
+
* Storage key is namespaced per routerId (`rango-state:{routerId}`) so
|
|
14
|
+
* tabs in different apps on the same origin do not collide. Two tabs in
|
|
15
|
+
* the same app share a key → one tab's invalidation is picked up by the
|
|
16
|
+
* other via the `storage` event. A smooth cross-app transition in this
|
|
17
|
+
* tab rebinds to the target app's key; other tabs still in the old app
|
|
18
|
+
* keep their own key intact.
|
|
19
|
+
*
|
|
20
|
+
* If no routerId is supplied, falls back to a single legacy key for
|
|
21
|
+
* backward compatibility (single-app deployments unaffected).
|
|
16
22
|
*/
|
|
17
23
|
|
|
18
|
-
const
|
|
24
|
+
const LEGACY_STORAGE_KEY = "rango-state";
|
|
25
|
+
|
|
26
|
+
function buildStorageKey(routerId: string | undefined): string {
|
|
27
|
+
return routerId ? `${LEGACY_STORAGE_KEY}:${routerId}` : LEGACY_STORAGE_KEY;
|
|
28
|
+
}
|
|
19
29
|
|
|
20
30
|
// Module-level cache avoids hitting localStorage on every getRangoState() call.
|
|
21
31
|
// Initialized from localStorage on first access or by initRangoState().
|
|
22
32
|
let cachedState: string | null = null;
|
|
23
33
|
|
|
34
|
+
// The localStorage key this tab is currently bound to. Rebinds on
|
|
35
|
+
// initRangoState (document boot) and setRangoStateLocal (smooth app
|
|
36
|
+
// switch). The storage listener filters cross-tab events by this key so
|
|
37
|
+
// events from tabs in a different app are ignored.
|
|
38
|
+
let currentStorageKey: string = LEGACY_STORAGE_KEY;
|
|
39
|
+
|
|
24
40
|
// Cross-tab sync: the `storage` event fires in OTHER tabs when one tab writes
|
|
25
41
|
// to localStorage, keeping cachedState fresh without polling.
|
|
26
42
|
let storageListenerAttached = false;
|
|
@@ -28,7 +44,10 @@ let storageListenerAttached = false;
|
|
|
28
44
|
function attachStorageListener(): void {
|
|
29
45
|
if (storageListenerAttached || typeof window === "undefined") return;
|
|
30
46
|
window.addEventListener("storage", (e) => {
|
|
31
|
-
|
|
47
|
+
// Only react to events for this tab's current app namespace. Events
|
|
48
|
+
// under other routerId-scoped keys belong to other apps and must not
|
|
49
|
+
// clobber this tab's state.
|
|
50
|
+
if (e.key !== currentStorageKey) return;
|
|
32
51
|
cachedState = e.newValue;
|
|
33
52
|
});
|
|
34
53
|
storageListenerAttached = true;
|
|
@@ -37,16 +56,22 @@ function attachStorageListener(): void {
|
|
|
37
56
|
/**
|
|
38
57
|
* Initialize the Rango state key in localStorage.
|
|
39
58
|
* Called once at app startup with the build version from the server.
|
|
40
|
-
*
|
|
41
|
-
*
|
|
59
|
+
* The routerId scopes the storage key to this app; in multi-app setups
|
|
60
|
+
* each app owns its own `rango-state:{routerId}` key and cannot observe
|
|
61
|
+
* invalidations from sibling apps on the same origin.
|
|
62
|
+
*
|
|
63
|
+
* If localStorage already has a matching-version entry under the key,
|
|
64
|
+
* keeps it (preserves invalidation state across refresh). Otherwise
|
|
65
|
+
* writes a new value.
|
|
42
66
|
*/
|
|
43
|
-
export function initRangoState(version: string): void {
|
|
67
|
+
export function initRangoState(version: string, routerId?: string): void {
|
|
68
|
+
currentStorageKey = buildStorageKey(routerId);
|
|
44
69
|
if (typeof window === "undefined") return;
|
|
45
70
|
|
|
46
71
|
attachStorageListener();
|
|
47
72
|
|
|
48
73
|
try {
|
|
49
|
-
const existing = localStorage.getItem(
|
|
74
|
+
const existing = localStorage.getItem(currentStorageKey);
|
|
50
75
|
if (existing) {
|
|
51
76
|
const colonIdx = existing.indexOf(":");
|
|
52
77
|
if (colonIdx > 0) {
|
|
@@ -59,7 +84,7 @@ export function initRangoState(version: string): void {
|
|
|
59
84
|
}
|
|
60
85
|
// New version or first load
|
|
61
86
|
const newState = `${version}:${Date.now()}`;
|
|
62
|
-
localStorage.setItem(
|
|
87
|
+
localStorage.setItem(currentStorageKey, newState);
|
|
63
88
|
cachedState = newState;
|
|
64
89
|
} catch {
|
|
65
90
|
// localStorage may be unavailable (private browsing in some browsers)
|
|
@@ -77,7 +102,7 @@ export function getRangoState(): string {
|
|
|
77
102
|
if (typeof window === "undefined") return "0:0";
|
|
78
103
|
|
|
79
104
|
try {
|
|
80
|
-
const stored = localStorage.getItem(
|
|
105
|
+
const stored = localStorage.getItem(currentStorageKey);
|
|
81
106
|
if (stored) {
|
|
82
107
|
cachedState = stored;
|
|
83
108
|
return stored;
|
|
@@ -89,6 +114,21 @@ export function getRangoState(): string {
|
|
|
89
114
|
return "0:0";
|
|
90
115
|
}
|
|
91
116
|
|
|
117
|
+
/**
|
|
118
|
+
* Update the in-memory rango-state to a new version WITHOUT writing
|
|
119
|
+
* localStorage. Intended for smooth cross-app transitions in this tab only:
|
|
120
|
+
* subsequent requests from this tab send the new token, but other tabs
|
|
121
|
+
* still in the previous app do not observe a storage event. Rebinds this
|
|
122
|
+
* tab's storage key to the target app's namespace (`rango-state:{routerId}`)
|
|
123
|
+
* so subsequent storage events only reflect the new app. On the next hard
|
|
124
|
+
* reload, initRangoState reconciles localStorage from the server's
|
|
125
|
+
* authoritative version.
|
|
126
|
+
*/
|
|
127
|
+
export function setRangoStateLocal(version: string, routerId?: string): void {
|
|
128
|
+
currentStorageKey = buildStorageKey(routerId);
|
|
129
|
+
cachedState = `${version}:${Date.now()}`;
|
|
130
|
+
}
|
|
131
|
+
|
|
92
132
|
/**
|
|
93
133
|
* Invalidate the Rango state key. Called when server actions mutate data.
|
|
94
134
|
* Updates the timestamp portion while keeping the version prefix.
|
|
@@ -105,7 +145,7 @@ export function invalidateRangoState(): void {
|
|
|
105
145
|
if (typeof window === "undefined") return;
|
|
106
146
|
|
|
107
147
|
try {
|
|
108
|
-
localStorage.setItem(
|
|
148
|
+
localStorage.setItem(currentStorageKey, newState);
|
|
109
149
|
} catch {
|
|
110
150
|
// Silently handle localStorage errors
|
|
111
151
|
}
|
|
@@ -98,25 +98,30 @@ export interface LinkProps extends Omit<
|
|
|
98
98
|
*/
|
|
99
99
|
prefetch?: PrefetchStrategy;
|
|
100
100
|
/**
|
|
101
|
-
*
|
|
102
|
-
* When set, prefetch responses are cached independently of the current
|
|
103
|
-
* page URL, so navigating to the same target from different source pages
|
|
104
|
-
* reuses the cached prefetch.
|
|
101
|
+
* Opt-in override for the prefetch cache scope.
|
|
105
102
|
*
|
|
106
|
-
* -
|
|
107
|
-
*
|
|
108
|
-
*
|
|
103
|
+
* The default cache is source-agnostic: one shared entry per target,
|
|
104
|
+
* keyed on Rango state + target URL. This is correct for routes whose
|
|
105
|
+
* response shape doesn't depend on where the user navigates from.
|
|
106
|
+
*
|
|
107
|
+
* Set `":source"` when this Link's response would legitimately differ
|
|
108
|
+
* based on the source page — typically when the target route (or one
|
|
109
|
+
* of its layouts) uses a custom `revalidate()` handler that reads
|
|
110
|
+
* `currentUrl` / `currentParams`, and the wildcard entry would
|
|
111
|
+
* therefore serve the wrong diff to a navigation from a different
|
|
112
|
+
* source.
|
|
113
|
+
*
|
|
114
|
+
* Intercept responses are auto-scoped to the source via a server-side
|
|
115
|
+
* tag, so `":source"` is only needed for custom revalidation logic.
|
|
109
116
|
*
|
|
110
117
|
* @example
|
|
111
118
|
* ```tsx
|
|
112
|
-
* //
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
* // Normalize — strip trailing page number from source URL
|
|
116
|
-
* <Link to="/page/3" prefetch="hover" prefetchKey={(from) => from.replace(/\/\d+$/, '')} />
|
|
119
|
+
* // Route uses a `revalidate()` that branches on currentUrl — opt in
|
|
120
|
+
* // so prefetches don't bleed across source pages.
|
|
121
|
+
* <Link to="/dashboard" prefetch="hover" prefetchKey=":source" />
|
|
117
122
|
* ```
|
|
118
123
|
*/
|
|
119
|
-
prefetchKey?:
|
|
124
|
+
prefetchKey?: ":source";
|
|
120
125
|
/**
|
|
121
126
|
* State to pass to history.pushState/replaceState.
|
|
122
127
|
* Accessible via useLocationState() hook.
|