@rangojs/router 0.0.0-experimental.20 → 0.0.0-experimental.20dbba0c

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.
Files changed (189) hide show
  1. package/AGENTS.md +4 -0
  2. package/README.md +172 -50
  3. package/dist/bin/rango.js +138 -50
  4. package/dist/vite/index.js +1160 -508
  5. package/dist/vite/index.js.bak +5448 -0
  6. package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  7. package/package.json +17 -16
  8. package/skills/breadcrumbs/SKILL.md +252 -0
  9. package/skills/cache-guide/SKILL.md +32 -0
  10. package/skills/caching/SKILL.md +49 -8
  11. package/skills/document-cache/SKILL.md +2 -2
  12. package/skills/handler-use/SKILL.md +362 -0
  13. package/skills/hooks/SKILL.md +61 -51
  14. package/skills/host-router/SKILL.md +218 -0
  15. package/skills/intercept/SKILL.md +20 -0
  16. package/skills/layout/SKILL.md +22 -0
  17. package/skills/links/SKILL.md +91 -17
  18. package/skills/loader/SKILL.md +107 -24
  19. package/skills/middleware/SKILL.md +34 -3
  20. package/skills/migrate-nextjs/SKILL.md +560 -0
  21. package/skills/migrate-react-router/SKILL.md +765 -0
  22. package/skills/parallel/SKILL.md +185 -0
  23. package/skills/prerender/SKILL.md +112 -70
  24. package/skills/rango/SKILL.md +24 -23
  25. package/skills/response-routes/SKILL.md +8 -0
  26. package/skills/route/SKILL.md +58 -4
  27. package/skills/router-setup/SKILL.md +95 -5
  28. package/skills/streams-and-websockets/SKILL.md +283 -0
  29. package/skills/typesafety/SKILL.md +38 -24
  30. package/src/__internal.ts +92 -0
  31. package/src/browser/app-shell.ts +52 -0
  32. package/src/browser/app-version.ts +14 -0
  33. package/src/browser/event-controller.ts +5 -0
  34. package/src/browser/link-interceptor.ts +4 -0
  35. package/src/browser/navigation-bridge.ts +175 -17
  36. package/src/browser/navigation-client.ts +177 -44
  37. package/src/browser/navigation-store.ts +68 -9
  38. package/src/browser/navigation-transaction.ts +11 -9
  39. package/src/browser/partial-update.ts +113 -17
  40. package/src/browser/prefetch/cache.ts +275 -28
  41. package/src/browser/prefetch/fetch.ts +191 -46
  42. package/src/browser/prefetch/policy.ts +6 -0
  43. package/src/browser/prefetch/queue.ts +123 -20
  44. package/src/browser/prefetch/resource-ready.ts +77 -0
  45. package/src/browser/rango-state.ts +53 -13
  46. package/src/browser/react/Link.tsx +98 -14
  47. package/src/browser/react/NavigationProvider.tsx +89 -14
  48. package/src/browser/react/context.ts +7 -2
  49. package/src/browser/react/use-handle.ts +9 -58
  50. package/src/browser/react/use-navigation.ts +22 -2
  51. package/src/browser/react/use-params.ts +11 -1
  52. package/src/browser/react/use-router.ts +29 -9
  53. package/src/browser/rsc-router.tsx +177 -66
  54. package/src/browser/scroll-restoration.ts +41 -42
  55. package/src/browser/segment-reconciler.ts +36 -9
  56. package/src/browser/server-action-bridge.ts +8 -6
  57. package/src/browser/types.ts +73 -5
  58. package/src/build/generate-manifest.ts +6 -6
  59. package/src/build/generate-route-types.ts +3 -0
  60. package/src/build/route-trie.ts +67 -25
  61. package/src/build/route-types/include-resolution.ts +8 -1
  62. package/src/build/route-types/router-processing.ts +223 -74
  63. package/src/build/route-types/scan-filter.ts +8 -1
  64. package/src/cache/cache-runtime.ts +15 -11
  65. package/src/cache/cache-scope.ts +48 -7
  66. package/src/cache/cf/cf-cache-store.ts +455 -15
  67. package/src/cache/cf/index.ts +5 -1
  68. package/src/cache/document-cache.ts +17 -7
  69. package/src/cache/index.ts +1 -0
  70. package/src/cache/taint.ts +55 -0
  71. package/src/client.rsc.tsx +2 -1
  72. package/src/client.tsx +85 -276
  73. package/src/context-var.ts +72 -2
  74. package/src/debug.ts +2 -2
  75. package/src/handle.ts +40 -0
  76. package/src/handles/breadcrumbs.ts +66 -0
  77. package/src/handles/index.ts +1 -0
  78. package/src/host/index.ts +0 -3
  79. package/src/index.rsc.ts +9 -36
  80. package/src/index.ts +79 -70
  81. package/src/outlet-context.ts +1 -1
  82. package/src/prerender/store.ts +57 -15
  83. package/src/prerender.ts +138 -77
  84. package/src/response-utils.ts +28 -0
  85. package/src/reverse.ts +27 -2
  86. package/src/route-definition/dsl-helpers.ts +240 -40
  87. package/src/route-definition/helpers-types.ts +67 -19
  88. package/src/route-definition/index.ts +3 -3
  89. package/src/route-definition/redirect.ts +11 -3
  90. package/src/route-definition/resolve-handler-use.ts +155 -0
  91. package/src/route-map-builder.ts +7 -1
  92. package/src/route-types.ts +18 -0
  93. package/src/router/content-negotiation.ts +100 -1
  94. package/src/router/find-match.ts +4 -2
  95. package/src/router/handler-context.ts +129 -26
  96. package/src/router/intercept-resolution.ts +11 -4
  97. package/src/router/lazy-includes.ts +10 -7
  98. package/src/router/loader-resolution.ts +160 -22
  99. package/src/router/logging.ts +5 -2
  100. package/src/router/manifest.ts +31 -16
  101. package/src/router/match-api.ts +128 -193
  102. package/src/router/match-middleware/background-revalidation.ts +30 -2
  103. package/src/router/match-middleware/cache-lookup.ts +94 -17
  104. package/src/router/match-middleware/cache-store.ts +53 -10
  105. package/src/router/match-middleware/intercept-resolution.ts +9 -7
  106. package/src/router/match-middleware/segment-resolution.ts +61 -5
  107. package/src/router/match-result.ts +103 -18
  108. package/src/router/metrics.ts +238 -13
  109. package/src/router/middleware-types.ts +48 -27
  110. package/src/router/middleware.ts +201 -86
  111. package/src/router/navigation-snapshot.ts +182 -0
  112. package/src/router/pattern-matching.ts +77 -11
  113. package/src/router/prerender-match.ts +114 -10
  114. package/src/router/preview-match.ts +30 -102
  115. package/src/router/request-classification.ts +310 -0
  116. package/src/router/revalidation.ts +27 -7
  117. package/src/router/route-snapshot.ts +245 -0
  118. package/src/router/router-context.ts +6 -1
  119. package/src/router/router-interfaces.ts +50 -5
  120. package/src/router/router-options.ts +50 -19
  121. package/src/router/segment-resolution/fresh.ts +215 -19
  122. package/src/router/segment-resolution/helpers.ts +30 -25
  123. package/src/router/segment-resolution/loader-cache.ts +1 -0
  124. package/src/router/segment-resolution/revalidation.ts +454 -301
  125. package/src/router/segment-wrappers.ts +2 -0
  126. package/src/router/trie-matching.ts +30 -6
  127. package/src/router/types.ts +1 -0
  128. package/src/router/url-params.ts +49 -0
  129. package/src/router.ts +89 -17
  130. package/src/rsc/handler.ts +563 -364
  131. package/src/rsc/helpers.ts +69 -41
  132. package/src/rsc/index.ts +0 -20
  133. package/src/rsc/loader-fetch.ts +23 -3
  134. package/src/rsc/manifest-init.ts +5 -1
  135. package/src/rsc/progressive-enhancement.ts +37 -10
  136. package/src/rsc/response-route-handler.ts +14 -1
  137. package/src/rsc/rsc-rendering.ts +47 -44
  138. package/src/rsc/server-action.ts +24 -10
  139. package/src/rsc/ssr-setup.ts +128 -0
  140. package/src/rsc/types.ts +11 -1
  141. package/src/search-params.ts +16 -13
  142. package/src/segment-content-promise.ts +67 -0
  143. package/src/segment-loader-promise.ts +122 -0
  144. package/src/segment-system.tsx +109 -23
  145. package/src/server/context.ts +174 -19
  146. package/src/server/handle-store.ts +19 -0
  147. package/src/server/loader-registry.ts +9 -8
  148. package/src/server/request-context.ts +218 -65
  149. package/src/server.ts +6 -0
  150. package/src/ssr/index.tsx +4 -0
  151. package/src/static-handler.ts +18 -6
  152. package/src/theme/index.ts +4 -13
  153. package/src/types/cache-types.ts +4 -4
  154. package/src/types/handler-context.ts +140 -72
  155. package/src/types/loader-types.ts +41 -15
  156. package/src/types/request-scope.ts +126 -0
  157. package/src/types/route-config.ts +17 -8
  158. package/src/types/route-entry.ts +19 -1
  159. package/src/types/segments.ts +2 -5
  160. package/src/urls/include-helper.ts +24 -14
  161. package/src/urls/path-helper-types.ts +39 -6
  162. package/src/urls/path-helper.ts +48 -13
  163. package/src/urls/pattern-types.ts +12 -0
  164. package/src/urls/response-types.ts +18 -16
  165. package/src/use-loader.tsx +77 -5
  166. package/src/vite/discovery/bundle-postprocess.ts +61 -89
  167. package/src/vite/discovery/discover-routers.ts +7 -4
  168. package/src/vite/discovery/prerender-collection.ts +162 -88
  169. package/src/vite/discovery/state.ts +17 -13
  170. package/src/vite/index.ts +8 -3
  171. package/src/vite/plugin-types.ts +51 -79
  172. package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
  173. package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  174. package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
  175. package/src/vite/plugins/expose-action-id.ts +1 -3
  176. package/src/vite/plugins/expose-id-utils.ts +12 -0
  177. package/src/vite/plugins/expose-ids/handler-transform.ts +30 -0
  178. package/src/vite/plugins/expose-internal-ids.ts +257 -40
  179. package/src/vite/plugins/performance-tracks.ts +88 -0
  180. package/src/vite/plugins/refresh-cmd.ts +127 -0
  181. package/src/vite/plugins/version-plugin.ts +13 -1
  182. package/src/vite/rango.ts +190 -217
  183. package/src/vite/router-discovery.ts +241 -45
  184. package/src/vite/utils/banner.ts +4 -4
  185. package/src/vite/utils/package-resolution.ts +34 -1
  186. package/src/vite/utils/prerender-utils.ts +97 -5
  187. package/src/vite/utils/shared-utils.ts +3 -2
  188. package/skills/testing/SKILL.md +0 -226
  189. package/src/route-definition/route-function.ts +0 -119
