@rangojs/router 0.0.0-experimental.8678bb02 → 0.0.0-experimental.87
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 +126 -38
- package/dist/bin/rango.js +130 -47
- package/dist/vite/index.js +847 -384
- package/dist/vite/index.js.bak +5448 -0
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +5 -5
- package/skills/breadcrumbs/SKILL.md +3 -1
- 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 +35 -2
- 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 +59 -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 +24 -0
- package/skills/router-setup/SKILL.md +35 -0
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/typesafety/SKILL.md +3 -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/navigation-bridge.ts +87 -6
- package/src/browser/navigation-client.ts +128 -77
- package/src/browser/navigation-store.ts +68 -9
- package/src/browser/partial-update.ts +60 -7
- package/src/browser/prefetch/cache.ts +129 -21
- package/src/browser/prefetch/fetch.ts +156 -18
- package/src/browser/prefetch/queue.ts +36 -5
- package/src/browser/rango-state.ts +53 -13
- package/src/browser/react/Link.tsx +72 -8
- package/src/browser/react/NavigationProvider.tsx +57 -11
- 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 +60 -9
- package/src/browser/scroll-restoration.ts +10 -8
- package/src/browser/segment-reconciler.ts +36 -14
- package/src/browser/server-action-bridge.ts +8 -18
- package/src/browser/types.ts +33 -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 +211 -72
- package/src/build/route-types/scan-filter.ts +8 -1
- package/src/cache/cf/cf-cache-store.ts +5 -7
- package/src/client.tsx +84 -230
- package/src/deps/browser.ts +0 -1
- 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 +210 -35
- package/src/route-definition/helpers-types.ts +61 -14
- package/src/route-definition/index.ts +3 -0
- package/src/route-definition/redirect.ts +9 -1
- package/src/route-definition/resolve-handler-use.ts +155 -0
- package/src/route-types.ts +18 -0
- package/src/router/content-negotiation.ts +100 -1
- package/src/router/handler-context.ts +70 -17
- package/src/router/intercept-resolution.ts +9 -4
- package/src/router/lazy-includes.ts +6 -6
- package/src/router/loader-resolution.ts +153 -21
- package/src/router/manifest.ts +22 -13
- package/src/router/match-api.ts +127 -192
- 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 +2 -28
- package/src/router/middleware.ts +32 -7
- 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-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/trie-matching.ts +10 -4
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +54 -7
- package/src/rsc/handler.ts +478 -399
- package/src/rsc/helpers.ts +69 -41
- package/src/rsc/loader-fetch.ts +18 -3
- package/src/rsc/manifest-init.ts +5 -1
- package/src/rsc/progressive-enhancement.ts +14 -3
- package/src/rsc/response-route-handler.ts +14 -1
- package/src/rsc/rsc-rendering.ts +15 -2
- package/src/rsc/server-action.ts +10 -2
- package/src/rsc/ssr-setup.ts +2 -2
- package/src/rsc/types.ts +6 -4
- 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 +65 -5
- package/src/server/handle-store.ts +19 -0
- package/src/server/loader-registry.ts +9 -8
- package/src/server/request-context.ts +142 -55
- 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 -43
- package/src/types/loader-types.ts +37 -11
- package/src/types/request-scope.ts +126 -0
- package/src/types/route-entry.ts +12 -1
- package/src/types/segments.ts +1 -1
- package/src/urls/include-helper.ts +24 -14
- package/src/urls/path-helper-types.ts +39 -6
- package/src/urls/path-helper.ts +47 -12
- package/src/urls/pattern-types.ts +12 -0
- package/src/urls/response-types.ts +18 -16
- package/src/use-loader.tsx +77 -5
- package/src/vite/discovery/bundle-postprocess.ts +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/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-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 -206
- package/src/vite/plugins/refresh-cmd.ts +88 -26
- package/src/vite/rango.ts +40 -18
- package/src/vite/router-discovery.ts +237 -37
- package/src/vite/utils/banner.ts +1 -1
- package/src/vite/utils/package-resolution.ts +1 -1
- package/src/vite/utils/prerender-utils.ts +37 -5
- package/src/vite/utils/shared-utils.ts +3 -2
- package/src/browser/debug-channel.ts +0 -93
|
@@ -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,
|
|
@@ -95,6 +97,31 @@ export interface LinkProps extends Omit<
|
|
|
95
97
|
* @default "none"
|
|
96
98
|
*/
|
|
97
99
|
prefetch?: PrefetchStrategy;
|
|
100
|
+
/**
|
|
101
|
+
* Opt-in override for the prefetch cache scope.
|
|
102
|
+
*
|
|
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.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```tsx
|
|
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" />
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
prefetchKey?: ":source";
|
|
98
125
|
/**
|
|
99
126
|
* State to pass to history.pushState/replaceState.
|
|
100
127
|
* Accessible via useLocationState() hook.
|
|
@@ -182,6 +209,7 @@ export const Link: ForwardRefExoticComponent<
|
|
|
182
209
|
reloadDocument = false,
|
|
183
210
|
revalidate,
|
|
184
211
|
prefetch = "none",
|
|
212
|
+
prefetchKey,
|
|
185
213
|
state,
|
|
186
214
|
children,
|
|
187
215
|
onClick,
|
|
@@ -192,6 +220,16 @@ export const Link: ForwardRefExoticComponent<
|
|
|
192
220
|
const ctx = useContext(NavigationStoreContext);
|
|
193
221
|
const isExternal = isExternalUrl(to);
|
|
194
222
|
|
|
223
|
+
// Auto-prefix with basename for app-local paths.
|
|
224
|
+
// Skip if external, already prefixed, or not a root-relative path.
|
|
225
|
+
const resolvedTo = useMemo(() => {
|
|
226
|
+
if (isExternal) return to;
|
|
227
|
+
const bn = ctx?.basename;
|
|
228
|
+
if (!bn || !to.startsWith("/") || to.startsWith(bn + "/") || to === bn)
|
|
229
|
+
return to;
|
|
230
|
+
return to === "/" ? bn : bn + to;
|
|
231
|
+
}, [to, isExternal, ctx?.basename]);
|
|
232
|
+
|
|
195
233
|
// Resolve adaptive: viewport on touch devices, hover on pointer devices
|
|
196
234
|
const resolvedStrategy =
|
|
197
235
|
prefetch === "adaptive" ? (isTouchDevice ? "viewport" : "hover") : prefetch;
|
|
@@ -273,9 +311,23 @@ export const Link: ForwardRefExoticComponent<
|
|
|
273
311
|
resolvedState = currentState;
|
|
274
312
|
}
|
|
275
313
|
|
|
276
|
-
ctx.navigate(
|
|
314
|
+
ctx.navigate(resolvedTo, {
|
|
315
|
+
replace,
|
|
316
|
+
scroll,
|
|
317
|
+
state: resolvedState,
|
|
318
|
+
revalidate,
|
|
319
|
+
});
|
|
277
320
|
},
|
|
278
|
-
[
|
|
321
|
+
[
|
|
322
|
+
resolvedTo,
|
|
323
|
+
isExternal,
|
|
324
|
+
reloadDocument,
|
|
325
|
+
replace,
|
|
326
|
+
scroll,
|
|
327
|
+
revalidate,
|
|
328
|
+
ctx,
|
|
329
|
+
onClick,
|
|
330
|
+
],
|
|
279
331
|
);
|
|
280
332
|
|
|
281
333
|
const handleMouseEnter = useCallback(() => {
|
|
@@ -289,9 +341,15 @@ export const Link: ForwardRefExoticComponent<
|
|
|
289
341
|
// prefetch — prefetchDirect bypasses the queue, and hasPrefetch
|
|
290
342
|
// deduplicates if the viewport prefetch already completed.
|
|
291
343
|
const segmentState = ctx.store.getSegmentState();
|
|
292
|
-
prefetchDirect(
|
|
344
|
+
prefetchDirect(
|
|
345
|
+
resolvedTo,
|
|
346
|
+
segmentState.currentSegmentIds,
|
|
347
|
+
getAppVersion(),
|
|
348
|
+
ctx.store.getRouterId?.(),
|
|
349
|
+
prefetchKey,
|
|
350
|
+
);
|
|
293
351
|
}
|
|
294
|
-
}, [resolvedStrategy,
|
|
352
|
+
}, [resolvedStrategy, resolvedTo, isExternal, ctx, prefetchKey]);
|
|
295
353
|
|
|
296
354
|
// Viewport/render prefetch: waits for idle before starting,
|
|
297
355
|
// uses concurrency-limited queue to avoid flooding.
|
|
@@ -308,7 +366,13 @@ export const Link: ForwardRefExoticComponent<
|
|
|
308
366
|
const triggerPrefetch = () => {
|
|
309
367
|
if (cancelled) return;
|
|
310
368
|
const segmentState = ctx.store.getSegmentState();
|
|
311
|
-
prefetchQueued(
|
|
369
|
+
prefetchQueued(
|
|
370
|
+
resolvedTo,
|
|
371
|
+
segmentState.currentSegmentIds,
|
|
372
|
+
getAppVersion(),
|
|
373
|
+
ctx.store.getRouterId?.(),
|
|
374
|
+
prefetchKey,
|
|
375
|
+
);
|
|
312
376
|
};
|
|
313
377
|
|
|
314
378
|
// Schedule prefetch only when the app is idle (no navigation/streaming).
|
|
@@ -347,12 +411,12 @@ export const Link: ForwardRefExoticComponent<
|
|
|
347
411
|
unobserveForPrefetch(observedElement);
|
|
348
412
|
}
|
|
349
413
|
};
|
|
350
|
-
}, [resolvedStrategy,
|
|
414
|
+
}, [resolvedStrategy, resolvedTo, isExternal, ctx, prefetchKey]);
|
|
351
415
|
|
|
352
416
|
return (
|
|
353
417
|
<a
|
|
354
418
|
ref={setRef}
|
|
355
|
-
href={
|
|
419
|
+
href={resolvedTo}
|
|
356
420
|
onClick={handleClick}
|
|
357
421
|
onMouseEnter={handleMouseEnter}
|
|
358
422
|
data-link-component
|
|
@@ -362,7 +426,7 @@ export const Link: ForwardRefExoticComponent<
|
|
|
362
426
|
data-revalidate={revalidate === false ? "false" : undefined}
|
|
363
427
|
{...props}
|
|
364
428
|
>
|
|
365
|
-
<LinkContext.Provider value={
|
|
429
|
+
<LinkContext.Provider value={resolvedTo}>{children}</LinkContext.Provider>
|
|
366
430
|
</a>
|
|
367
431
|
);
|
|
368
432
|
});
|
|
@@ -28,6 +28,7 @@ import { NonceContext } from "./nonce-context.js";
|
|
|
28
28
|
import type { ResolvedThemeConfig, Theme } from "../../theme/types.js";
|
|
29
29
|
import { cancelAllPrefetches } from "../prefetch/queue.js";
|
|
30
30
|
import { handleNavigationEnd } from "../scroll-restoration.js";
|
|
31
|
+
import type { AppShellRef } from "../app-shell.js";
|
|
31
32
|
|
|
32
33
|
/**
|
|
33
34
|
* Process handles from an async generator, updating the event controller
|
|
@@ -133,10 +134,23 @@ export interface NavigationProviderProps {
|
|
|
133
134
|
warmupEnabled?: boolean;
|
|
134
135
|
|
|
135
136
|
/**
|
|
136
|
-
* App version from server payload
|
|
137
|
-
*
|
|
137
|
+
* App version from server payload.
|
|
138
|
+
* Used only as a fallback when `appShellRef` is not supplied.
|
|
138
139
|
*/
|
|
139
140
|
version?: string;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* URL prefix for all routes (from createRouter({ basename })).
|
|
144
|
+
* Used only as a fallback when `appShellRef` is not supplied.
|
|
145
|
+
*/
|
|
146
|
+
basename?: string;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Live app-shell ref. When provided, the context's `basename` and `version`
|
|
150
|
+
* properties become live getters that track app-switch updates without
|
|
151
|
+
* invalidating the memoized context value.
|
|
152
|
+
*/
|
|
153
|
+
appShellRef?: AppShellRef;
|
|
140
154
|
}
|
|
141
155
|
|
|
142
156
|
/**
|
|
@@ -169,6 +183,8 @@ export function NavigationProvider({
|
|
|
169
183
|
initialTheme,
|
|
170
184
|
warmupEnabled,
|
|
171
185
|
version,
|
|
186
|
+
basename,
|
|
187
|
+
appShellRef,
|
|
172
188
|
}: NavigationProviderProps): ReactNode {
|
|
173
189
|
// Track current payload for rendering (this triggers re-renders)
|
|
174
190
|
const [payload, setPayload] = useState(initialPayload);
|
|
@@ -190,17 +206,39 @@ export function NavigationProvider({
|
|
|
190
206
|
await bridge.refresh();
|
|
191
207
|
}, []);
|
|
192
208
|
|
|
193
|
-
// Context value is stable (store, eventController, navigate, refresh never
|
|
194
|
-
|
|
195
|
-
|
|
209
|
+
// Context value is stable (store, eventController, navigate, refresh never
|
|
210
|
+
// change). When an appShellRef is supplied, `basename` and `version` are
|
|
211
|
+
// installed as live getters so app-switch transitions (which update the ref)
|
|
212
|
+
// propagate to consumers without forcing a tree-wide rerender.
|
|
213
|
+
const contextValue = useMemo<NavigationStoreContextValue>(() => {
|
|
214
|
+
if (appShellRef) {
|
|
215
|
+
const value = {
|
|
216
|
+
store,
|
|
217
|
+
eventController,
|
|
218
|
+
navigate,
|
|
219
|
+
refresh,
|
|
220
|
+
} as NavigationStoreContextValue;
|
|
221
|
+
Object.defineProperty(value, "basename", {
|
|
222
|
+
configurable: true,
|
|
223
|
+
enumerable: true,
|
|
224
|
+
get: () => appShellRef.get().basename,
|
|
225
|
+
});
|
|
226
|
+
Object.defineProperty(value, "version", {
|
|
227
|
+
configurable: true,
|
|
228
|
+
enumerable: true,
|
|
229
|
+
get: () => appShellRef.get().version,
|
|
230
|
+
});
|
|
231
|
+
return value;
|
|
232
|
+
}
|
|
233
|
+
return {
|
|
196
234
|
store,
|
|
197
235
|
eventController,
|
|
198
236
|
navigate,
|
|
199
237
|
refresh,
|
|
200
238
|
version,
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
);
|
|
239
|
+
basename,
|
|
240
|
+
};
|
|
241
|
+
}, []);
|
|
204
242
|
|
|
205
243
|
// Connection warmup: keep TLS alive after idle periods.
|
|
206
244
|
// After 60s of no user interaction, marks connection as "cold".
|
|
@@ -338,8 +376,12 @@ export function NavigationProvider({
|
|
|
338
376
|
metadata: update.metadata,
|
|
339
377
|
});
|
|
340
378
|
|
|
341
|
-
// Update route params
|
|
342
|
-
|
|
379
|
+
// Update route params. Only reset when the server actually sends a params
|
|
380
|
+
// map — an absent `params` field means "no change" (e.g., legacy action
|
|
381
|
+
// responses that omitted params). Explicit `{}` still clears correctly.
|
|
382
|
+
if (update.metadata.params !== undefined) {
|
|
383
|
+
eventController.setParams(update.metadata.params);
|
|
384
|
+
}
|
|
343
385
|
|
|
344
386
|
// Update handle data progressively as it streams in
|
|
345
387
|
if (update.metadata.handles) {
|
|
@@ -391,7 +433,11 @@ export function NavigationProvider({
|
|
|
391
433
|
// Build the content tree
|
|
392
434
|
let content = <RootErrorBoundary>{root}</RootErrorBoundary>;
|
|
393
435
|
|
|
394
|
-
// Wrap with ThemeProvider when theme is enabled
|
|
436
|
+
// Wrap with ThemeProvider when theme is enabled. The ThemeProvider is
|
|
437
|
+
// document-lifetime: its config comes from the initial load and does NOT
|
|
438
|
+
// swap on cross-app transitions, because the ThemeProvider sits above the
|
|
439
|
+
// segment tree and a smooth (no-reload) app switch cannot safely remount
|
|
440
|
+
// it. A new theme config only takes effect on a full document load.
|
|
395
441
|
if (themeConfig) {
|
|
396
442
|
content = (
|
|
397
443
|
<ThemeProvider config={themeConfig} initialTheme={initialTheme}>
|
|
@@ -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;
|
|
@@ -53,6 +53,12 @@ export function useNavigation<T>(
|
|
|
53
53
|
});
|
|
54
54
|
const prevState = useRef(baseValue);
|
|
55
55
|
|
|
56
|
+
// Tracks whether the most recent setOptimisticValue call pinned the value
|
|
57
|
+
// to a non-idle state. Used to decide whether to emit a release update when
|
|
58
|
+
// returning to idle, so the optimistic store doesn't stay pinned if a
|
|
59
|
+
// parent transition (e.g. <Link> click) is still pending.
|
|
60
|
+
const optimisticPinnedRef = useRef(false);
|
|
61
|
+
|
|
56
62
|
// useOptimistic allows immediate updates during transitions/actions
|
|
57
63
|
const [value, setOptimisticValue] = useOptimistic(baseValue);
|
|
58
64
|
|
|
@@ -82,11 +88,25 @@ export function useNavigation<T>(
|
|
|
82
88
|
const hasInflightActions =
|
|
83
89
|
ctx.eventController.getInflightActions().size > 0;
|
|
84
90
|
|
|
85
|
-
|
|
86
|
-
|
|
91
|
+
const shouldPin = hasInflightActions || publicState.state !== "idle";
|
|
92
|
+
|
|
93
|
+
if (shouldPin) {
|
|
94
|
+
// Pin the optimistic store so the loading value shows immediately
|
|
95
|
+
// even if a parent transition (e.g. <Link> click) defers the
|
|
96
|
+
// urgent setBaseValue commit.
|
|
97
|
+
startTransition(() => {
|
|
98
|
+
setOptimisticValue(nextSelected);
|
|
99
|
+
});
|
|
100
|
+
optimisticPinnedRef.current = true;
|
|
101
|
+
} else if (optimisticPinnedRef.current) {
|
|
102
|
+
// Release a previously-pinned optimistic value. Without this,
|
|
103
|
+
// useOptimistic keeps returning the stale loading value while
|
|
104
|
+
// any parent transition is still pending, even after baseValue
|
|
105
|
+
// flipped to idle.
|
|
87
106
|
startTransition(() => {
|
|
88
107
|
setOptimisticValue(nextSelected);
|
|
89
108
|
});
|
|
109
|
+
optimisticPinnedRef.current = false;
|
|
90
110
|
}
|
|
91
111
|
|
|
92
112
|
// Always update base state so UI reflects current state
|
|
@@ -16,11 +16,21 @@ import { shallowEqual } from "./shallow-equal.js";
|
|
|
16
16
|
* const params = useParams();
|
|
17
17
|
* // { productId: "123" }
|
|
18
18
|
*
|
|
19
|
+
* // Annotate the expected shape via a generic
|
|
20
|
+
* const { productId } = useParams<{ productId: string }>();
|
|
21
|
+
*
|
|
19
22
|
* // With selector
|
|
20
23
|
* const productId = useParams(p => p.productId);
|
|
21
24
|
* ```
|
|
22
25
|
*/
|
|
23
|
-
|
|
26
|
+
// `T extends object` (not `Record<string, string | undefined>`) so that
|
|
27
|
+
// interface shapes pass the constraint — interfaces lack an implicit
|
|
28
|
+
// index signature and would otherwise be rejected. The generic is a
|
|
29
|
+
// shape annotation, not a runtime check; the body always returns the
|
|
30
|
+
// underlying params map unchanged.
|
|
31
|
+
export function useParams<
|
|
32
|
+
T extends object = Record<string, string>,
|
|
33
|
+
>(): Readonly<T>;
|
|
24
34
|
export function useParams<T>(
|
|
25
35
|
selector: (params: Record<string, string>) => T,
|
|
26
36
|
): T;
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { useContext, useMemo } from "react";
|
|
4
4
|
import { NavigationStoreContext } from "./context.js";
|
|
5
5
|
import { prefetchDirect } from "../prefetch/fetch.js";
|
|
6
|
+
import { getAppVersion } from "../app-version.js";
|
|
6
7
|
import type { RouterInstance, RouterNavigateOptions } from "../types.js";
|
|
7
8
|
|
|
8
9
|
/**
|
|
@@ -12,6 +13,10 @@ import type { RouterInstance, RouterNavigateOptions } from "../types.js";
|
|
|
12
13
|
* useRouter() do not re-render on navigation state changes.
|
|
13
14
|
* For reactive navigation state, use useNavigation() instead.
|
|
14
15
|
*
|
|
16
|
+
* Methods read `basename` from the live context on each call so that
|
|
17
|
+
* cross-app navigation (app-switch) sees the current app's basename
|
|
18
|
+
* rather than the one captured at mount time.
|
|
19
|
+
*
|
|
15
20
|
* @example
|
|
16
21
|
* ```tsx
|
|
17
22
|
* const router = useRouter();
|
|
@@ -28,15 +33,26 @@ export function useRouter(): RouterInstance {
|
|
|
28
33
|
throw new Error("useRouter must be used within NavigationProvider");
|
|
29
34
|
}
|
|
30
35
|
|
|
31
|
-
// Stable reference: ctx is
|
|
32
|
-
|
|
33
|
-
|
|
36
|
+
// Stable reference: ctx itself is stable, and reads on each method call
|
|
37
|
+
// pick up live basename values from the context (backed by a live ref
|
|
38
|
+
// in NavigationProvider), so app-switch transitions are reflected without
|
|
39
|
+
// recreating this object.
|
|
40
|
+
return useMemo<RouterInstance>(() => {
|
|
41
|
+
/** Prefix a root-relative path with basename if not already prefixed. */
|
|
42
|
+
function withBasename(url: string): string {
|
|
43
|
+
const bn = ctx!.basename;
|
|
44
|
+
if (!bn || !url.startsWith("/") || url.startsWith(bn + "/") || url === bn)
|
|
45
|
+
return url;
|
|
46
|
+
return url === "/" ? bn : bn + url;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return {
|
|
34
50
|
push(url: string, options?: RouterNavigateOptions): Promise<void> {
|
|
35
|
-
return ctx.navigate(url, { ...options, replace: false });
|
|
51
|
+
return ctx.navigate(withBasename(url), { ...options, replace: false });
|
|
36
52
|
},
|
|
37
53
|
|
|
38
54
|
replace(url: string, options?: RouterNavigateOptions): Promise<void> {
|
|
39
|
-
return ctx.navigate(url, { ...options, replace: true });
|
|
55
|
+
return ctx.navigate(withBasename(url), { ...options, replace: true });
|
|
40
56
|
},
|
|
41
57
|
|
|
42
58
|
refresh(): Promise<void> {
|
|
@@ -46,7 +62,12 @@ export function useRouter(): RouterInstance {
|
|
|
46
62
|
prefetch(url: string): void {
|
|
47
63
|
const segmentState = ctx.store?.getSegmentState();
|
|
48
64
|
if (segmentState) {
|
|
49
|
-
prefetchDirect(
|
|
65
|
+
prefetchDirect(
|
|
66
|
+
withBasename(url),
|
|
67
|
+
segmentState.currentSegmentIds,
|
|
68
|
+
getAppVersion(),
|
|
69
|
+
ctx.store?.getRouterId?.(),
|
|
70
|
+
);
|
|
50
71
|
}
|
|
51
72
|
},
|
|
52
73
|
|
|
@@ -57,7 +78,6 @@ export function useRouter(): RouterInstance {
|
|
|
57
78
|
forward(): void {
|
|
58
79
|
window.history.forward();
|
|
59
80
|
},
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
);
|
|
81
|
+
};
|
|
82
|
+
}, []);
|
|
63
83
|
}
|