@rangojs/router 0.0.0-experimental.29 → 0.0.0-experimental.2a0dea97

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 (156) hide show
  1. package/AGENTS.md +4 -0
  2. package/README.md +78 -19
  3. package/dist/bin/rango.js +138 -50
  4. package/dist/vite/index.js +853 -435
  5. package/package.json +17 -16
  6. package/skills/breadcrumbs/SKILL.md +250 -0
  7. package/skills/cache-guide/SKILL.md +32 -0
  8. package/skills/caching/SKILL.md +45 -4
  9. package/skills/handler-use/SKILL.md +362 -0
  10. package/skills/hooks/SKILL.md +22 -4
  11. package/skills/intercept/SKILL.md +20 -0
  12. package/skills/layout/SKILL.md +22 -0
  13. package/skills/links/SKILL.md +3 -1
  14. package/skills/loader/SKILL.md +71 -21
  15. package/skills/middleware/SKILL.md +34 -3
  16. package/skills/migrate-nextjs/SKILL.md +560 -0
  17. package/skills/migrate-react-router/SKILL.md +764 -0
  18. package/skills/parallel/SKILL.md +185 -0
  19. package/skills/prerender/SKILL.md +110 -68
  20. package/skills/rango/SKILL.md +24 -22
  21. package/skills/route/SKILL.md +56 -2
  22. package/skills/router-setup/SKILL.md +87 -2
  23. package/skills/typesafety/SKILL.md +33 -21
  24. package/src/__internal.ts +92 -0
  25. package/src/browser/app-version.ts +14 -0
  26. package/src/browser/event-controller.ts +5 -0
  27. package/src/browser/link-interceptor.ts +4 -0
  28. package/src/browser/navigation-bridge.ts +125 -16
  29. package/src/browser/navigation-client.ts +142 -57
  30. package/src/browser/navigation-store.ts +43 -8
  31. package/src/browser/navigation-transaction.ts +11 -9
  32. package/src/browser/partial-update.ts +94 -17
  33. package/src/browser/prefetch/cache.ts +82 -12
  34. package/src/browser/prefetch/fetch.ts +98 -27
  35. package/src/browser/prefetch/policy.ts +6 -0
  36. package/src/browser/prefetch/queue.ts +92 -20
  37. package/src/browser/prefetch/resource-ready.ts +77 -0
  38. package/src/browser/react/Link.tsx +88 -9
  39. package/src/browser/react/NavigationProvider.tsx +40 -4
  40. package/src/browser/react/context.ts +7 -2
  41. package/src/browser/react/use-handle.ts +9 -58
  42. package/src/browser/react/use-router.ts +21 -8
  43. package/src/browser/rsc-router.tsx +134 -59
  44. package/src/browser/scroll-restoration.ts +41 -42
  45. package/src/browser/segment-reconciler.ts +72 -10
  46. package/src/browser/server-action-bridge.ts +8 -6
  47. package/src/browser/types.ts +55 -5
  48. package/src/build/generate-manifest.ts +6 -6
  49. package/src/build/generate-route-types.ts +3 -0
  50. package/src/build/route-trie.ts +50 -24
  51. package/src/build/route-types/include-resolution.ts +8 -1
  52. package/src/build/route-types/router-processing.ts +223 -74
  53. package/src/build/route-types/scan-filter.ts +8 -1
  54. package/src/cache/cache-runtime.ts +15 -11
  55. package/src/cache/cache-scope.ts +48 -7
  56. package/src/cache/cf/cf-cache-store.ts +453 -11
  57. package/src/cache/cf/index.ts +5 -1
  58. package/src/cache/document-cache.ts +17 -7
  59. package/src/cache/index.ts +1 -0
  60. package/src/cache/taint.ts +55 -0
  61. package/src/client.rsc.tsx +2 -0
  62. package/src/client.tsx +6 -66
  63. package/src/context-var.ts +72 -2
  64. package/src/debug.ts +2 -2
  65. package/src/handle.ts +40 -0
  66. package/src/handles/breadcrumbs.ts +66 -0
  67. package/src/handles/index.ts +1 -0
  68. package/src/index.rsc.ts +6 -36
  69. package/src/index.ts +50 -43
  70. package/src/prerender/store.ts +5 -4
  71. package/src/prerender.ts +138 -77
  72. package/src/reverse.ts +25 -1
  73. package/src/route-definition/dsl-helpers.ts +224 -37
  74. package/src/route-definition/helpers-types.ts +67 -19
  75. package/src/route-definition/index.ts +3 -0
  76. package/src/route-definition/redirect.ts +11 -3
  77. package/src/route-definition/resolve-handler-use.ts +149 -0
  78. package/src/route-map-builder.ts +7 -1
  79. package/src/route-types.ts +11 -0
  80. package/src/router/content-negotiation.ts +100 -1
  81. package/src/router/find-match.ts +4 -2
  82. package/src/router/handler-context.ts +111 -25
  83. package/src/router/intercept-resolution.ts +11 -4
  84. package/src/router/lazy-includes.ts +4 -1
  85. package/src/router/loader-resolution.ts +156 -21
  86. package/src/router/logging.ts +5 -2
  87. package/src/router/manifest.ts +9 -3
  88. package/src/router/match-api.ts +125 -190
  89. package/src/router/match-middleware/background-revalidation.ts +30 -2
  90. package/src/router/match-middleware/cache-lookup.ts +94 -17
  91. package/src/router/match-middleware/cache-store.ts +53 -10
  92. package/src/router/match-middleware/intercept-resolution.ts +9 -7
  93. package/src/router/match-middleware/segment-resolution.ts +61 -5
  94. package/src/router/match-result.ts +104 -10
  95. package/src/router/metrics.ts +6 -1
  96. package/src/router/middleware-types.ts +16 -22
  97. package/src/router/middleware.ts +24 -30
  98. package/src/router/navigation-snapshot.ts +182 -0
  99. package/src/router/prerender-match.ts +114 -10
  100. package/src/router/preview-match.ts +30 -102
  101. package/src/router/request-classification.ts +310 -0
  102. package/src/router/route-snapshot.ts +245 -0
  103. package/src/router/router-context.ts +6 -1
  104. package/src/router/router-interfaces.ts +36 -4
  105. package/src/router/router-options.ts +37 -11
  106. package/src/router/segment-resolution/fresh.ts +198 -20
  107. package/src/router/segment-resolution/helpers.ts +30 -25
  108. package/src/router/segment-resolution/loader-cache.ts +1 -0
  109. package/src/router/segment-resolution/revalidation.ts +438 -300
  110. package/src/router/segment-wrappers.ts +2 -0
  111. package/src/router/types.ts +1 -0
  112. package/src/router.ts +59 -6
  113. package/src/rsc/handler.ts +472 -372
  114. package/src/rsc/loader-fetch.ts +23 -3
  115. package/src/rsc/manifest-init.ts +5 -1
  116. package/src/rsc/progressive-enhancement.ts +14 -2
  117. package/src/rsc/rsc-rendering.ts +12 -1
  118. package/src/rsc/server-action.ts +8 -0
  119. package/src/rsc/ssr-setup.ts +2 -2
  120. package/src/rsc/types.ts +9 -1
  121. package/src/segment-content-promise.ts +33 -0
  122. package/src/segment-system.tsx +164 -23
  123. package/src/server/context.ts +140 -14
  124. package/src/server/handle-store.ts +19 -0
  125. package/src/server/loader-registry.ts +9 -8
  126. package/src/server/request-context.ts +204 -28
  127. package/src/ssr/index.tsx +4 -0
  128. package/src/static-handler.ts +18 -6
  129. package/src/types/cache-types.ts +4 -4
  130. package/src/types/handler-context.ts +149 -49
  131. package/src/types/loader-types.ts +36 -9
  132. package/src/types/route-entry.ts +8 -1
  133. package/src/types/segments.ts +6 -0
  134. package/src/urls/path-helper-types.ts +39 -6
  135. package/src/urls/path-helper.ts +48 -13
  136. package/src/urls/pattern-types.ts +12 -0
  137. package/src/urls/response-types.ts +16 -6
  138. package/src/use-loader.tsx +77 -5
  139. package/src/vite/discovery/bundle-postprocess.ts +30 -33
  140. package/src/vite/discovery/discover-routers.ts +5 -1
  141. package/src/vite/discovery/prerender-collection.ts +128 -74
  142. package/src/vite/discovery/state.ts +13 -6
  143. package/src/vite/index.ts +4 -0
  144. package/src/vite/plugin-types.ts +51 -79
  145. package/src/vite/plugins/expose-action-id.ts +1 -3
  146. package/src/vite/plugins/expose-id-utils.ts +12 -0
  147. package/src/vite/plugins/expose-ids/handler-transform.ts +30 -0
  148. package/src/vite/plugins/expose-internal-ids.ts +257 -40
  149. package/src/vite/plugins/performance-tracks.ts +88 -0
  150. package/src/vite/plugins/refresh-cmd.ts +88 -26
  151. package/src/vite/plugins/version-plugin.ts +13 -1
  152. package/src/vite/rango.ts +163 -211
  153. package/src/vite/router-discovery.ts +178 -45
  154. package/src/vite/utils/banner.ts +3 -3
  155. package/src/vite/utils/prerender-utils.ts +37 -5
  156. package/src/vite/utils/shared-utils.ts +3 -2
