@rangojs/router 0.0.0-experimental.a769fbe7 → 0.0.0-experimental.b02a2fec
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 +76 -18
- package/dist/bin/rango.js +130 -47
- package/dist/vite/index.js +689 -366
- package/dist/vite/index.js.bak +5448 -0
- package/package.json +2 -2
- package/skills/links/SKILL.md +3 -1
- package/skills/middleware/SKILL.md +2 -0
- package/skills/prerender/SKILL.md +110 -68
- package/skills/router-setup/SKILL.md +35 -0
- package/src/__internal.ts +1 -1
- package/src/browser/app-version.ts +14 -0
- package/src/browser/navigation-bridge.ts +19 -4
- package/src/browser/navigation-client.ts +64 -64
- package/src/browser/navigation-store.ts +43 -8
- package/src/browser/partial-update.ts +27 -5
- package/src/browser/prefetch/fetch.ts +8 -2
- package/src/browser/react/Link.tsx +44 -8
- package/src/browser/react/NavigationProvider.tsx +8 -1
- package/src/browser/react/context.ts +7 -2
- package/src/browser/react/use-handle.ts +9 -58
- package/src/browser/react/use-router.ts +21 -8
- package/src/browser/rsc-router.tsx +26 -3
- package/src/browser/scroll-restoration.ts +10 -8
- package/src/browser/server-action-bridge.ts +8 -18
- package/src/browser/types.ts +20 -5
- package/src/build/generate-manifest.ts +6 -6
- package/src/build/generate-route-types.ts +3 -0
- package/src/build/route-types/include-resolution.ts +8 -1
- package/src/build/route-types/router-processing.ts +211 -72
- package/src/build/route-types/scan-filter.ts +8 -1
- package/src/client.tsx +2 -56
- package/src/deps/browser.ts +0 -1
- package/src/handle.ts +40 -0
- package/src/index.rsc.ts +3 -1
- package/src/index.ts +12 -0
- package/src/prerender/store.ts +5 -4
- package/src/prerender.ts +138 -77
- package/src/reverse.ts +22 -1
- package/src/route-definition/dsl-helpers.ts +42 -19
- package/src/route-definition/helpers-types.ts +4 -1
- package/src/route-definition/index.ts +3 -0
- package/src/route-definition/redirect.ts +9 -1
- package/src/route-definition/resolve-handler-use.ts +149 -0
- package/src/route-types.ts +11 -0
- package/src/router/content-negotiation.ts +100 -1
- package/src/router/handler-context.ts +48 -15
- package/src/router/intercept-resolution.ts +9 -4
- package/src/router/loader-resolution.ts +150 -21
- package/src/router/match-api.ts +124 -189
- package/src/router/match-middleware/cache-lookup.ts +28 -8
- package/src/router/match-middleware/segment-resolution.ts +53 -0
- package/src/router/match-result.ts +82 -4
- package/src/router/middleware-types.ts +0 -6
- package/src/router/middleware.ts +0 -3
- package/src/router/navigation-snapshot.ts +182 -0
- 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-interfaces.ts +36 -4
- package/src/router/router-options.ts +37 -11
- package/src/router/segment-resolution/fresh.ts +70 -5
- package/src/router/segment-resolution/revalidation.ts +87 -9
- package/src/router.ts +53 -5
- package/src/rsc/handler.ts +472 -398
- package/src/rsc/loader-fetch.ts +18 -3
- package/src/rsc/manifest-init.ts +5 -1
- package/src/rsc/progressive-enhancement.ts +12 -3
- package/src/rsc/rsc-rendering.ts +8 -2
- package/src/rsc/server-action.ts +8 -2
- package/src/rsc/ssr-setup.ts +2 -2
- package/src/rsc/types.ts +6 -4
- package/src/server/context.ts +39 -2
- package/src/server/handle-store.ts +19 -0
- package/src/server/loader-registry.ts +9 -8
- package/src/server/request-context.ts +132 -13
- package/src/ssr/index.tsx +3 -0
- package/src/static-handler.ts +18 -6
- package/src/types/cache-types.ts +4 -4
- package/src/types/handler-context.ts +17 -11
- package/src/types/loader-types.ts +32 -5
- package/src/types/route-entry.ts +1 -1
- package/src/types/segments.ts +1 -0
- package/src/urls/path-helper-types.ts +9 -2
- package/src/urls/path-helper.ts +47 -12
- package/src/urls/pattern-types.ts +12 -0
- package/src/urls/response-types.ts +16 -6
- package/src/use-loader.tsx +77 -5
- 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 -4
- package/src/vite/index.ts +4 -0
- package/src/vite/plugin-types.ts +60 -5
- 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 +64 -207
- package/src/vite/plugins/refresh-cmd.ts +88 -26
- package/src/vite/rango.ts +18 -5
- package/src/vite/router-discovery.ts +178 -37
- package/src/vite/utils/prerender-utils.ts +18 -0
- package/src/vite/utils/shared-utils.ts +3 -2
- package/src/browser/debug-channel.ts +0 -93
|
@@ -12,8 +12,6 @@ import {
|
|
|
12
12
|
startBrowserTransaction,
|
|
13
13
|
} from "./logging.js";
|
|
14
14
|
import { getRangoState } from "./rango-state.js";
|
|
15
|
-
import { createClientDebugChannel, DEBUG_ID_HEADER } from "./debug-channel.js";
|
|
16
|
-
import { findSourceMapURL } from "../deps/browser.js";
|
|
17
15
|
import {
|
|
18
16
|
extractRscHeaderUrl,
|
|
19
17
|
emptyResponse,
|
|
@@ -63,6 +61,7 @@ export function createNavigationClient(
|
|
|
63
61
|
staleRevalidation,
|
|
64
62
|
interceptSourceUrl,
|
|
65
63
|
version,
|
|
64
|
+
routerId,
|
|
66
65
|
hmr,
|
|
67
66
|
} = options;
|
|
68
67
|
|
|
@@ -90,6 +89,9 @@ export function createNavigationClient(
|
|
|
90
89
|
if (version) {
|
|
91
90
|
fetchUrl.searchParams.set("_rsc_v", version);
|
|
92
91
|
}
|
|
92
|
+
if (routerId) {
|
|
93
|
+
fetchUrl.searchParams.set("_rsc_rid", routerId);
|
|
94
|
+
}
|
|
93
95
|
|
|
94
96
|
// Check completed in-memory prefetch cache before making a network request.
|
|
95
97
|
// The cache key includes the source URL (previousUrl) because the
|
|
@@ -109,21 +111,53 @@ export function createNavigationClient(
|
|
|
109
111
|
resolveStreamComplete = resolve;
|
|
110
112
|
});
|
|
111
113
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
:
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
114
|
+
/**
|
|
115
|
+
* Validate RSC control headers on any response (fresh, cached, or
|
|
116
|
+
* in-flight). Handles version-mismatch reloads and server redirects.
|
|
117
|
+
* Returns the response unchanged when no control header is present.
|
|
118
|
+
*/
|
|
119
|
+
const validateRscHeaders = (
|
|
120
|
+
response: Response,
|
|
121
|
+
source: string,
|
|
122
|
+
): Response | Promise<Response> => {
|
|
123
|
+
// Version mismatch — server wants a full page reload
|
|
124
|
+
const reload = extractRscHeaderUrl(response, "X-RSC-Reload");
|
|
125
|
+
if (reload === "blocked") {
|
|
126
|
+
resolveStreamComplete();
|
|
127
|
+
return emptyResponse();
|
|
128
|
+
}
|
|
129
|
+
if (reload) {
|
|
130
|
+
if (tx) {
|
|
131
|
+
browserDebugLog(tx, `version mismatch, reloading (${source})`, {
|
|
132
|
+
reloadUrl: reload.url,
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
window.location.href = reload.url;
|
|
136
|
+
// Block further processing — page is reloading
|
|
137
|
+
return new Promise<Response>(() => {});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Server-side redirect without state: the server returned 204 with
|
|
141
|
+
// X-RSC-Redirect instead of a 3xx (which fetch would auto-follow
|
|
142
|
+
// to a URL rendering full HTML). Throw ServerRedirect so the
|
|
143
|
+
// navigation bridge catches it and re-navigates with _skipCache.
|
|
144
|
+
const redirect = extractRscHeaderUrl(response, "X-RSC-Redirect");
|
|
145
|
+
if (redirect === "blocked") {
|
|
146
|
+
resolveStreamComplete();
|
|
147
|
+
return emptyResponse();
|
|
148
|
+
}
|
|
149
|
+
if (redirect) {
|
|
150
|
+
if (tx) {
|
|
151
|
+
browserDebugLog(tx, `server redirect (${source})`, {
|
|
152
|
+
redirectUrl: redirect.url,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
resolveStreamComplete();
|
|
156
|
+
throw new ServerRedirect(redirect.url, undefined);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return response;
|
|
160
|
+
};
|
|
127
161
|
|
|
128
162
|
/** Start a fresh navigation fetch (no cache / inflight hit). */
|
|
129
163
|
const doFreshFetch = (): Promise<Response> => {
|
|
@@ -142,47 +176,14 @@ export function createNavigationClient(
|
|
|
142
176
|
"X-RSC-Router-Intercept-Source": interceptSourceUrl,
|
|
143
177
|
}),
|
|
144
178
|
...(hmr && { "X-RSC-HMR": "1" }),
|
|
145
|
-
...(debugId && { [DEBUG_ID_HEADER]: debugId }),
|
|
146
179
|
},
|
|
147
180
|
signal,
|
|
148
181
|
}).then((response) => {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
if (reload === "blocked") {
|
|
152
|
-
resolveStreamComplete();
|
|
153
|
-
return emptyResponse();
|
|
154
|
-
}
|
|
155
|
-
if (reload) {
|
|
156
|
-
if (tx) {
|
|
157
|
-
browserDebugLog(tx, "version mismatch, reloading", {
|
|
158
|
-
reloadUrl: reload.url,
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
window.location.href = reload.url;
|
|
162
|
-
return new Promise<Response>(() => {});
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// Server-side redirect without state: the server returned 204 with
|
|
166
|
-
// X-RSC-Redirect instead of a 3xx (which fetch would auto-follow
|
|
167
|
-
// to a URL rendering full HTML). Throw ServerRedirect so the
|
|
168
|
-
// navigation bridge catches it and re-navigates with _skipCache.
|
|
169
|
-
const redirect = extractRscHeaderUrl(response, "X-RSC-Redirect");
|
|
170
|
-
if (redirect === "blocked") {
|
|
171
|
-
resolveStreamComplete();
|
|
172
|
-
return emptyResponse();
|
|
173
|
-
}
|
|
174
|
-
if (redirect) {
|
|
175
|
-
if (tx) {
|
|
176
|
-
browserDebugLog(tx, "server redirect", {
|
|
177
|
-
redirectUrl: redirect.url,
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
resolveStreamComplete();
|
|
181
|
-
throw new ServerRedirect(redirect.url, undefined);
|
|
182
|
-
}
|
|
182
|
+
const validated = validateRscHeaders(response, "fetch");
|
|
183
|
+
if (validated instanceof Promise) return validated;
|
|
183
184
|
|
|
184
185
|
return teeWithCompletion(
|
|
185
|
-
|
|
186
|
+
validated,
|
|
186
187
|
() => {
|
|
187
188
|
if (tx) browserDebugLog(tx, "stream complete");
|
|
188
189
|
resolveStreamComplete();
|
|
@@ -198,11 +199,12 @@ export function createNavigationClient(
|
|
|
198
199
|
if (tx) {
|
|
199
200
|
browserDebugLog(tx, "prefetch cache hit", { key: cacheKey });
|
|
200
201
|
}
|
|
201
|
-
// Cached response body is already fully buffered (arrayBuffer),
|
|
202
|
-
// so stream completion is immediate.
|
|
203
202
|
responsePromise = Promise.resolve(cachedResponse).then((response) => {
|
|
203
|
+
const validated = validateRscHeaders(response, "prefetch cache");
|
|
204
|
+
if (validated instanceof Promise) return validated;
|
|
205
|
+
|
|
204
206
|
return teeWithCompletion(
|
|
205
|
-
|
|
207
|
+
validated,
|
|
206
208
|
() => {
|
|
207
209
|
if (tx) browserDebugLog(tx, "stream complete (from cache)");
|
|
208
210
|
resolveStreamComplete();
|
|
@@ -222,8 +224,11 @@ export function createNavigationClient(
|
|
|
222
224
|
return doFreshFetch();
|
|
223
225
|
}
|
|
224
226
|
|
|
227
|
+
const validated = validateRscHeaders(response, "inflight prefetch");
|
|
228
|
+
if (validated instanceof Promise) return validated;
|
|
229
|
+
|
|
225
230
|
return teeWithCompletion(
|
|
226
|
-
|
|
231
|
+
validated,
|
|
227
232
|
() => {
|
|
228
233
|
if (tx) {
|
|
229
234
|
browserDebugLog(tx, "stream complete (from inflight prefetch)");
|
|
@@ -238,13 +243,8 @@ export function createNavigationClient(
|
|
|
238
243
|
}
|
|
239
244
|
|
|
240
245
|
try {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
responsePromise,
|
|
244
|
-
{
|
|
245
|
-
...(debugChannel && { debugChannel, findSourceMapURL }),
|
|
246
|
-
},
|
|
247
|
-
);
|
|
246
|
+
const payload = await deps.createFromFetch<RscPayload>(responsePromise);
|
|
247
|
+
|
|
248
248
|
if (tx) {
|
|
249
249
|
browserDebugLog(tx, "response received", {
|
|
250
250
|
isPartial: payload.metadata?.isPartial,
|
|
@@ -28,9 +28,15 @@ const DEFAULT_ACTION_STATE: TrackedActionState = {
|
|
|
28
28
|
// Maximum number of history entries to cache (URLs visited)
|
|
29
29
|
const HISTORY_CACHE_SIZE = 20;
|
|
30
30
|
|
|
31
|
-
// Cache entry: [url-key, segments, stale, handleData?]
|
|
31
|
+
// Cache entry: [url-key, segments, stale, handleData?, routerId?]
|
|
32
32
|
// stale=true means the data may be outdated and should be revalidated on access
|
|
33
|
-
type HistoryCacheEntry = [
|
|
33
|
+
type HistoryCacheEntry = [
|
|
34
|
+
string,
|
|
35
|
+
ResolvedSegment[],
|
|
36
|
+
boolean,
|
|
37
|
+
HandleData?,
|
|
38
|
+
string?,
|
|
39
|
+
];
|
|
34
40
|
|
|
35
41
|
/**
|
|
36
42
|
* Shallow clone handleData to avoid reference sharing between cache entries.
|
|
@@ -258,6 +264,11 @@ export function createNavigationStore(
|
|
|
258
264
|
// Used to maintain intercept context during action revalidation
|
|
259
265
|
let interceptSourceUrl: string | null = null;
|
|
260
266
|
|
|
267
|
+
// Router identity - tracks which router is currently active.
|
|
268
|
+
// When this changes on a partial response, the client forces a full
|
|
269
|
+
// tree replacement instead of reconciling with stale segments.
|
|
270
|
+
let currentRouterId: string | undefined;
|
|
271
|
+
|
|
261
272
|
// Action state tracking (for useAction hook)
|
|
262
273
|
// Maps action function ID to its tracked state
|
|
263
274
|
const actionStates = new Map<string, TrackedActionState>();
|
|
@@ -571,10 +582,17 @@ export function createNavigationStore(
|
|
|
571
582
|
segments,
|
|
572
583
|
false,
|
|
573
584
|
clonedHandleData,
|
|
585
|
+
currentRouterId,
|
|
574
586
|
];
|
|
575
587
|
} else {
|
|
576
588
|
// Add new entry at the end (not stale)
|
|
577
|
-
historyCache.push([
|
|
589
|
+
historyCache.push([
|
|
590
|
+
historyKey,
|
|
591
|
+
segments,
|
|
592
|
+
false,
|
|
593
|
+
clonedHandleData,
|
|
594
|
+
currentRouterId,
|
|
595
|
+
]);
|
|
578
596
|
// Remove oldest entries if over limit
|
|
579
597
|
while (historyCache.length > cacheSize) {
|
|
580
598
|
historyCache.shift();
|
|
@@ -586,14 +604,22 @@ export function createNavigationStore(
|
|
|
586
604
|
* Get cached segments for a history entry
|
|
587
605
|
* Returns { segments, stale, handleData } or undefined if not cached
|
|
588
606
|
*/
|
|
589
|
-
getCachedSegments(
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
607
|
+
getCachedSegments(historyKey: string):
|
|
608
|
+
| {
|
|
609
|
+
segments: ResolvedSegment[];
|
|
610
|
+
stale: boolean;
|
|
611
|
+
handleData?: HandleData;
|
|
612
|
+
routerId?: string;
|
|
613
|
+
}
|
|
593
614
|
| undefined {
|
|
594
615
|
const entry = historyCache.find(([key]) => key === historyKey);
|
|
595
616
|
if (!entry) return undefined;
|
|
596
|
-
return {
|
|
617
|
+
return {
|
|
618
|
+
segments: entry[1],
|
|
619
|
+
stale: entry[2],
|
|
620
|
+
handleData: entry[3],
|
|
621
|
+
routerId: entry[4],
|
|
622
|
+
};
|
|
597
623
|
},
|
|
598
624
|
|
|
599
625
|
/**
|
|
@@ -621,6 +647,7 @@ export function createNavigationStore(
|
|
|
621
647
|
entry[1],
|
|
622
648
|
entry[2],
|
|
623
649
|
clonedHandleData,
|
|
650
|
+
entry[4], // preserve routerId
|
|
624
651
|
];
|
|
625
652
|
}
|
|
626
653
|
},
|
|
@@ -687,6 +714,14 @@ export function createNavigationStore(
|
|
|
687
714
|
interceptSourceUrl = url;
|
|
688
715
|
},
|
|
689
716
|
|
|
717
|
+
getRouterId(): string | undefined {
|
|
718
|
+
return currentRouterId;
|
|
719
|
+
},
|
|
720
|
+
|
|
721
|
+
setRouterId(id: string): void {
|
|
722
|
+
currentRouterId = id;
|
|
723
|
+
},
|
|
724
|
+
|
|
690
725
|
// ========================================================================
|
|
691
726
|
// UI Update Notifications
|
|
692
727
|
// ========================================================================
|
|
@@ -39,8 +39,8 @@ export interface PartialUpdateConfig {
|
|
|
39
39
|
segments: ResolvedSegment[],
|
|
40
40
|
options?: RenderSegmentsOptions,
|
|
41
41
|
) => Promise<ReactNode> | ReactNode;
|
|
42
|
-
/** RSC version
|
|
43
|
-
|
|
42
|
+
/** RSC version getter — returns the current version (may change after HMR) */
|
|
43
|
+
getVersion?: () => string | undefined;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
/**
|
|
@@ -104,7 +104,13 @@ export type PartialUpdater = (
|
|
|
104
104
|
export function createPartialUpdater(
|
|
105
105
|
config: PartialUpdateConfig,
|
|
106
106
|
): PartialUpdater {
|
|
107
|
-
const {
|
|
107
|
+
const {
|
|
108
|
+
store,
|
|
109
|
+
client,
|
|
110
|
+
onUpdate,
|
|
111
|
+
renderSegments,
|
|
112
|
+
getVersion = () => undefined,
|
|
113
|
+
} = config;
|
|
108
114
|
|
|
109
115
|
/**
|
|
110
116
|
* Get current page's cached segments as an array
|
|
@@ -193,7 +199,8 @@ export function createPartialUpdater(
|
|
|
193
199
|
// (action redirect sends empty segments for a fresh render).
|
|
194
200
|
staleRevalidation:
|
|
195
201
|
mode.type === "stale-revalidation" || segments.length === 0,
|
|
196
|
-
version,
|
|
202
|
+
version: getVersion(),
|
|
203
|
+
routerId: store.getRouterId?.(),
|
|
197
204
|
});
|
|
198
205
|
// Mark navigation as streaming (response received, now parsing RSC).
|
|
199
206
|
// Called after fetchPartial so pendingUrl stays set during the network wait,
|
|
@@ -206,6 +213,21 @@ export function createPartialUpdater(
|
|
|
206
213
|
streamingToken.end();
|
|
207
214
|
});
|
|
208
215
|
|
|
216
|
+
// Detect app switch: if routerId changed, the navigation crossed into
|
|
217
|
+
// a different router (e.g., via host router path mount). Downgrade
|
|
218
|
+
// partial to full so the entire tree is replaced without reconciliation
|
|
219
|
+
// against stale segments from the previous app.
|
|
220
|
+
if (payload.metadata?.routerId) {
|
|
221
|
+
const prevRouterId = store.getRouterId?.();
|
|
222
|
+
if (prevRouterId && prevRouterId !== payload.metadata.routerId) {
|
|
223
|
+
debugLog(
|
|
224
|
+
`[Browser] App switch detected (${prevRouterId} → ${payload.metadata.routerId}), forcing full update`,
|
|
225
|
+
);
|
|
226
|
+
payload.metadata.isPartial = false;
|
|
227
|
+
}
|
|
228
|
+
store.setRouterId?.(payload.metadata.routerId);
|
|
229
|
+
}
|
|
230
|
+
|
|
209
231
|
// Handle server-side redirect with state
|
|
210
232
|
if (payload.metadata?.redirect) {
|
|
211
233
|
if (signal?.aborted) {
|
|
@@ -259,7 +281,7 @@ export function createPartialUpdater(
|
|
|
259
281
|
existingSegments,
|
|
260
282
|
);
|
|
261
283
|
|
|
262
|
-
//
|
|
284
|
+
// tx.commit() cached the source page's handleData because
|
|
263
285
|
// eventController hasn't been updated yet. Overwrite with the
|
|
264
286
|
// correct cached handleData to prevent cache corruption on
|
|
265
287
|
// subsequent navigations to this same URL.
|
|
@@ -34,6 +34,7 @@ function buildPrefetchUrl(
|
|
|
34
34
|
url: string,
|
|
35
35
|
segmentIds: string[],
|
|
36
36
|
version?: string,
|
|
37
|
+
routerId?: string,
|
|
37
38
|
): URL | null {
|
|
38
39
|
let targetUrl: URL;
|
|
39
40
|
try {
|
|
@@ -51,6 +52,9 @@ function buildPrefetchUrl(
|
|
|
51
52
|
if (version) {
|
|
52
53
|
targetUrl.searchParams.set("_rsc_v", version);
|
|
53
54
|
}
|
|
55
|
+
if (routerId) {
|
|
56
|
+
targetUrl.searchParams.set("_rsc_rid", routerId);
|
|
57
|
+
}
|
|
54
58
|
return targetUrl;
|
|
55
59
|
}
|
|
56
60
|
|
|
@@ -108,10 +112,11 @@ export function prefetchDirect(
|
|
|
108
112
|
url: string,
|
|
109
113
|
segmentIds: string[],
|
|
110
114
|
version?: string,
|
|
115
|
+
routerId?: string,
|
|
111
116
|
): void {
|
|
112
117
|
if (!shouldPrefetch()) return;
|
|
113
118
|
|
|
114
|
-
const targetUrl = buildPrefetchUrl(url, segmentIds, version);
|
|
119
|
+
const targetUrl = buildPrefetchUrl(url, segmentIds, version, routerId);
|
|
115
120
|
if (!targetUrl) return;
|
|
116
121
|
const key = buildPrefetchKey(window.location.href, targetUrl);
|
|
117
122
|
if (hasPrefetch(key)) return;
|
|
@@ -127,9 +132,10 @@ export function prefetchQueued(
|
|
|
127
132
|
url: string,
|
|
128
133
|
segmentIds: string[],
|
|
129
134
|
version?: string,
|
|
135
|
+
routerId?: string,
|
|
130
136
|
): string {
|
|
131
137
|
if (!shouldPrefetch()) return "";
|
|
132
|
-
const targetUrl = buildPrefetchUrl(url, segmentIds, version);
|
|
138
|
+
const targetUrl = buildPrefetchUrl(url, segmentIds, version, routerId);
|
|
133
139
|
if (!targetUrl) return "";
|
|
134
140
|
const key = buildPrefetchKey(window.location.href, targetUrl);
|
|
135
141
|
if (hasPrefetch(key)) return key;
|
|
@@ -5,6 +5,7 @@ import React, {
|
|
|
5
5
|
useCallback,
|
|
6
6
|
useContext,
|
|
7
7
|
useEffect,
|
|
8
|
+
useMemo,
|
|
8
9
|
useRef,
|
|
9
10
|
type ForwardRefExoticComponent,
|
|
10
11
|
type RefAttributes,
|
|
@@ -32,6 +33,7 @@ export type LinkState =
|
|
|
32
33
|
| StateOrGetter<Record<string, unknown>>;
|
|
33
34
|
|
|
34
35
|
import { prefetchDirect, prefetchQueued } from "../prefetch/fetch.js";
|
|
36
|
+
import { getAppVersion } from "../app-version.js";
|
|
35
37
|
import {
|
|
36
38
|
observeForPrefetch,
|
|
37
39
|
unobserveForPrefetch,
|
|
@@ -192,6 +194,16 @@ export const Link: ForwardRefExoticComponent<
|
|
|
192
194
|
const ctx = useContext(NavigationStoreContext);
|
|
193
195
|
const isExternal = isExternalUrl(to);
|
|
194
196
|
|
|
197
|
+
// Auto-prefix with basename for app-local paths.
|
|
198
|
+
// Skip if external, already prefixed, or not a root-relative path.
|
|
199
|
+
const resolvedTo = useMemo(() => {
|
|
200
|
+
if (isExternal) return to;
|
|
201
|
+
const bn = ctx?.basename;
|
|
202
|
+
if (!bn || !to.startsWith("/") || to.startsWith(bn + "/") || to === bn)
|
|
203
|
+
return to;
|
|
204
|
+
return to === "/" ? bn : bn + to;
|
|
205
|
+
}, [to, isExternal, ctx?.basename]);
|
|
206
|
+
|
|
195
207
|
// Resolve adaptive: viewport on touch devices, hover on pointer devices
|
|
196
208
|
const resolvedStrategy =
|
|
197
209
|
prefetch === "adaptive" ? (isTouchDevice ? "viewport" : "hover") : prefetch;
|
|
@@ -273,9 +285,23 @@ export const Link: ForwardRefExoticComponent<
|
|
|
273
285
|
resolvedState = currentState;
|
|
274
286
|
}
|
|
275
287
|
|
|
276
|
-
ctx.navigate(
|
|
288
|
+
ctx.navigate(resolvedTo, {
|
|
289
|
+
replace,
|
|
290
|
+
scroll,
|
|
291
|
+
state: resolvedState,
|
|
292
|
+
revalidate,
|
|
293
|
+
});
|
|
277
294
|
},
|
|
278
|
-
[
|
|
295
|
+
[
|
|
296
|
+
resolvedTo,
|
|
297
|
+
isExternal,
|
|
298
|
+
reloadDocument,
|
|
299
|
+
replace,
|
|
300
|
+
scroll,
|
|
301
|
+
revalidate,
|
|
302
|
+
ctx,
|
|
303
|
+
onClick,
|
|
304
|
+
],
|
|
279
305
|
);
|
|
280
306
|
|
|
281
307
|
const handleMouseEnter = useCallback(() => {
|
|
@@ -289,9 +315,14 @@ export const Link: ForwardRefExoticComponent<
|
|
|
289
315
|
// prefetch — prefetchDirect bypasses the queue, and hasPrefetch
|
|
290
316
|
// deduplicates if the viewport prefetch already completed.
|
|
291
317
|
const segmentState = ctx.store.getSegmentState();
|
|
292
|
-
prefetchDirect(
|
|
318
|
+
prefetchDirect(
|
|
319
|
+
resolvedTo,
|
|
320
|
+
segmentState.currentSegmentIds,
|
|
321
|
+
getAppVersion(),
|
|
322
|
+
ctx.store.getRouterId?.(),
|
|
323
|
+
);
|
|
293
324
|
}
|
|
294
|
-
}, [resolvedStrategy,
|
|
325
|
+
}, [resolvedStrategy, resolvedTo, isExternal, ctx]);
|
|
295
326
|
|
|
296
327
|
// Viewport/render prefetch: waits for idle before starting,
|
|
297
328
|
// uses concurrency-limited queue to avoid flooding.
|
|
@@ -308,7 +339,12 @@ export const Link: ForwardRefExoticComponent<
|
|
|
308
339
|
const triggerPrefetch = () => {
|
|
309
340
|
if (cancelled) return;
|
|
310
341
|
const segmentState = ctx.store.getSegmentState();
|
|
311
|
-
prefetchQueued(
|
|
342
|
+
prefetchQueued(
|
|
343
|
+
resolvedTo,
|
|
344
|
+
segmentState.currentSegmentIds,
|
|
345
|
+
getAppVersion(),
|
|
346
|
+
ctx.store.getRouterId?.(),
|
|
347
|
+
);
|
|
312
348
|
};
|
|
313
349
|
|
|
314
350
|
// Schedule prefetch only when the app is idle (no navigation/streaming).
|
|
@@ -347,12 +383,12 @@ export const Link: ForwardRefExoticComponent<
|
|
|
347
383
|
unobserveForPrefetch(observedElement);
|
|
348
384
|
}
|
|
349
385
|
};
|
|
350
|
-
}, [resolvedStrategy,
|
|
386
|
+
}, [resolvedStrategy, resolvedTo, isExternal, ctx]);
|
|
351
387
|
|
|
352
388
|
return (
|
|
353
389
|
<a
|
|
354
390
|
ref={setRef}
|
|
355
|
-
href={
|
|
391
|
+
href={resolvedTo}
|
|
356
392
|
onClick={handleClick}
|
|
357
393
|
onMouseEnter={handleMouseEnter}
|
|
358
394
|
data-link-component
|
|
@@ -362,7 +398,7 @@ export const Link: ForwardRefExoticComponent<
|
|
|
362
398
|
data-revalidate={revalidate === false ? "false" : undefined}
|
|
363
399
|
{...props}
|
|
364
400
|
>
|
|
365
|
-
<LinkContext.Provider value={
|
|
401
|
+
<LinkContext.Provider value={resolvedTo}>{children}</LinkContext.Provider>
|
|
366
402
|
</a>
|
|
367
403
|
);
|
|
368
404
|
});
|
|
@@ -134,9 +134,14 @@ export interface NavigationProviderProps {
|
|
|
134
134
|
|
|
135
135
|
/**
|
|
136
136
|
* App version from server payload (stable, immutable).
|
|
137
|
-
* Forwarded to
|
|
137
|
+
* Forwarded to context for cache key building.
|
|
138
138
|
*/
|
|
139
139
|
version?: string;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* URL prefix for all routes (from createRouter({ basename })).
|
|
143
|
+
*/
|
|
144
|
+
basename?: string;
|
|
140
145
|
}
|
|
141
146
|
|
|
142
147
|
/**
|
|
@@ -169,6 +174,7 @@ export function NavigationProvider({
|
|
|
169
174
|
initialTheme,
|
|
170
175
|
warmupEnabled,
|
|
171
176
|
version,
|
|
177
|
+
basename,
|
|
172
178
|
}: NavigationProviderProps): ReactNode {
|
|
173
179
|
// Track current payload for rendering (this triggers re-renders)
|
|
174
180
|
const [payload, setPayload] = useState(initialPayload);
|
|
@@ -198,6 +204,7 @@ export function NavigationProvider({
|
|
|
198
204
|
navigate,
|
|
199
205
|
refresh,
|
|
200
206
|
version,
|
|
207
|
+
basename,
|
|
201
208
|
}),
|
|
202
209
|
[],
|
|
203
210
|
);
|
|
@@ -43,10 +43,15 @@ export interface NavigationStoreContextValue {
|
|
|
43
43
|
refresh: () => Promise<void>;
|
|
44
44
|
|
|
45
45
|
/**
|
|
46
|
-
* App version from server payload
|
|
47
|
-
* Used in prefetch requests for version mismatch detection.
|
|
46
|
+
* App version from the initial server payload.
|
|
48
47
|
*/
|
|
49
48
|
version: string | undefined;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* URL prefix for all routes (from createRouter({ basename })).
|
|
52
|
+
* Used by Link and useRouter() to auto-prefix app-local paths.
|
|
53
|
+
*/
|
|
54
|
+
basename: string | undefined;
|
|
50
55
|
}
|
|
51
56
|
|
|
52
57
|
/**
|
|
@@ -9,64 +9,11 @@ import {
|
|
|
9
9
|
startTransition,
|
|
10
10
|
} from "react";
|
|
11
11
|
import type { Handle } from "../../handle.js";
|
|
12
|
-
import {
|
|
12
|
+
import { collectHandleData } from "../../handle.js";
|
|
13
13
|
import type { HandleData } from "../types.js";
|
|
14
14
|
import { NavigationStoreContext } from "./context.js";
|
|
15
15
|
import { shallowEqual } from "./shallow-equal.js";
|
|
16
16
|
|
|
17
|
-
/**
|
|
18
|
-
* Resolve the collect function for a handle.
|
|
19
|
-
* Handle objects are plain { __brand, $$id } - collect is stored in the registry
|
|
20
|
-
* (populated when createHandle runs on the client).
|
|
21
|
-
*/
|
|
22
|
-
function resolveCollect<T, A>(handle: Handle<T, A>): (segments: T[][]) => A {
|
|
23
|
-
// Look up collect from the registry (populated when the handle module is imported).
|
|
24
|
-
const registered = getCollectFn(handle.$$id);
|
|
25
|
-
if (registered) {
|
|
26
|
-
return registered as (segments: T[][]) => A;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// Fall back to default flat collect with a dev warning.
|
|
30
|
-
if (process.env.NODE_ENV !== "production") {
|
|
31
|
-
console.warn(
|
|
32
|
-
`[rsc-router] Handle "${handle.$$id}" was passed as a prop but its collect ` +
|
|
33
|
-
`function could not be resolved. Falling back to flat array. ` +
|
|
34
|
-
`Import the handle module in a client component to register its collect function.`,
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
return ((segments: unknown[][]) => segments.flat()) as unknown as (
|
|
38
|
-
segments: T[][],
|
|
39
|
-
) => A;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Collect handle data from segments and transform to final value.
|
|
44
|
-
*/
|
|
45
|
-
function collectHandle<T, A>(
|
|
46
|
-
handle: Handle<T, A>,
|
|
47
|
-
data: HandleData,
|
|
48
|
-
segmentOrder: string[],
|
|
49
|
-
): A {
|
|
50
|
-
const collect = resolveCollect(handle);
|
|
51
|
-
const segmentData = data[handle.$$id];
|
|
52
|
-
|
|
53
|
-
if (!segmentData) {
|
|
54
|
-
return collect([]);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// Build array of segment arrays in parent -> child order
|
|
58
|
-
const segmentArrays: T[][] = [];
|
|
59
|
-
for (const segmentId of segmentOrder) {
|
|
60
|
-
const entries = segmentData[segmentId];
|
|
61
|
-
if (entries && entries.length > 0) {
|
|
62
|
-
segmentArrays.push(entries as T[]);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// Call collect once with all segment data
|
|
67
|
-
return collect(segmentArrays);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
17
|
/**
|
|
71
18
|
* Hook to access collected handle data.
|
|
72
19
|
*
|
|
@@ -99,13 +46,13 @@ export function useHandle<T, A, S>(
|
|
|
99
46
|
// Initial state from context event controller, or empty fallback without provider.
|
|
100
47
|
const [value, setValue] = useState<A | S>(() => {
|
|
101
48
|
if (!ctx) {
|
|
102
|
-
const collected =
|
|
49
|
+
const collected = collectHandleData(handle, {}, []);
|
|
103
50
|
return selector ? selector(collected) : collected;
|
|
104
51
|
}
|
|
105
52
|
|
|
106
53
|
// On client, use event controller state
|
|
107
54
|
const state = ctx.eventController.getHandleState();
|
|
108
|
-
const collected =
|
|
55
|
+
const collected = collectHandleData(handle, state.data, state.segmentOrder);
|
|
109
56
|
return selector ? selector(collected) : collected;
|
|
110
57
|
});
|
|
111
58
|
const [optimisticValue, setOptimisticValue] = useOptimistic(value);
|
|
@@ -125,7 +72,7 @@ export function useHandle<T, A, S>(
|
|
|
125
72
|
// Sync current state for the (possibly new) handle so that switching
|
|
126
73
|
// handles on an idle page doesn't leave stale data from the old handle.
|
|
127
74
|
const currentHandleState = ctx.eventController.getHandleState();
|
|
128
|
-
const currentCollected =
|
|
75
|
+
const currentCollected = collectHandleData(
|
|
129
76
|
handle,
|
|
130
77
|
currentHandleState.data,
|
|
131
78
|
currentHandleState.segmentOrder,
|
|
@@ -142,7 +89,11 @@ export function useHandle<T, A, S>(
|
|
|
142
89
|
const state = ctx.eventController.getHandleState();
|
|
143
90
|
const isAction =
|
|
144
91
|
ctx.eventController.getState().inflightActions.length > 0;
|
|
145
|
-
const collected =
|
|
92
|
+
const collected = collectHandleData(
|
|
93
|
+
handle,
|
|
94
|
+
state.data,
|
|
95
|
+
state.segmentOrder,
|
|
96
|
+
);
|
|
146
97
|
const nextValue = selectorRef.current
|
|
147
98
|
? selectorRef.current(collected)
|
|
148
99
|
: collected;
|