@rangojs/router 0.0.0-experimental.66 → 0.0.0-experimental.66cdebe3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +112 -17
- package/dist/vite/index.js +1462 -422
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +7 -5
- package/skills/breadcrumbs/SKILL.md +3 -1
- package/skills/handler-use/SKILL.md +364 -0
- package/skills/hooks/SKILL.md +54 -20
- package/skills/i18n/SKILL.md +276 -0
- package/skills/intercept/SKILL.md +45 -0
- package/skills/layout/SKILL.md +24 -0
- package/skills/links/SKILL.md +234 -16
- package/skills/loader/SKILL.md +70 -3
- package/skills/middleware/SKILL.md +34 -3
- package/skills/migrate-nextjs/SKILL.md +562 -0
- package/skills/migrate-react-router/SKILL.md +769 -0
- package/skills/parallel/SKILL.md +68 -0
- package/skills/rango/SKILL.md +26 -22
- package/skills/response-routes/SKILL.md +8 -0
- package/skills/route/SKILL.md +48 -0
- package/skills/server-actions/SKILL.md +739 -0
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/typesafety/SKILL.md +9 -1
- package/skills/view-transitions/SKILL.md +212 -0
- package/src/browser/app-shell.ts +52 -0
- package/src/browser/event-controller.ts +44 -4
- package/src/browser/navigation-bridge.ts +151 -9
- package/src/browser/navigation-client.ts +64 -13
- package/src/browser/navigation-store.ts +25 -1
- package/src/browser/partial-update.ts +58 -12
- package/src/browser/prefetch/cache.ts +129 -21
- package/src/browser/prefetch/fetch.ts +148 -16
- package/src/browser/prefetch/queue.ts +36 -5
- package/src/browser/rango-state.ts +53 -13
- package/src/browser/react/Link.tsx +30 -2
- package/src/browser/react/NavigationProvider.tsx +95 -44
- package/src/browser/react/filter-segment-order.ts +51 -7
- package/src/browser/react/index.ts +3 -0
- package/src/browser/react/use-navigation.ts +22 -2
- package/src/browser/react/use-params.ts +17 -4
- package/src/browser/react/use-reverse.ts +99 -0
- package/src/browser/react/use-router.ts +8 -1
- package/src/browser/react/use-segments.ts +11 -8
- package/src/browser/rsc-router.tsx +34 -6
- package/src/browser/scroll-restoration.ts +69 -28
- package/src/browser/segment-reconciler.ts +36 -14
- package/src/browser/types.ts +19 -0
- package/src/build/route-trie.ts +52 -25
- package/src/cache/cf/cf-cache-store.ts +5 -7
- package/src/client.rsc.tsx +3 -0
- package/src/client.tsx +87 -175
- package/src/href-client.ts +4 -1
- package/src/index.rsc.ts +3 -0
- package/src/index.ts +44 -9
- package/src/outlet-context.ts +1 -1
- package/src/response-utils.ts +28 -0
- package/src/reverse.ts +62 -36
- package/src/route-definition/dsl-helpers.ts +175 -23
- package/src/route-definition/helpers-types.ts +63 -14
- package/src/route-definition/resolve-handler-use.ts +6 -0
- package/src/route-types.ts +7 -0
- package/src/router/handler-context.ts +21 -38
- package/src/router/lazy-includes.ts +6 -6
- package/src/router/loader-resolution.ts +3 -0
- package/src/router/manifest.ts +22 -13
- package/src/router/match-api.ts +4 -3
- package/src/router/match-handlers.ts +1 -0
- package/src/router/match-middleware/cache-lookup.ts +2 -1
- package/src/router/match-result.ts +101 -4
- package/src/router/middleware-types.ts +14 -25
- package/src/router/middleware.ts +54 -7
- package/src/router/pattern-matching.ts +101 -17
- package/src/router/revalidation.ts +15 -1
- package/src/router/segment-resolution/fresh.ts +13 -0
- package/src/router/segment-resolution/revalidation.ts +135 -101
- package/src/router/substitute-pattern-params.ts +56 -0
- package/src/router/trie-matching.ts +18 -13
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +1 -2
- package/src/rsc/handler.ts +16 -8
- package/src/rsc/helpers.ts +69 -41
- package/src/rsc/progressive-enhancement.ts +4 -0
- package/src/rsc/response-route-handler.ts +14 -1
- package/src/rsc/rsc-rendering.ts +10 -0
- package/src/rsc/server-action.ts +4 -0
- package/src/rsc/types.ts +6 -0
- package/src/segment-content-promise.ts +67 -0
- package/src/segment-loader-promise.ts +122 -0
- package/src/segment-system.tsx +71 -70
- package/src/server/context.ts +26 -3
- package/src/server/request-context.ts +10 -42
- package/src/ssr/index.tsx +5 -1
- package/src/types/handler-context.ts +12 -39
- package/src/types/loader-types.ts +5 -6
- package/src/types/request-scope.ts +126 -0
- package/src/types/route-entry.ts +11 -0
- package/src/types/segments.ts +18 -1
- package/src/urls/include-helper.ts +24 -14
- package/src/urls/path-helper-types.ts +30 -4
- package/src/urls/response-types.ts +2 -10
- package/src/use-loader.tsx +4 -1
- package/src/vite/debug.ts +184 -0
- package/src/vite/discovery/discover-routers.ts +31 -3
- package/src/vite/discovery/gate-state.ts +171 -0
- package/src/vite/discovery/prerender-collection.ts +172 -84
- package/src/vite/discovery/self-gen-tracking.ts +27 -1
- package/src/vite/plugins/cjs-to-esm.ts +5 -0
- package/src/vite/plugins/client-ref-dedup.ts +16 -0
- package/src/vite/plugins/client-ref-hashing.ts +16 -4
- package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
- package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
- package/src/vite/plugins/expose-action-id.ts +52 -28
- package/src/vite/plugins/expose-id-utils.ts +12 -0
- package/src/vite/plugins/expose-ids/router-transform.ts +20 -3
- package/src/vite/plugins/expose-internal-ids.ts +545 -304
- package/src/vite/plugins/performance-tracks.ts +17 -9
- package/src/vite/plugins/use-cache-transform.ts +56 -43
- package/src/vite/plugins/version-injector.ts +37 -11
- package/src/vite/rango.ts +49 -14
- package/src/vite/router-discovery.ts +558 -53
- package/src/vite/utils/banner.ts +1 -1
- package/src/vite/utils/package-resolution.ts +41 -1
- package/src/vite/utils/prerender-utils.ts +21 -6
|
@@ -5,6 +5,8 @@ import type {
|
|
|
5
5
|
ResolvedSegment,
|
|
6
6
|
} from "./types.js";
|
|
7
7
|
import { setAppVersion } from "./app-version.js";
|
|
8
|
+
import { setRangoStateLocal } from "./rango-state.js";
|
|
9
|
+
import type { AppShell, AppShellRef } from "./app-shell.js";
|
|
8
10
|
import * as React from "react";
|
|
9
11
|
import { startTransition } from "react";
|
|
10
12
|
import {
|
|
@@ -48,8 +50,13 @@ export { createNavigationTransaction };
|
|
|
48
50
|
*/
|
|
49
51
|
export interface NavigationBridgeConfigWithController extends NavigationBridgeConfig {
|
|
50
52
|
eventController: EventController;
|
|
51
|
-
/** RSC version from initial payload metadata */
|
|
53
|
+
/** RSC version from initial payload metadata (fallback when appShellRef is not provided) */
|
|
52
54
|
version?: string;
|
|
55
|
+
/**
|
|
56
|
+
* Live app-shell ref. When supplied, the bridge reads version/basename
|
|
57
|
+
* from this ref so cross-app navigations propagate correctly.
|
|
58
|
+
*/
|
|
59
|
+
appShellRef?: AppShellRef;
|
|
53
60
|
}
|
|
54
61
|
|
|
55
62
|
/**
|
|
@@ -68,9 +75,46 @@ export interface NavigationBridgeConfigWithController extends NavigationBridgeCo
|
|
|
68
75
|
export function createNavigationBridge(
|
|
69
76
|
config: NavigationBridgeConfigWithController,
|
|
70
77
|
): NavigationBridge {
|
|
71
|
-
const {
|
|
78
|
+
const {
|
|
79
|
+
store,
|
|
80
|
+
client,
|
|
81
|
+
eventController,
|
|
82
|
+
onUpdate,
|
|
83
|
+
renderSegments,
|
|
84
|
+
appShellRef,
|
|
85
|
+
} = config;
|
|
72
86
|
let version = config.version;
|
|
73
87
|
|
|
88
|
+
/**
|
|
89
|
+
* Replace the active app-shell snapshot atomically. Called by the partial
|
|
90
|
+
* updater when a response's routerId indicates the navigation crossed
|
|
91
|
+
* into a different app. Runs the local-only side-effects tied to
|
|
92
|
+
* app-shell fields (app version, rango-state namespace) so the new app
|
|
93
|
+
* owns them after the swap. Theme, warmup, and prefetch TTL are
|
|
94
|
+
* document-lifetime and are NOT touched here.
|
|
95
|
+
*/
|
|
96
|
+
function applyAppShell(next: AppShell): void {
|
|
97
|
+
if (appShellRef) {
|
|
98
|
+
appShellRef.update(next);
|
|
99
|
+
}
|
|
100
|
+
if (next.version !== undefined) {
|
|
101
|
+
version = next.version;
|
|
102
|
+
setAppVersion(next.version);
|
|
103
|
+
// Use the local-only setter — initRangoState writes the shared
|
|
104
|
+
// localStorage key and fires a storage event in other tabs still in
|
|
105
|
+
// the old app. setRangoStateLocal only mutates this tab's in-memory
|
|
106
|
+
// cache and rebinds it to the target app's routerId-scoped key,
|
|
107
|
+
// preserving the "local-only, no broadcast/rotation" contract for
|
|
108
|
+
// smooth app-switch transitions.
|
|
109
|
+
setRangoStateLocal(next.version, next.routerId);
|
|
110
|
+
}
|
|
111
|
+
// Cross-app: prior cache entries belong to a different app's segments.
|
|
112
|
+
// Drop them locally only — do NOT broadcast invalidation or rotate the
|
|
113
|
+
// shared X-Rango-State token, since other tabs still in the old app are
|
|
114
|
+
// unaffected by this tab's transition.
|
|
115
|
+
store.clearHistoryCacheLocal();
|
|
116
|
+
}
|
|
117
|
+
|
|
74
118
|
// Create shared partial updater
|
|
75
119
|
const fetchPartialUpdate = createPartialUpdater({
|
|
76
120
|
store,
|
|
@@ -78,6 +122,7 @@ export function createNavigationBridge(
|
|
|
78
122
|
onUpdate,
|
|
79
123
|
renderSegments,
|
|
80
124
|
getVersion: () => version,
|
|
125
|
+
applyAppShell,
|
|
81
126
|
});
|
|
82
127
|
|
|
83
128
|
return {
|
|
@@ -261,18 +306,24 @@ export function createNavigationBridge(
|
|
|
261
306
|
// 2. routes that CAN be intercepted - we don't know if this navigation will intercept
|
|
262
307
|
// 3. when leaving intercept - we need fresh non-intercept segments from server
|
|
263
308
|
// 4. redirect-with-state - force re-render so hooks read fresh state
|
|
309
|
+
// 5. stale cache - server action invalidated it, need fresh data with loading state
|
|
264
310
|
const hasUsableCache =
|
|
265
311
|
cachedSegments &&
|
|
266
312
|
cachedSegments.length > 0 &&
|
|
267
313
|
!isInterceptOnlyCache(cachedSegments) &&
|
|
268
314
|
!hasInterceptCache &&
|
|
269
315
|
!isLeavingIntercept &&
|
|
316
|
+
!cached?.stale &&
|
|
270
317
|
!options?._skipCache;
|
|
271
318
|
|
|
319
|
+
// Forward navigations always await fetchPartialUpdate before rendering,
|
|
320
|
+
// so useNavigation should always report "loading". skipLoadingState is
|
|
321
|
+
// only used for popstate background revalidation (line ~526) where
|
|
322
|
+
// cached content renders instantly without a network wait.
|
|
272
323
|
const tx = createNavigationTransaction(store, eventController, url, {
|
|
273
324
|
...options,
|
|
274
325
|
state: resolvedState,
|
|
275
|
-
skipLoadingState:
|
|
326
|
+
skipLoadingState: false,
|
|
276
327
|
});
|
|
277
328
|
|
|
278
329
|
// REVALIDATE: Fetch fresh data from server
|
|
@@ -412,6 +463,15 @@ export function createNavigationBridge(
|
|
|
412
463
|
eventController.abortAllActions();
|
|
413
464
|
}
|
|
414
465
|
|
|
466
|
+
// Popstate that exits an intercept to a non-intercept destination. The
|
|
467
|
+
// fallback fetch path below needs `leave-intercept` mode so it filters
|
|
468
|
+
// the cached @modal segment from the request and forces a re-render —
|
|
469
|
+
// otherwise a cache-miss popstate whose server response has an empty
|
|
470
|
+
// diff hits the "no changes" branch in partial-update and the modal
|
|
471
|
+
// stays on screen.
|
|
472
|
+
const isLeavingIntercept =
|
|
473
|
+
!isIntercept && currentInterceptSource !== null;
|
|
474
|
+
|
|
415
475
|
// Compute history key from URL (with intercept suffix if applicable)
|
|
416
476
|
const historyKey = generateHistoryKey(url, { intercept: isIntercept });
|
|
417
477
|
|
|
@@ -479,9 +539,25 @@ export function createNavigationBridge(
|
|
|
479
539
|
cachedHandleData,
|
|
480
540
|
params: cachedParams,
|
|
481
541
|
},
|
|
482
|
-
|
|
542
|
+
// Cache-restore: render is from local cache, no network
|
|
543
|
+
// stream to wait for. Don't pass isStreaming — otherwise
|
|
544
|
+
// restoreScrollPosition enters its retryIfStreaming polling
|
|
545
|
+
// branch and silently never fires scrollTo if the streaming
|
|
546
|
+
// flag stays true (e.g., during a view transition or stale
|
|
547
|
+
// token lifecycle), leaving the page at whatever scroll the
|
|
548
|
+
// VT pseudo-elements left behind. The fetch-fallback path
|
|
549
|
+
// below DOES still pass isStreaming because it awaits a real
|
|
550
|
+
// fetch that may stream.
|
|
551
|
+
scroll: { restore: true },
|
|
483
552
|
};
|
|
484
|
-
|
|
553
|
+
// Intercept-driven popstate (entering OR leaving an intercept) only
|
|
554
|
+
// mutates the parallel slot; the main outlet shows the same content.
|
|
555
|
+
// Skip startViewTransition in those cases — same rationale as the
|
|
556
|
+
// intercept guard in partial-update.ts's hasTransition computation.
|
|
557
|
+
const hasTransition =
|
|
558
|
+
!isIntercept &&
|
|
559
|
+
!isLeavingIntercept &&
|
|
560
|
+
cachedSegments.some((s) => s.transition);
|
|
485
561
|
if (hasTransition) {
|
|
486
562
|
startTransition(() => {
|
|
487
563
|
if (addTransitionType) {
|
|
@@ -562,7 +638,11 @@ export function createNavigationBridge(
|
|
|
562
638
|
intercept: isIntercept,
|
|
563
639
|
interceptSourceUrl,
|
|
564
640
|
}),
|
|
565
|
-
isIntercept
|
|
641
|
+
isIntercept
|
|
642
|
+
? { type: "navigate", interceptSourceUrl }
|
|
643
|
+
: isLeavingIntercept
|
|
644
|
+
? { type: "leave-intercept" }
|
|
645
|
+
: undefined,
|
|
566
646
|
);
|
|
567
647
|
// Restore scroll position after fetch completes
|
|
568
648
|
handleNavigationEnd({ restore: true, isStreaming });
|
|
@@ -604,6 +684,55 @@ export function createNavigationBridge(
|
|
|
604
684
|
this.handlePopstate();
|
|
605
685
|
};
|
|
606
686
|
|
|
687
|
+
// React's experimental ViewTransition integration deliberately skips
|
|
688
|
+
// animations for commits originating from a popstate event handler —
|
|
689
|
+
// popstate must finish synchronously to preserve scroll/form
|
|
690
|
+
// restoration, which conflicts with running a transition. The
|
|
691
|
+
// Navigation API's `navigate` event runs in an async-safe context
|
|
692
|
+
// (`event.intercept({ handler })` lets us await), so commits made
|
|
693
|
+
// inside the intercept handler are NOT popstate-originated from
|
|
694
|
+
// React's perspective and the VT walker fires normally for
|
|
695
|
+
// back-/forward-navigations. Falls back to popstate on browsers
|
|
696
|
+
// without Navigation API support (Firefox today); on those browsers
|
|
697
|
+
// back-nav view transitions won't fire — matching the React
|
|
698
|
+
// limitation and current behavior.
|
|
699
|
+
// See https://react.dev/reference/react/ViewTransition.
|
|
700
|
+
const navigationApi: any = (window as any).navigation;
|
|
701
|
+
const supportsNavigationApi =
|
|
702
|
+
!!navigationApi && typeof navigationApi.addEventListener === "function";
|
|
703
|
+
|
|
704
|
+
const handleNavigateEvent = (event: any): void => {
|
|
705
|
+
// Only handle browser history traversal (back/forward).
|
|
706
|
+
// Push/replace are still driven by setupLinkInterception →
|
|
707
|
+
// this.navigate(...) (which calls history.pushState/replaceState).
|
|
708
|
+
if (event.navigationType !== "traverse") return;
|
|
709
|
+
if (!event.canIntercept) return;
|
|
710
|
+
// canIntercept doesn't exclude every cross-document case (e.g., back
|
|
711
|
+
// to a previous same-origin non-Rango document, or a doc-level
|
|
712
|
+
// history.go() target). Without this guard, event.intercept() would
|
|
713
|
+
// forcibly turn that into a same-document navigation and route it
|
|
714
|
+
// through handlePopstate — silently breaking the destination page.
|
|
715
|
+
if (!event.destination?.sameDocument) return;
|
|
716
|
+
if (event.hashChange || event.downloadRequest) return;
|
|
717
|
+
// Known limitation: Chromium's Navigation API replaces the entry's
|
|
718
|
+
// history.state with `null` when an intercepted traverse commits,
|
|
719
|
+
// which clobbers the scroll-restoration `key` that rango stamps
|
|
720
|
+
// onto each history entry. Attempts to write the state back from
|
|
721
|
+
// the handler, finally, or event.finished all run after React's
|
|
722
|
+
// commit-phase useLayoutEffect has already read history.state, so
|
|
723
|
+
// none of them help. Consumers who rely on per-entry scroll
|
|
724
|
+
// restoration should use a URL-based scroll key today
|
|
725
|
+
// (useScrollRestoration({ getKey: ({ pathname }) => pathname }) or
|
|
726
|
+
// similar). Tracked as follow-up to switch scroll-restoration to
|
|
727
|
+
// navigation.currentEntry.id (or rango-store-backed keys) so it
|
|
728
|
+
// survives the intercept commit.
|
|
729
|
+
event.intercept({
|
|
730
|
+
handler: async () => {
|
|
731
|
+
await this.handlePopstate();
|
|
732
|
+
},
|
|
733
|
+
});
|
|
734
|
+
};
|
|
735
|
+
|
|
607
736
|
// When the browser restores a page from bfcache (back-forward cache),
|
|
608
737
|
// any in-flight navigation state is stale. This happens when:
|
|
609
738
|
// 1. A navigation triggers X-RSC-Reload (e.g., response route hit via SPA)
|
|
@@ -629,13 +758,22 @@ export function createNavigationBridge(
|
|
|
629
758
|
this.refresh();
|
|
630
759
|
});
|
|
631
760
|
|
|
632
|
-
|
|
761
|
+
if (supportsNavigationApi) {
|
|
762
|
+
navigationApi.addEventListener("navigate", handleNavigateEvent);
|
|
763
|
+
debugLog("[Browser] Navigation bridge ready (Navigation API)");
|
|
764
|
+
} else {
|
|
765
|
+
window.addEventListener("popstate", handlePopstate);
|
|
766
|
+
debugLog("[Browser] Navigation bridge ready (popstate fallback)");
|
|
767
|
+
}
|
|
633
768
|
window.addEventListener("pageshow", handlePageShow);
|
|
634
|
-
debugLog("[Browser] Navigation bridge ready");
|
|
635
769
|
|
|
636
770
|
return () => {
|
|
637
771
|
cleanupLinks();
|
|
638
|
-
|
|
772
|
+
if (supportsNavigationApi) {
|
|
773
|
+
navigationApi.removeEventListener("navigate", handleNavigateEvent);
|
|
774
|
+
} else {
|
|
775
|
+
window.removeEventListener("popstate", handlePopstate);
|
|
776
|
+
}
|
|
639
777
|
window.removeEventListener("pageshow", handlePageShow);
|
|
640
778
|
};
|
|
641
779
|
},
|
|
@@ -645,6 +783,10 @@ export function createNavigationBridge(
|
|
|
645
783
|
setAppVersion(newVersion);
|
|
646
784
|
store.clearHistoryCache();
|
|
647
785
|
},
|
|
786
|
+
|
|
787
|
+
updateAppShell(next: AppShell): void {
|
|
788
|
+
applyAppShell(next);
|
|
789
|
+
},
|
|
648
790
|
};
|
|
649
791
|
}
|
|
650
792
|
|
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
} from "./response-adapter.js";
|
|
20
20
|
import {
|
|
21
21
|
buildPrefetchKey,
|
|
22
|
+
buildSourceKey,
|
|
22
23
|
consumeInflightPrefetch,
|
|
23
24
|
consumePrefetch,
|
|
24
25
|
} from "./prefetch/cache.js";
|
|
@@ -30,8 +31,10 @@ import {
|
|
|
30
31
|
* deserializing the response using the RSC runtime.
|
|
31
32
|
*
|
|
32
33
|
* Checks the in-memory prefetch cache before making a network request.
|
|
33
|
-
*
|
|
34
|
-
*
|
|
34
|
+
* Tries the source-scoped key first (populated when the server tagged
|
|
35
|
+
* the response as source-sensitive via `X-RSC-Prefetch-Scope: source`)
|
|
36
|
+
* and falls back to the Rango-state-keyed wildcard slot used for the
|
|
37
|
+
* common source-agnostic case.
|
|
35
38
|
*
|
|
36
39
|
* @param deps - RSC browser dependencies (createFromFetch)
|
|
37
40
|
* @returns NavigationClient instance
|
|
@@ -93,18 +96,42 @@ export function createNavigationClient(
|
|
|
93
96
|
fetchUrl.searchParams.set("_rsc_rid", routerId);
|
|
94
97
|
}
|
|
95
98
|
|
|
96
|
-
// Check completed in-memory prefetch cache before making a network
|
|
97
|
-
//
|
|
98
|
-
//
|
|
99
|
+
// Check completed in-memory prefetch cache before making a network
|
|
100
|
+
// request. Try the source-scoped key first (populated when the server
|
|
101
|
+
// tagged the prefetch response as source-sensitive, e.g. intercepts,
|
|
102
|
+
// or when a Link opted in with `prefetchKey=":source"`), then fall
|
|
103
|
+
// back to the wildcard slot shared across source pages.
|
|
104
|
+
// Both keys embed the Rango state, so state rotation (deploy or
|
|
105
|
+
// server-action invalidation) auto-invalidates both scopes.
|
|
99
106
|
// Skip cache for stale revalidation (needs fresh data), HMR (needs
|
|
100
107
|
// fresh modules), and intercept contexts (source-dependent responses).
|
|
101
|
-
//
|
|
102
108
|
const canUsePrefetch = !staleRevalidation && !hmr && !interceptSourceUrl;
|
|
103
|
-
const
|
|
104
|
-
const
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
|
|
109
|
+
const rangoState = getRangoState();
|
|
110
|
+
const wildcardKey = buildPrefetchKey(rangoState, fetchUrl);
|
|
111
|
+
const cacheKey = buildSourceKey(rangoState, previousUrl, fetchUrl);
|
|
112
|
+
|
|
113
|
+
let cachedResponse: Response | null = null;
|
|
114
|
+
let hitKey: string | null = null;
|
|
115
|
+
if (canUsePrefetch) {
|
|
116
|
+
cachedResponse = consumePrefetch(cacheKey);
|
|
117
|
+
if (cachedResponse) {
|
|
118
|
+
hitKey = cacheKey;
|
|
119
|
+
} else {
|
|
120
|
+
cachedResponse = consumePrefetch(wildcardKey);
|
|
121
|
+
if (cachedResponse) hitKey = wildcardKey;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
let inflightResponsePromise: Promise<Response | null> | null = null;
|
|
126
|
+
if (canUsePrefetch && !cachedResponse) {
|
|
127
|
+
inflightResponsePromise = consumeInflightPrefetch(cacheKey);
|
|
128
|
+
if (inflightResponsePromise) {
|
|
129
|
+
hitKey = cacheKey;
|
|
130
|
+
} else {
|
|
131
|
+
inflightResponsePromise = consumeInflightPrefetch(wildcardKey);
|
|
132
|
+
if (inflightResponsePromise) hitKey = wildcardKey;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
108
135
|
// Track when the stream completes
|
|
109
136
|
let resolveStreamComplete: () => void;
|
|
110
137
|
const streamComplete = new Promise<void>((resolve) => {
|
|
@@ -197,7 +224,10 @@ export function createNavigationClient(
|
|
|
197
224
|
|
|
198
225
|
if (cachedResponse) {
|
|
199
226
|
if (tx) {
|
|
200
|
-
browserDebugLog(tx, "prefetch cache hit", {
|
|
227
|
+
browserDebugLog(tx, "prefetch cache hit", {
|
|
228
|
+
key: hitKey,
|
|
229
|
+
wildcard: hitKey === wildcardKey,
|
|
230
|
+
});
|
|
201
231
|
}
|
|
202
232
|
responsePromise = Promise.resolve(cachedResponse).then((response) => {
|
|
203
233
|
const validated = validateRscHeaders(response, "prefetch cache");
|
|
@@ -214,8 +244,12 @@ export function createNavigationClient(
|
|
|
214
244
|
});
|
|
215
245
|
} else if (inflightResponsePromise) {
|
|
216
246
|
if (tx) {
|
|
217
|
-
browserDebugLog(tx, "reusing inflight prefetch", {
|
|
247
|
+
browserDebugLog(tx, "reusing inflight prefetch", {
|
|
248
|
+
key: hitKey,
|
|
249
|
+
wildcard: hitKey === wildcardKey,
|
|
250
|
+
});
|
|
218
251
|
}
|
|
252
|
+
const adoptedViaWildcard = hitKey === wildcardKey;
|
|
219
253
|
responsePromise = inflightResponsePromise.then(async (response) => {
|
|
220
254
|
if (!response) {
|
|
221
255
|
if (tx) {
|
|
@@ -224,6 +258,23 @@ export function createNavigationClient(
|
|
|
224
258
|
return doFreshFetch();
|
|
225
259
|
}
|
|
226
260
|
|
|
261
|
+
// Cross-source safety: an inflight promise adopted via the
|
|
262
|
+
// wildcard key may turn out to be source-scoped (server emitted
|
|
263
|
+
// `X-RSC-Prefetch-Scope: source`), which means it was built for
|
|
264
|
+
// a different source page. Discard and refetch.
|
|
265
|
+
if (
|
|
266
|
+
adoptedViaWildcard &&
|
|
267
|
+
response.headers.get("x-rsc-prefetch-scope") === "source"
|
|
268
|
+
) {
|
|
269
|
+
if (tx) {
|
|
270
|
+
browserDebugLog(
|
|
271
|
+
tx,
|
|
272
|
+
"wildcard inflight turned out source-scoped, refetching",
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
return doFreshFetch();
|
|
276
|
+
}
|
|
277
|
+
|
|
227
278
|
const validated = validateRscHeaders(response, "inflight prefetch");
|
|
228
279
|
if (validated instanceof Promise) return validated;
|
|
229
280
|
|
|
@@ -12,7 +12,10 @@ import type {
|
|
|
12
12
|
ActionStateListener,
|
|
13
13
|
HandleData,
|
|
14
14
|
} from "./types.js";
|
|
15
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
clearPrefetchCache,
|
|
17
|
+
clearPrefetchCacheLocal,
|
|
18
|
+
} from "./prefetch/cache.js";
|
|
16
19
|
|
|
17
20
|
/**
|
|
18
21
|
* Default action state (idle with no payload)
|
|
@@ -335,6 +338,18 @@ export function createNavigationStore(
|
|
|
335
338
|
clearPrefetchCache();
|
|
336
339
|
}
|
|
337
340
|
|
|
341
|
+
/**
|
|
342
|
+
* Drop this tab's navigation + prefetch caches without broadcasting or
|
|
343
|
+
* rotating shared state. Used when the local session changes in a way that
|
|
344
|
+
* doesn't affect other tabs — e.g. this tab crosses into a different app
|
|
345
|
+
* via a cross-router navigation. Other tabs in the old app keep their
|
|
346
|
+
* caches and their X-Rango-State token.
|
|
347
|
+
*/
|
|
348
|
+
function clearCacheInternalLocal(): void {
|
|
349
|
+
historyCache.length = 0;
|
|
350
|
+
clearPrefetchCacheLocal();
|
|
351
|
+
}
|
|
352
|
+
|
|
338
353
|
/**
|
|
339
354
|
* Mark all cache entries as stale (internal - does not broadcast)
|
|
340
355
|
*/
|
|
@@ -668,6 +683,15 @@ export function createNavigationStore(
|
|
|
668
683
|
clearCacheAndBroadcast();
|
|
669
684
|
},
|
|
670
685
|
|
|
686
|
+
/**
|
|
687
|
+
* Drop this tab's navigation + prefetch caches locally without
|
|
688
|
+
* broadcasting or rotating shared state. Intended for cross-app
|
|
689
|
+
* transitions where the session state diverges for this tab only.
|
|
690
|
+
*/
|
|
691
|
+
clearHistoryCacheLocal(): void {
|
|
692
|
+
clearCacheInternalLocal();
|
|
693
|
+
},
|
|
694
|
+
|
|
671
695
|
/**
|
|
672
696
|
* Mark cache as stale and broadcast to other tabs
|
|
673
697
|
* Called after server actions - allows SWR pattern for popstate
|
|
@@ -14,7 +14,10 @@ const addTransitionType: ((type: string) => void) | undefined =
|
|
|
14
14
|
import type { RenderSegmentsOptions } from "../segment-system.js";
|
|
15
15
|
import { reconcileSegments } from "./segment-reconciler.js";
|
|
16
16
|
import type { ReconcileActor } from "./segment-reconciler.js";
|
|
17
|
-
import {
|
|
17
|
+
import {
|
|
18
|
+
hasActiveIntercept as hasActiveInterceptSlots,
|
|
19
|
+
isInterceptSegment,
|
|
20
|
+
} from "./intercept-utils.js";
|
|
18
21
|
import type { BoundTransaction } from "./navigation-transaction.js";
|
|
19
22
|
import { ServerRedirect } from "../errors.js";
|
|
20
23
|
import { debugLog } from "./logging.js";
|
|
@@ -28,6 +31,23 @@ function toScrollPayload(
|
|
|
28
31
|
return { enabled: scroll !== false ? scroll : false };
|
|
29
32
|
}
|
|
30
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Whether to wrap an update in startViewTransition.
|
|
36
|
+
*
|
|
37
|
+
* Intercept-driven updates only mutate the parallel slot — the main outlet
|
|
38
|
+
* shows the same content — so transitions on the underlying main segments
|
|
39
|
+
* shouldn't fire (otherwise their elements get hoisted above the modal).
|
|
40
|
+
*/
|
|
41
|
+
function shouldStartViewTransition(segments: ResolvedSegment[]): boolean {
|
|
42
|
+
let hasIntercept = false;
|
|
43
|
+
let hasTransition = false;
|
|
44
|
+
for (const s of segments) {
|
|
45
|
+
if (isInterceptSegment(s)) hasIntercept = true;
|
|
46
|
+
else if (s.transition) hasTransition = true;
|
|
47
|
+
}
|
|
48
|
+
return !hasIntercept && hasTransition;
|
|
49
|
+
}
|
|
50
|
+
|
|
31
51
|
/**
|
|
32
52
|
* Configuration for creating a partial updater
|
|
33
53
|
*/
|
|
@@ -41,6 +61,13 @@ export interface PartialUpdateConfig {
|
|
|
41
61
|
) => Promise<ReactNode> | ReactNode;
|
|
42
62
|
/** RSC version getter — returns the current version (may change after HMR) */
|
|
43
63
|
getVersion?: () => string | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* Replace the active app-shell when a cross-app navigation is detected.
|
|
66
|
+
* Called before the full-update tree replacement renders, so the new
|
|
67
|
+
* payload's rootLayout, basename, and version are picked up. Theme,
|
|
68
|
+
* warmup, and prefetch TTL are not part of the shell — see AppShell.
|
|
69
|
+
*/
|
|
70
|
+
applyAppShell?: (next: import("./app-shell.js").AppShell) => void;
|
|
44
71
|
}
|
|
45
72
|
|
|
46
73
|
/**
|
|
@@ -110,6 +137,7 @@ export function createPartialUpdater(
|
|
|
110
137
|
onUpdate,
|
|
111
138
|
renderSegments,
|
|
112
139
|
getVersion = () => undefined,
|
|
140
|
+
applyAppShell,
|
|
113
141
|
} = config;
|
|
114
142
|
|
|
115
143
|
/**
|
|
@@ -167,9 +195,16 @@ export function createPartialUpdater(
|
|
|
167
195
|
segments = segmentIds ?? segmentState.currentSegmentIds;
|
|
168
196
|
}
|
|
169
197
|
|
|
170
|
-
// For intercept revalidation, use the intercept source URL as previousUrl
|
|
198
|
+
// For intercept revalidation, use the intercept source URL as previousUrl.
|
|
199
|
+
// For leave-intercept, tx.currentUrl captures window.location.href at tx
|
|
200
|
+
// creation, which on popstate is already the destination URL and would
|
|
201
|
+
// tell the server "from == to". segmentState.currentUrl still points at
|
|
202
|
+
// the URL the cached segments render (the intercept URL), which is the
|
|
203
|
+
// correct "from" for the server's diff computation.
|
|
171
204
|
const previousUrl =
|
|
172
|
-
|
|
205
|
+
mode.type === "leave-intercept"
|
|
206
|
+
? segmentState.currentUrl || tx.currentUrl
|
|
207
|
+
: interceptSourceUrl || tx.currentUrl || segmentState.currentUrl;
|
|
173
208
|
|
|
174
209
|
debugLog(`\n[Browser] >>> NAVIGATION`);
|
|
175
210
|
debugLog(`[Browser] From: ${previousUrl}`);
|
|
@@ -188,6 +223,11 @@ export function createPartialUpdater(
|
|
|
188
223
|
targetCache && targetCache.length > 0
|
|
189
224
|
? targetCache
|
|
190
225
|
: getCurrentCachedSegments();
|
|
226
|
+
const cachedSegsSource =
|
|
227
|
+
targetCache && targetCache.length > 0 ? "history-cache" : "current-page";
|
|
228
|
+
debugLog(
|
|
229
|
+
`[Browser] cachedSegs source: ${cachedSegsSource} (${cachedSegs.length} segments: ${cachedSegs.map((s) => s.id).join(", ")})`,
|
|
230
|
+
);
|
|
191
231
|
|
|
192
232
|
// Fetch partial payload (no abort signal - RSC doesn't support it well)
|
|
193
233
|
let fetchResult: Awaited<ReturnType<NavigationClient["fetchPartial"]>>;
|
|
@@ -216,7 +256,12 @@ export function createPartialUpdater(
|
|
|
216
256
|
// Detect app switch: if routerId changed, the navigation crossed into
|
|
217
257
|
// a different router (e.g., via host router path mount). Downgrade
|
|
218
258
|
// partial to full so the entire tree is replaced without reconciliation
|
|
219
|
-
// against stale segments from the previous app
|
|
259
|
+
// against stale segments from the previous app, and replace the app
|
|
260
|
+
// shell (rootLayout, basename, version) so the target app's document
|
|
261
|
+
// and router config take effect instead of remaining captured from the
|
|
262
|
+
// initial load. Theme, warmup, and prefetch TTL are intentionally
|
|
263
|
+
// document-lifetime (see AppShell doc); a new document navigation
|
|
264
|
+
// applies them.
|
|
220
265
|
if (payload.metadata?.routerId) {
|
|
221
266
|
const prevRouterId = store.getRouterId?.();
|
|
222
267
|
if (prevRouterId && prevRouterId !== payload.metadata.routerId) {
|
|
@@ -224,6 +269,12 @@ export function createPartialUpdater(
|
|
|
224
269
|
`[Browser] App switch detected (${prevRouterId} → ${payload.metadata.routerId}), forcing full update`,
|
|
225
270
|
);
|
|
226
271
|
payload.metadata.isPartial = false;
|
|
272
|
+
applyAppShell?.({
|
|
273
|
+
routerId: payload.metadata.routerId,
|
|
274
|
+
rootLayout: payload.metadata.rootLayout,
|
|
275
|
+
basename: payload.metadata.basename,
|
|
276
|
+
version: payload.metadata.version,
|
|
277
|
+
});
|
|
227
278
|
}
|
|
228
279
|
store.setRouterId?.(payload.metadata.routerId);
|
|
229
280
|
}
|
|
@@ -307,10 +358,7 @@ export function createPartialUpdater(
|
|
|
307
358
|
scroll: toScrollPayload(commitScroll),
|
|
308
359
|
};
|
|
309
360
|
|
|
310
|
-
|
|
311
|
-
(s) => s.transition,
|
|
312
|
-
);
|
|
313
|
-
if (cachedHasTransition) {
|
|
361
|
+
if (shouldStartViewTransition(existingSegments)) {
|
|
314
362
|
startTransition(() => {
|
|
315
363
|
if (addTransitionType) {
|
|
316
364
|
addTransitionType("navigation");
|
|
@@ -496,7 +544,7 @@ export function createPartialUpdater(
|
|
|
496
544
|
|
|
497
545
|
// Emit update to trigger React render.
|
|
498
546
|
// Scroll info is included so NavigationProvider applies it after React commits.
|
|
499
|
-
const hasTransition = reconciled.
|
|
547
|
+
const hasTransition = shouldStartViewTransition(reconciled.segments);
|
|
500
548
|
const scrollPayload = toScrollPayload(navScroll);
|
|
501
549
|
|
|
502
550
|
if (mode.type === "action" || mode.type === "stale-revalidation") {
|
|
@@ -558,9 +606,7 @@ export function createPartialUpdater(
|
|
|
558
606
|
})
|
|
559
607
|
: tx.commit(segmentIds, segments);
|
|
560
608
|
|
|
561
|
-
const fullHasTransition = segments
|
|
562
|
-
(s: ResolvedSegment) => s.transition,
|
|
563
|
-
);
|
|
609
|
+
const fullHasTransition = shouldStartViewTransition(segments);
|
|
564
610
|
const fullScrollPayload = toScrollPayload(fullScroll);
|
|
565
611
|
|
|
566
612
|
if (mode.type === "stale-revalidation") {
|