@@ -181,6 +181,37 @@ String keys still work (`ctx.set("key", value)` / `ctx.get("key")`), but
181
181
  Only route handlers and middleware can call `ctx.set()`. Layouts, parallels,
182
182
  and intercepts can only read via `ctx.get()`.
183
183
 
184
+ #### Non-cacheable context variables
185
+
186
+ Mark a var as non-cacheable when it holds inherently request-specific data
187
+ (sessions, auth tokens, per-request IDs). There are two ways:
188
+
189
+ ```typescript
190
+ // Var-level: every value written to this var is non-cacheable
191
+ const Session = createVar<SessionData>({ cache: false });
192
+
193
+ // Write-level: escalate a normally-cacheable var for this specific write
194
+ const Theme = createVar<string>();
195
+ ctx.set(Theme, userTheme, { cache: false });
196
+ ```
197
+
198
+ "Least cacheable wins" — if either the var definition or the write site says
199
+ `cache: false`, the value is non-cacheable.
200
+
201
+ Reading a non-cacheable var inside `cache()` or `"use cache"` throws at
202
+ runtime. This prevents request-specific data from leaking into cached output:
203
+
204
+ ```typescript
205
+ // This throws — Session is non-cacheable
206
+ async function CachedWidget(ctx) {
207
+ "use cache";
208
+ const session = ctx.get(Session); // Error: non-cacheable var read inside cache scope
209
+ return <Widget />;
210
+ }
211
+ ```
212
+
213
+ Cacheable vars (the default) can be read freely inside cache scopes.
214
+
184
215
  ### Revalidation Contracts for Handler Data