package/src/__internal.ts CHANGED
@@ -164,6 +164,98 @@ export type {
164
164
  */
165
165
  export type { InternalHandlerContext } from "./types.js";
166
166
 
167
+ // ============================================================================
168
+ // Rendering (Internal)
169
+ // ============================================================================
170
+
171
+ /**
172
+ * @internal
173
+ * Builds React element trees from route segments.
174
+ */
175
+ export { renderSegments } from "./segment-system.js";
176
+
177
+ // ============================================================================
178
+ // Error Utilities (Internal)
179
+ // ============================================================================
180
+
181
+ /**
182
+ * @internal
183
+ * Error sanitization and network error utilities.
184
+ */
185
+ export { sanitizeError, NetworkError, isNetworkError } from "./errors.js";
186
+
187
+ // ============================================================================
188
+ // Type Utilities (Internal)
189
+ // ============================================================================
190
+
191
+ /**
192
+ * @internal
193
+ * Scoped view of GeneratedRouteMap for Handler<"localName", ScopedRouteMap<"prefix">>.
194
+ */
195
+ export type { ScopedRouteMap } from "./types.js";
196
+
197
+ /**
198
+ * @internal
199
+ * Type-level utilities for reverse URL generation.
200
+ */
201
+ export type { MergeRoutes, SanitizePrefix } from "./reverse.js";
202
+
203
+ /**
204
+ * @internal
205
+ * Individual telemetry event types.
206
+ */
207
+ export type {
208
+ RequestStartEvent,
209
+ RequestEndEvent,
210
+ RequestErrorEvent,
211
+ RequestTimeoutEvent,
212
+ LoaderStartEvent,
213
+ LoaderEndEvent,
214
+ LoaderErrorEvent,
215
+ HandlerErrorEvent,
216
+ CacheDecisionEvent,
217
+ RevalidationDecisionEvent,
218
+ } from "./router/telemetry.js";
219
+
220
+ // ============================================================================
221
+ // Pre-render / Static Handler Guards (Internal)
222
+ // ============================================================================
223
+
224
+ /**
225
+ * @internal
226
+ * Type guard for prerender handler definitions.
227
+ */
228
+ export { isPrerenderHandler, isPassthroughHandler } from "./prerender.js";
229
+
230
+ /**
231
+ * @internal
232
+ * Type guard for static handler definitions.
233
+ */
234
+ export { isStaticHandler } from "./static-handler.js";
235
+
236
+ // ============================================================================
237
+ // URL Pattern Internals
238
+ // ============================================================================
239
+
240
+ /**
241
+ * @internal
242
+ * Sentinel used to tag response-type route entries.
243
+ */
244
+ export { RESPONSE_TYPE } from "./urls.js";
245
+
246
+ // ============================================================================
247
+ // Route Match Debug (Internal)
248
+ // ============================================================================
249
+
250
+ /**
251
+ * @internal
252
+ * Debug utilities for route matching performance analysis.
253
+ */
254
+ export {
255
+ enableMatchDebug,
256
+ getMatchDebugStats,
257
+ } from "./router/pattern-matching.js";
258
+
167
259
  // ============================================================================
168
260
  // Debug Utilities (Internal)
169
261
  // ============================================================================
@@ -0,0 +1,52 @@
1
+ import type { ComponentType, ReactNode } from "react";
2
+
3
+ /**
4
+ * App-shell metadata: the set of per-router fields that describe the
5
+ * "envelope" around the current app's segment tree. These fields are set
6
+ * from the initial RSC payload and must be replaced atomically when the
7
+ * client navigates into a different router (app switch).
8
+ *
9
+ * Intentionally NOT part of the shell (all document-lifetime):
10
+ * - themeConfig / initialTheme: ThemeProvider is mounted above the segment
11
+ * tree and must not remount on smooth transitions.
12
+ * - warmupEnabled: attached to the NavigationProvider's lifetime effect;
13
+ * toggling it mid-session would tear down and restart idle listeners.
14
+ * Also not serialized on every full-render path (e.g. the not-found
15
+ * fallback), so carrying it here would be unreliable.
16
+ * - prefetchCacheTTL: the not-found full-render payload does not serialize
17
+ * it, so a cross-app nav into a 404 would silently erase the setting.
18
+ * Mutable shell fields must be serialized on EVERY full-render path,
19
+ * otherwise absent fields are indistinguishable from "new app has no
20
+ * value" and the old app's value is dropped.
21
+ *
22
+ * A new document navigation (hard reload) applies these fields from the
23
+ * target app's initial payload.
24
+ */
25
+ export interface AppShell {
26
+ /** Router identity. Used to namespace per-app client state (e.g. the
27
+ * rango-state localStorage key) so sibling apps on the same origin
28
+ * cannot observe each other's cache invalidations. */
29
+ routerId?: string;
30
+ rootLayout?: ComponentType<{ children: ReactNode }>;
31
+ basename?: string;
32
+ version?: string;
33
+ }
34
+
35
+ /**
36
+ * Mutable container for the active app shell. Read-through via `get()` so
37
+ * closures capture the ref, not the shell, and pick up updates at call time.
38
+ */
39
+ export interface AppShellRef {
40
+ get(): AppShell;
41
+ update(next: AppShell): void;
42
+ }
43
+
44
+ export function createAppShellRef(initial: AppShell): AppShellRef {
45
+ let current = initial;
46
+ return {
47
+ get: () => current,
48
+ update: (next) => {
49
+ current = next;
50
+ },
51
+ };
52
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Mutable app version — updated after HMR revalidation.
3
+ * Read by prefetch, navigation, and context code.
4
+ */
5
+
6
+ let currentVersion: string | undefined;
7
+
8
+ export function getAppVersion(): string | undefined {
9
+ return currentVersion;
10
+ }
11
+
12
+ export function setAppVersion(version: string | undefined): void {
13
+ currentVersion = version;
14
+ }
@@ -79,6 +79,8 @@ export interface DerivedNavigationState {
79
79
  state: "idle" | "loading";
80
80
  /** Whether any operation is streaming */
81
81
  isStreaming: boolean;
82
+ /** Whether a navigation is active (fetching or streaming, before commit) */
83
+ isNavigating: boolean;
82
84
  /** Current committed location */
83
85
  location: NavigationLocation;
84
86
  /** URL being navigated to (null if idle) */
@@ -389,6 +391,9 @@ export function createEventController(
389
391
  return {
390
392
  state,
391
393
  isStreaming,
394
+ // True when a navigation is active (fetching or streaming, before
395
+ // commit). Broader than pendingUrl which clears during streaming.
396
+ isNavigating: currentNavigation !== null,
392
397
  location,
393
398
  // pendingUrl only during fetching phase - once streaming starts (URL changed), not pending.
394
399
  // Background revalidations (skipLoadingState) don't expose a pending URL.
@@ -117,6 +117,7 @@ export function setupLinkInterception(
117
117
  // Read navigation options from data attributes (set by Link component)
118
118
  const scrollAttr = link.getAttribute("data-scroll");
119
119
  const replaceAttr = link.getAttribute("data-replace");
120
+ const revalidateAttr = link.getAttribute("data-revalidate");
120
121
 
121
122
  const navigateOptions: NavigateOptions = {};
122
123
  if (scrollAttr === "false") {
@@ -125,6 +126,9 @@ export function setupLinkInterception(
125
126
  if (replaceAttr === "true") {
126
127
  navigateOptions.replace = true;
127
128
  }
129
+ if (revalidateAttr === "false") {
130
+ navigateOptions.revalidate = false;
131
+ }
128
132
 
129
133
  onNavigate(href, navigateOptions);
130
134
  };
@@ -4,12 +4,21 @@ import type {
4
4
  NavigateOptionsInternal,
5
5
  ResolvedSegment,
6
6
  } from "./types.js";
7
+ import { setAppVersion } from "./app-version.js";
8
+ import { setRangoStateLocal } from "./rango-state.js";
9
+ import type { AppShell, AppShellRef } from "./app-shell.js";
7
10
  import * as React from "react";
8
11
  import { startTransition } from "react";
9
12
  import {
10
13
  createNavigationTransaction,
11
14
  resolveNavigationState,
12
15
  } from "./navigation-transaction.js";
16
+ import { buildHistoryState } from "./history-state.js";
17
+ import {
18
+ handleNavigationStart,
19
+ handleNavigationEnd,
20
+ ensureHistoryKey,
21
+ } from "./scroll-restoration.js";
13
22
 
14
23
  // addTransitionType is only available in React experimental
15
24
  const addTransitionType: ((type: string) => void) | undefined =
@@ -18,7 +27,6 @@ const addTransitionType: ((type: string) => void) | undefined =
18
27
  import { setupLinkInterception } from "./link-interceptor.js";
19
28
  import { createPartialUpdater } from "./partial-update.js";
20
29
  import { generateHistoryKey } from "./navigation-store.js";
21
- import { handleNavigationEnd } from "./scroll-restoration.js";
22
30
  import type { EventController } from "./event-controller.js";
23
31
  import { isInterceptOnlyCache } from "./intercept-utils.js";
24
32
  import {
@@ -35,11 +43,6 @@ if (typeof Symbol.dispose === "undefined") {
35
43
  (Symbol as any).dispose = Symbol("Symbol.dispose");
36
44
  }
37
45
 
38
- /** Get IDs of non-loader segments (layouts, routes, parallels). */
39
- function getNonLoaderSegmentIds(segments: ResolvedSegment[]): string[] {
40
- return segments.filter((s) => s.type !== "loader").map((s) => s.id);
41
- }
42
-
43
46
  export { createNavigationTransaction };
44
47
 
45
48
  /**
@@ -47,8 +50,13 @@ export { createNavigationTransaction };
47
50
  */
48
51
  export interface NavigationBridgeConfigWithController extends NavigationBridgeConfig {
49
52
  eventController: EventController;
50
- /** RSC version from initial payload metadata */
53
+ /** RSC version from initial payload metadata (fallback when appShellRef is not provided) */
51
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;
52
60
  }
53
61
 
54
62
  /**
@@ -67,8 +75,45 @@ export interface NavigationBridgeConfigWithController extends NavigationBridgeCo
67
75
  export function createNavigationBridge(
68
76
  config: NavigationBridgeConfigWithController,
69
77
  ): NavigationBridge {
70
- const { store, client, eventController, onUpdate, renderSegments, version } =
71
- config;
78
+ const {
79
+ store,
80
+ client,
81
+ eventController,
82
+ onUpdate,
83
+ renderSegments,
84
+ appShellRef,
85
+ } = config;
86
+ let version = config.version;
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
+ }
72
117
 
73
118
  // Create shared partial updater
74
119
  const fetchPartialUpdate = createPartialUpdater({
@@ -76,7 +121,8 @@ export function createNavigationBridge(
76
121
  client,
77
122
  onUpdate,
78
123
  renderSegments,
79
- version,
124
+ getVersion: () => version,
125
+ applyAppShell,
80
126
  });
81
127
 
82
128
  return {
@@ -114,6 +160,85 @@ export function createNavigationBridge(
114
160
  return;
115
161
  }
116
162
 
163
+ // Shallow navigation: skip RSC fetch when revalidate is false
164
+ // and the pathname hasn't changed (search param / hash only change).
165
+ if (
166
+ options?.revalidate === false &&
167
+ targetUrl.pathname === new URL(window.location.href).pathname
168
+ ) {
169
+ // Preserve intercept context from the current history entry so that
170
+ // popstate uses the correct cache key (:intercept suffix) and restores
171
+ // the right full-page vs modal semantics.
172
+ const currentHistoryState = window.history.state;
173
+ const isIntercept = currentHistoryState?.intercept === true;
174
+ const interceptSourceUrl = isIntercept
175
+ ? currentHistoryState?.sourceUrl
176
+ : undefined;
177
+
178
+ const historyKey = generateHistoryKey(url, { intercept: isIntercept });
179
+
180
+ // Copy current segments to the new history key so back/forward restores instantly
181
+ const currentKey = store.getHistoryKey();
182
+ const currentCache = store.getCachedSegments(currentKey);
183
+ if (currentCache?.segments) {
184
+ const currentHandleData = eventController.getHandleState().data;
185
+ store.cacheSegmentsForHistory(
186
+ historyKey,
187
+ currentCache.segments,
188
+ currentHandleData,
189
+ );
190
+ }
191
+
192
+ // Save current scroll position before changing URL
193
+ handleNavigationStart();
194
+
195
+ // Snapshot old state before pushState/replaceState overwrites it
196
+ const oldState = window.history.state;
197
+
198
+ // Update browser URL (carry intercept context into history state)
199
+ const historyState = buildHistoryState(
200
+ resolvedState,
201
+ {
202
+ intercept: isIntercept || undefined,
203
+ sourceUrl: interceptSourceUrl,
204
+ },
205
+ {},
206
+ );
207
+ if (options.replace) {
208
+ window.history.replaceState(historyState, "", url);
209
+ } else {
210
+ window.history.pushState(historyState, "", url);
211
+ }
212
+
213
+ // Ensure new history entry has a scroll restoration key
214
+ ensureHistoryKey();
215
+
216
+ // Notify useLocationState() hooks when state changes
217
+ const hasOldState =
218
+ oldState &&
219
+ typeof oldState === "object" &&
220
+ ("state" in oldState ||
221
+ Object.keys(oldState).some((k) => k.startsWith("__rsc_ls_")));
222
+ const hasNewState =
223
+ historyState &&
224
+ ("state" in historyState ||
225
+ Object.keys(historyState).some((k) => k.startsWith("__rsc_ls_")));
226
+ if (hasOldState || hasNewState) {
227
+ window.dispatchEvent(new Event("__rsc_locationstate"));
228
+ }
229
+
230
+ // Update store history key so future navigations reference the right cache
231
+ store.setHistoryKey(historyKey);
232
+ store.setCurrentUrl(url);
233
+
234
+ // Notify hooks — location updates, state stays idle
235
+ eventController.setLocation(targetUrl);
236
+
237
+ // Handle post-navigation scroll
238
+ handleNavigationEnd({ scroll: options.scroll });
239
+ return;
240
+ }
241
+
117
242
  // Only abort pending requests when navigating to a different route
118
243
  // Same-route navigation (e.g., /todos -> /todos) should not cancel in-flight actions
119
244
  const currentPath = new URL(window.location.href).pathname;
@@ -181,18 +306,24 @@ export function createNavigationBridge(
181
306
  // 2. routes that CAN be intercepted - we don't know if this navigation will intercept
182
307
  // 3. when leaving intercept - we need fresh non-intercept segments from server
183
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
184
310
  const hasUsableCache =
185
311
  cachedSegments &&
186
312
  cachedSegments.length > 0 &&
187
313
  !isInterceptOnlyCache(cachedSegments) &&
188
314
  !hasInterceptCache &&
189
315
  !isLeavingIntercept &&
316
+ !cached?.stale &&
190
317
  !options?._skipCache;
191
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.
192
323
  const tx = createNavigationTransaction(store, eventController, url, {
193
324
  ...options,
194
325
  state: resolvedState,
195
- skipLoadingState: hasUsableCache,
326
+ skipLoadingState: false,
196
327
  });
197
328
 
198
329
  // REVALIDATE: Fetch fresh data from server
@@ -200,7 +331,7 @@ export function createNavigationBridge(
200
331
  await fetchPartialUpdate(
201
332
  url,
202
333
  hasUsableCache
203
- ? getNonLoaderSegmentIds(cachedSegments!)
334
+ ? cachedSegments!.map((s) => s.id)
204
335
  : options?._skipCache
205
336
  ? [] // Action redirect: send no segments so server renders everything fresh
206
337
  : undefined,
@@ -332,6 +463,15 @@ export function createNavigationBridge(
332
463
  eventController.abortAllActions();
333
464
  }
334
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
+
335
475
  // Compute history key from URL (with intercept suffix if applicable)
336
476
  const historyKey = generateHistoryKey(url, { intercept: isIntercept });
337
477
 
@@ -368,6 +508,12 @@ export function createNavigationBridge(
368
508
  store.setCurrentUrl(url);
369
509
  store.setPath(new URL(url).pathname);
370
510
 
511
+ // Restore router identity from cache so subsequent navigations
512
+ // don't falsely detect an app switch.
513
+ if (cached?.routerId) {
514
+ store.setRouterId?.(cached.routerId);
515
+ }
516
+
371
517
  // Render from cache - force await to skip loading fallbacks
372
518
  try {
373
519
  const root = await renderSegments(cachedSegments, {
@@ -393,6 +539,7 @@ export function createNavigationBridge(
393
539
  cachedHandleData,
394
540
  params: cachedParams,
395
541
  },
542
+ scroll: { restore: true, isStreaming },
396
543
  };
397
544
  const hasTransition = cachedSegments.some((s) => s.transition);
398
545
  if (hasTransition) {
@@ -406,14 +553,11 @@ export function createNavigationBridge(
406
553
  onUpdate(popstateUpdate);
407
554
  }
408
555
 
409
- // Restore scroll position for back/forward navigation
410
- handleNavigationEnd({ restore: true, isStreaming });
411
-
412
556
  // SWR: If stale, trigger background revalidation
413
557
  if (isStale) {
414
558
  debugLog("[Browser] Cache is stale, background revalidating...");
415
559
  // Background revalidation - don't await, just fire and forget
416
- const segmentIds = getNonLoaderSegmentIds(cachedSegments);
560
+ const segmentIds = cachedSegments.map((s) => s.id);
417
561
 
418
562
  const tx = createNavigationTransaction(
419
563
  store,
@@ -478,7 +622,11 @@ export function createNavigationBridge(
478
622
  intercept: isIntercept,
479
623
  interceptSourceUrl,
480
624
  }),
481
- isIntercept ? { type: "navigate", interceptSourceUrl } : undefined,
625
+ isIntercept
626
+ ? { type: "navigate", interceptSourceUrl }
627
+ : isLeavingIntercept
628
+ ? { type: "leave-intercept" }
629
+ : undefined,
482
630
  );
483
631
  // Restore scroll position after fetch completes
484
632
  handleNavigationEnd({ restore: true, isStreaming });
@@ -555,6 +703,16 @@ export function createNavigationBridge(
555
703
  window.removeEventListener("pageshow", handlePageShow);
556
704
  };
557
705
  },
706
+
707
+ updateVersion(newVersion: string): void {
708
+ version = newVersion;
709
+ setAppVersion(newVersion);
710
+ store.clearHistoryCache();
711
+ },
712
+
713
+ updateAppShell(next: AppShell): void {
714
+ applyAppShell(next);
715
+ },
558
716
  };
559
717
  }
560
718