185
216
 
186
217
  Handler-first guarantees apply within a single full render pass. For partial
@@ -352,11 +383,34 @@ urls(({ path, layout }) => [
352
383
  ])
353
384
  ```
354
385
 
386
+ ## Handler-attached `.use`
387
+
388
+ Page handlers can carry their own loader, middleware, error boundaries, parallels, and other defaults via a `.use` callback — so the page is self-contained and reusable across mount sites without re-wiring the same items.
389
+
390
+ ```typescript
391
+ const ProductPage: Handler<"/product/:slug"> = async (ctx) => {
392
+ const product = await ctx.use(ProductLoader);
393
+ return <ProductView product={product} />;
394
+ };
395
+ ProductPage.use = () => [
396
+ loader(ProductLoader),
397
+ loading(<ProductSkeleton />),
398
+ middleware(async (ctx, next) => {
399
+ await next();
400
+ ctx.header("Cache-Control", "private, max-age=60");
401
+ }),
402
+ ];
403
+
404
+ // Mount site has no per-page wiring — defaults travel with the handler.
405
+ path("/product/:slug", ProductPage, { name: "product" });
406
+ ```
407
+
408
+ Explicit `use()` at the mount site merges with `handler.use` (handler defaults first, explicit second). See [skills/handler-use](../handler-use/SKILL.md) for the merge order, allowed item types per mount site, and override semantics.
409
+
355
410
  ## Complete Example
356
411
 
357
412
  ```typescript
358
- import { urls } from "@rangojs/router";
359
- import { Breadcrumbs } from "./handles/breadcrumbs";
413
+ import { urls, Breadcrumbs } from "@rangojs/router";
360
414
 
361
415
  export const urlpatterns = urls(({ path, layout, loader, loading }) => [
362
416
  // Simple route
@@ -78,16 +78,21 @@ interface RSCRouterOptions<TEnv> {
78
78
  // Document component wrapping entire app
79
79
  document?: ComponentType<{ children: ReactNode }>;
80
80
 
81
+ // URL prefix for sub-path deployments (e.g. "/admin")
82
+ // All routes, reverse(), href(), Link, redirect(), and router.use()
83
+ // patterns are automatically prefixed. Route names stay unprefixed.
84
+ basename?: string;
85
+
81
86
  // Enable per-request performance timeline (console waterfall + Server-Timing header)
82
87
  debugPerformance?: boolean;
83
88
 
84
89
  // Default error boundary
85
90
  defaultErrorBoundary?: ReactNode | ErrorBoundaryHandler;
86
91
 
87
- // Default not-found boundary
92
+ // Default not-found boundary for notFound() thrown in handlers/loaders
88
93
  defaultNotFoundBoundary?: ReactNode | NotFoundBoundaryHandler;
89
94
 
90
- // Component for 404 routes
95
+ // Component for 404 (no route match, or notFound() without a boundary)
91
96
  notFound?: ReactNode | ((props: { pathname: string }) => ReactNode);
92
97
 
93
98
  // Error logging callback
@@ -124,6 +129,36 @@ interface RSCRouterOptions<TEnv> {
124
129
  }
125
130
  ```
126
131
 
132
+ ## Basename (Sub-Path Deployment)
133
+
134
+ When your app is served under a sub-path (e.g. `/admin` or `/v2`), set `basename`:
135
+
136
+ ```typescript
137
+ const router = createRouter({
138
+ basename: "/admin",
139
+ document: Document,
140
+ }).routes(({ path, include }) => [
141
+ path("/", Dashboard, { name: "home" }), // matches /admin
142
+ path("/users", Users, { name: "users" }), // matches /admin/users
143
+ include("/api", apiPatterns, { name: "api" }), // matches /admin/api/*
144
+ ]);
145
+
146
+ router.reverse("home"); // "/admin"
147
+ router.reverse("users"); // "/admin/users"
148
+ ```
149
+
150
+ Router-owned APIs are basename-aware:
151
+
152
+ - `reverse()` returns prefixed paths
153
+ - `<Link to="/users">` renders `<a href="/admin/users">`
154
+ - `redirect("/login")` redirects to `"/admin/login"`
155
+ - `router.use("/users/*", mw)` matches `/admin/users/*`
156
+ - `useRouter().push("/users")` navigates to `/admin/users`
157
+ - Route names stay unprefixed (`"home"`, not `"admin.home"`)
158
+
159
+ Note: `href()` is a raw path helper and does **not** auto-prefix with basename.
160
+ Use `reverse()` or `<Link>` for basename-aware URLs.
161
+
127
162
  ## Using the Request Handler
128
163
 
129
164
  The router provides a `fetch` method to handle RSC requests:
@@ -290,6 +325,56 @@ const router = createRouter({
290
325
  export default router;
291
326
  ```
292
327
 
328
+ ## Not Found Handling
329
+
330
+ Two distinct 404 scenarios:
331
+
332
+ **1. No route matches the URL** — the router renders the `notFound` component from `createRouter()` config. This is automatic.
333
+
334
+ **2. A handler/loader calls `notFound()`** — signals that the route matched but the data doesn't exist (e.g., invalid product ID).
335
+
336
+ ```typescript
337
+ import { notFound } from "@rangojs/router";
338
+
339
+ // In a handler or loader
340
+ path("/product/:slug", async (ctx) => {
341
+ const product = await db.getProduct(ctx.params.slug);
342
+ if (!product) notFound("Product not found");
343
+ return <ProductPage product={product} />;
344
+ });
345
+ ```
346
+
347
+ ### Fallback chain for `notFound()`
348
+
349
+ When `notFound()` is thrown, the router looks for a fallback in this order:
350
+
351
+ 1. **`notFoundBoundary()`** — nearest boundary in the route tree (route-level)
352
+ 2. **`defaultNotFoundBoundary`** — from `createRouter()` config (app-level)
353
+ 3. **`notFound`** — from `createRouter()` config (same component used for no-route-match)
354
+ 4. **Default `<h1>Not Found</h1>`** — built-in fallback
355
+
356
+ All cases set HTTP 404 status.
357
+
358
+ ### notFoundBoundary
359
+
360
+ Wrap routes with `notFoundBoundary()` for route-specific not-found UI:
361
+
362
+ ```typescript
363
+ urls(({ path, layout }) => [
364
+ layout(ShopLayout, () => [
365
+ notFoundBoundary(({ notFound: info }) => (
366
+ <div>
367
+ <h1>Not Found</h1>
368
+ <p>{info.message}</p>
369
+ </div>
370
+ )),
371
+ path("/product/:slug", ProductPage),
372
+ ]),
373
+ ]);
374
+ ```
375
+
376
+ `notFoundBoundary` receives `{ notFound: NotFoundInfo }` where `NotFoundInfo` contains `message`, `segmentId`, `segmentType`, and `pathname`.
377
+
293
378
  ## Including Sub-patterns
294
379
 
295
380
  ```typescript
@@ -369,8 +369,18 @@ interface PaginationData {
369
369
  perPage: number;
370
370
  }
371
371
  export const Pagination = createVar<PaginationData>();
372
+
373
+ // Non-cacheable var — reading inside cache() or "use cache" throws at runtime
374
+ const Session = createVar<SessionData>({ cache: false });
372
375
  ```
373
376
 
377
+ `createVar` accepts an optional options object. The `cache` option (default
378
+ `true`) controls whether the var's values can be read inside cache scopes.
379
+ Write-level escalation is also supported: `ctx.set(Var, value, { cache: false })`
380
+ marks a specific write as non-cacheable even if the var itself is cacheable.
381
+ "Least cacheable wins" — if either says `cache: false`, the value throws on
382
+ read inside `cache()` or `"use cache"`.
383
+
374
384
  ### Producer (handler or middleware)
375
385
 
376
386
  ```typescript
@@ -414,26 +424,30 @@ Both approaches coexist: `ctx.get("user")` (global via Vars) and
414
424
  Handles have typed data:
415
425
 
416
426
  ```typescript
417
- // handles/breadcrumbs.ts
418
- import { createHandle } from "@rangojs/router";
419
-
420
- // All export patterns work: export const, const + export { X }, export { X as Y }
421
- export const Breadcrumbs = createHandle<{ label: string; href: string }>();
422
-
423
- // In route definition - use handle() DSL
424
- import { urls } from "@rangojs/router";
425
-
426
- export const urlpatterns = urls(({ path, handle }) => [
427
- path("/shop/product/:slug", ProductPage, { name: "product" }, () => [
428
- handle(Breadcrumbs, { label: "Products", href: "/shop/products" }),
429
- ]),
430
- ]);
431
-
432
- // In client - typed array
427
+ // Built-in Breadcrumbs handle — import from "@rangojs/router"
428
+ import { Breadcrumbs } from "@rangojs/router";
429
+ // Type: Handle<BreadcrumbItem, BreadcrumbItem[]>
430
+ // BreadcrumbItem: { label: string; href: string; content?: ReactNode | Promise<ReactNode> }
431
+
432
+ // In route handler — push is fully typed
433
+ path("/shop/product/:slug", (ctx) => {
434
+ const breadcrumb = ctx.use(Breadcrumbs);
435
+ breadcrumb({ label: "Products", href: "/shop/products" });
436
+ return <ProductPage />;
437
+ }, { name: "product" });
438
+
439
+ // In client — typed array
440
+ import { useHandle, Breadcrumbs } from "@rangojs/router/client";
433
441
  function BreadcrumbNav() {
434
442
  const crumbs = useHandle(Breadcrumbs);
435
- // crumbs: Array<{ label: string; href: string }>
443
+ // crumbs: BreadcrumbItem[]
436
444
  }
445
+
446
+ // Custom handles also work the same way
447
+ import { createHandle } from "@rangojs/router";
448
+ export const PageTitle = createHandle<string, string>(
449
+ (segments) => segments.flat().at(-1) ?? "Default Title"
450
+ );
437
451
  ```
438
452
 
439
453
  ## Ref Prop Type Safety (Loaders & Handles)
@@ -447,14 +461,12 @@ export const ProductLoader = createLoader(async (ctx) => {
447
461
  return { product: await fetchProduct(ctx.params.slug) };
448
462
  });
449
463
 
450
- // handles.ts
451
- export const Breadcrumbs = createHandle<{ label: string; href: string }>();
464
+ // Built-in Breadcrumbs — or any custom handle created with createHandle()
452
465
 
453
466
  // Client component — typeof infers all generics
454
467
  ("use client");
455
- import { useLoader, useHandle } from "@rangojs/router/client";
468
+ import { useLoader, useHandle, type Breadcrumbs } from "@rangojs/router/client";
456
469
  import type { ProductLoader } from "../loaders";
457
- import type { Breadcrumbs } from "../handles";
458
470
 
459
471
  function MyComponent({
460
472
  loader,
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,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,19 @@ import type {
4
4
  NavigateOptionsInternal,
5
5
  ResolvedSegment,
6
6
  } from "./types.js";
7
+ import { setAppVersion } from "./app-version.js";
7
8
  import * as React from "react";
8
9
  import { startTransition } from "react";
9
10
  import {
10
11
  createNavigationTransaction,
11
12
  resolveNavigationState,
12
13
  } from "./navigation-transaction.js";
14
+ import { buildHistoryState } from "./history-state.js";
15
+ import {
16
+ handleNavigationStart,
17
+ handleNavigationEnd,
18
+ ensureHistoryKey,
19
+ } from "./scroll-restoration.js";
13
20
 
14
21
  // addTransitionType is only available in React experimental
15
22
  const addTransitionType: ((type: string) => void) | undefined =
@@ -18,7 +25,6 @@ const addTransitionType: ((type: string) => void) | undefined =
18
25
  import { setupLinkInterception } from "./link-interceptor.js";
19
26
  import { createPartialUpdater } from "./partial-update.js";
20
27
  import { generateHistoryKey } from "./navigation-store.js";
21
- import { handleNavigationEnd } from "./scroll-restoration.js";
22
28
  import type { EventController } from "./event-controller.js";
23
29
  import { isInterceptOnlyCache } from "./intercept-utils.js";
24
30
  import {
@@ -35,11 +41,6 @@ if (typeof Symbol.dispose === "undefined") {
35
41
  (Symbol as any).dispose = Symbol("Symbol.dispose");
36
42
  }
37
43
 
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
44
  export { createNavigationTransaction };
44
45
 
45
46
  /**
@@ -67,8 +68,8 @@ export interface NavigationBridgeConfigWithController extends NavigationBridgeCo
67
68
  export function createNavigationBridge(
68
69
  config: NavigationBridgeConfigWithController,
69
70
  ): NavigationBridge {
70
- const { store, client, eventController, onUpdate, renderSegments, version } =
71
- config;
71
+ const { store, client, eventController, onUpdate, renderSegments } = config;
72
+ let version = config.version;
72
73
 
73
74
  // Create shared partial updater
74
75
  const fetchPartialUpdate = createPartialUpdater({
@@ -76,7 +77,7 @@ export function createNavigationBridge(
76
77
  client,
77
78
  onUpdate,
78
79
  renderSegments,
79
- version,
80
+ getVersion: () => version,
80
81
  });
81
82
 
82
83
  return {
@@ -114,6 +115,85 @@ export function createNavigationBridge(
114
115
  return;
115
116
  }
116
117
 
118
+ // Shallow navigation: skip RSC fetch when revalidate is false
119
+ // and the pathname hasn't changed (search param / hash only change).
120
+ if (
121
+ options?.revalidate === false &&
122
+ targetUrl.pathname === new URL(window.location.href).pathname
123
+ ) {
124
+ // Preserve intercept context from the current history entry so that
125
+ // popstate uses the correct cache key (:intercept suffix) and restores
126
+ // the right full-page vs modal semantics.
127
+ const currentHistoryState = window.history.state;
128
+ const isIntercept = currentHistoryState?.intercept === true;
129
+ const interceptSourceUrl = isIntercept
130
+ ? currentHistoryState?.sourceUrl
131
+ : undefined;
132
+
133
+ const historyKey = generateHistoryKey(url, { intercept: isIntercept });
134
+
135
+ // Copy current segments to the new history key so back/forward restores instantly
136
+ const currentKey = store.getHistoryKey();
137
+ const currentCache = store.getCachedSegments(currentKey);
138
+ if (currentCache?.segments) {
139
+ const currentHandleData = eventController.getHandleState().data;
140
+ store.cacheSegmentsForHistory(
141
+ historyKey,
142
+ currentCache.segments,
143
+ currentHandleData,
144
+ );
145
+ }
146
+
147
+ // Save current scroll position before changing URL
148
+ handleNavigationStart();
149
+
150
+ // Snapshot old state before pushState/replaceState overwrites it
151
+ const oldState = window.history.state;
152
+
153
+ // Update browser URL (carry intercept context into history state)
154
+ const historyState = buildHistoryState(
155
+ resolvedState,
156
+ {
157
+ intercept: isIntercept || undefined,
158
+ sourceUrl: interceptSourceUrl,
159
+ },
160
+ {},
161
+ );
162
+ if (options.replace) {
163
+ window.history.replaceState(historyState, "", url);
164
+ } else {
165
+ window.history.pushState(historyState, "", url);
166
+ }
167
+
168
+ // Ensure new history entry has a scroll restoration key
169
+ ensureHistoryKey();
170
+
171
+ // Notify useLocationState() hooks when state changes
172
+ const hasOldState =
173
+ oldState &&
174
+ typeof oldState === "object" &&
175
+ ("state" in oldState ||
176
+ Object.keys(oldState).some((k) => k.startsWith("__rsc_ls_")));
177
+ const hasNewState =
178
+ historyState &&
179
+ ("state" in historyState ||
180
+ Object.keys(historyState).some((k) => k.startsWith("__rsc_ls_")));
181
+ if (hasOldState || hasNewState) {
182
+ window.dispatchEvent(new Event("__rsc_locationstate"));
183
+ }
184
+
185
+ // Update store history key so future navigations reference the right cache
186
+ store.setHistoryKey(historyKey);
187
+ store.setCurrentUrl(url);
188
+
189
+ // Notify hooks — location updates, state stays idle
190
+ eventController.setLocation(targetUrl);
191
+
192
+ // Handle post-navigation scroll
193
+ handleNavigationEnd({ scroll: options.scroll });
194
+ return;
195
+ }
196
+
117
197
  // Only abort pending requests when navigating to a different route
118
198
  // Same-route navigation (e.g., /todos -> /todos) should not cancel in-flight actions
119
199
  const currentPath = new URL(window.location.href).pathname;
@@ -181,18 +261,24 @@ export function createNavigationBridge(
181
261
  // 2. routes that CAN be intercepted - we don't know if this navigation will intercept
182
262
  // 3. when leaving intercept - we need fresh non-intercept segments from server
183
263
  // 4. redirect-with-state - force re-render so hooks read fresh state
264
+ // 5. stale cache - server action invalidated it, need fresh data with loading state
184
265
  const hasUsableCache =
185
266
  cachedSegments &&
186
267
  cachedSegments.length > 0 &&
187
268
  !isInterceptOnlyCache(cachedSegments) &&
188
269
  !hasInterceptCache &&
189
270
  !isLeavingIntercept &&
271
+ !cached?.stale &&
190
272
  !options?._skipCache;
191
273
 
274
+ // Forward navigations always await fetchPartialUpdate before rendering,
275
+ // so useNavigation should always report "loading". skipLoadingState is
276
+ // only used for popstate background revalidation (line ~526) where
277
+ // cached content renders instantly without a network wait.
192
278
  const tx = createNavigationTransaction(store, eventController, url, {
193
279
  ...options,
194
280
  state: resolvedState,
195
- skipLoadingState: hasUsableCache,
281
+ skipLoadingState: false,
196
282
  });
197
283
 
198
284
  // REVALIDATE: Fetch fresh data from server
@@ -200,7 +286,7 @@ export function createNavigationBridge(
200
286
  await fetchPartialUpdate(
201
287
  url,
202
288
  hasUsableCache
203
- ? getNonLoaderSegmentIds(cachedSegments!)
289
+ ? cachedSegments!.map((s) => s.id)
204
290
  : options?._skipCache
205
291
  ? [] // Action redirect: send no segments so server renders everything fresh
206
292
  : undefined,
@@ -332,6 +418,15 @@ export function createNavigationBridge(
332
418
  eventController.abortAllActions();
333
419
  }
334
420
 
421
+ // Popstate that exits an intercept to a non-intercept destination. The
422
+ // fallback fetch path below needs `leave-intercept` mode so it filters
423
+ // the cached @modal segment from the request and forces a re-render —
424
+ // otherwise a cache-miss popstate whose server response has an empty
425
+ // diff hits the "no changes" branch in partial-update and the modal
426
+ // stays on screen.
427
+ const isLeavingIntercept =
428
+ !isIntercept && currentInterceptSource !== null;
429
+
335
430
  // Compute history key from URL (with intercept suffix if applicable)
336
431
  const historyKey = generateHistoryKey(url, { intercept: isIntercept });
337
432
 
@@ -368,6 +463,12 @@ export function createNavigationBridge(
368
463
  store.setCurrentUrl(url);
369
464
  store.setPath(new URL(url).pathname);
370
465
 
466
+ // Restore router identity from cache so subsequent navigations
467
+ // don't falsely detect an app switch.
468
+ if (cached?.routerId) {
469
+ store.setRouterId?.(cached.routerId);
470
+ }
471
+
371
472
  // Render from cache - force await to skip loading fallbacks
372
473
  try {
373
474
  const root = await renderSegments(cachedSegments, {
@@ -393,6 +494,7 @@ export function createNavigationBridge(
393
494
  cachedHandleData,
394
495
  params: cachedParams,
395
496
  },
497
+ scroll: { restore: true, isStreaming },
396
498
  };
397
499
  const hasTransition = cachedSegments.some((s) => s.transition);
398
500
  if (hasTransition) {
@@ -406,14 +508,11 @@ export function createNavigationBridge(
406
508
  onUpdate(popstateUpdate);
407
509
  }
408
510
 
409
- // Restore scroll position for back/forward navigation
410
- handleNavigationEnd({ restore: true, isStreaming });
411
-
412
511
  // SWR: If stale, trigger background revalidation
413
512
  if (isStale) {
414
513
  debugLog("[Browser] Cache is stale, background revalidating...");
415
514
  // Background revalidation - don't await, just fire and forget
416
- const segmentIds = getNonLoaderSegmentIds(cachedSegments);
515
+ const segmentIds = cachedSegments.map((s) => s.id);
417
516
 
418
517
  const tx = createNavigationTransaction(
419
518
  store,
@@ -478,7 +577,11 @@ export function createNavigationBridge(
478
577
  intercept: isIntercept,
479
578
  interceptSourceUrl,
480
579
  }),
481
- isIntercept ? { type: "navigate", interceptSourceUrl } : undefined,
580
+ isIntercept
581
+ ? { type: "navigate", interceptSourceUrl }
582
+ : isLeavingIntercept
583
+ ? { type: "leave-intercept" }
584
+ : undefined,
482
585
  );
483
586
  // Restore scroll position after fetch completes
484
587
  handleNavigationEnd({ restore: true, isStreaming });
@@ -555,6 +658,12 @@ export function createNavigationBridge(
555
658
  window.removeEventListener("pageshow", handlePageShow);
556
659
  };
557
660
  },
661
+
662
+ updateVersion(newVersion: string): void {
663
+ version = newVersion;
664
+ setAppVersion(newVersion);
665
+ store.clearHistoryCache();
666
+ },
558
667
  };
559
668
  }
560
669