@rangojs/router 0.24.4 → 0.26.0

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 (301) hide show
  1. package/README.md +1 -0
  2. package/dist/bin/rango.js +363 -39
  3. package/dist/testing/vitest.js +37 -4
  4. package/dist/types/browser/event-controller.d.ts +24 -5
  5. package/dist/types/browser/intercept-utils.d.ts +5 -2
  6. package/dist/types/browser/merge-segment-loaders.d.ts +0 -23
  7. package/dist/types/browser/navigation-transaction.d.ts +5 -5
  8. package/dist/types/browser/partial-update.d.ts +21 -13
  9. package/dist/types/browser/prefetch/cache.d.ts +8 -0
  10. package/dist/types/browser/rango-marker.d.ts +7 -0
  11. package/dist/types/browser/react/filter-segment-order.d.ts +10 -0
  12. package/dist/types/browser/react/use-handle.d.ts +20 -0
  13. package/dist/types/browser/react/use-href.d.ts +6 -0
  14. package/dist/types/browser/react/use-router.d.ts +6 -1
  15. package/dist/types/browser/response-adapter.d.ts +5 -3
  16. package/dist/types/browser/scroll-restoration.d.ts +9 -3
  17. package/dist/types/browser/segment-reconciler.d.ts +10 -13
  18. package/dist/types/browser/types.d.ts +38 -8
  19. package/dist/types/build/route-types/per-module-writer.d.ts +12 -0
  20. package/dist/types/cache/cf/cf-cache-store.d.ts +0 -2
  21. package/dist/types/cache/cf/cf-cache-types.d.ts +0 -16
  22. package/dist/types/cache/cf/cf-shell-frame.d.ts +2 -0
  23. package/dist/types/cache/handle-snapshot.d.ts +2 -2
  24. package/dist/types/cache/types.d.ts +16 -6
  25. package/dist/types/cache/vercel/vercel-cache-store.d.ts +1 -15
  26. package/dist/types/client-urls/client-urls.d.ts +6 -2
  27. package/dist/types/client-urls/revalidate-registry.d.ts +1 -1
  28. package/dist/types/client-urls/server-projection.d.ts +9 -5
  29. package/dist/types/client-urls/types.d.ts +29 -16
  30. package/dist/types/client.d.ts +2 -2
  31. package/dist/types/client.rsc.d.ts +2 -2
  32. package/dist/types/decode-loader-results.d.ts +28 -7
  33. package/dist/types/handles/deferred-resolution.d.ts +3 -2
  34. package/dist/types/handles/meta.d.ts +5 -0
  35. package/dist/types/href-client.d.ts +3 -1
  36. package/dist/types/index.rsc.d.ts +1 -1
  37. package/dist/types/loader-redirect.d.ts +2 -3
  38. package/dist/types/outlet-context.d.ts +7 -6
  39. package/dist/types/prefetch-markers.d.ts +14 -5
  40. package/dist/types/prerender/build-shell-capture.d.ts +1 -2
  41. package/dist/types/prerender/writable-store.d.ts +2 -3
  42. package/dist/types/route-content-wrapper.d.ts +17 -14
  43. package/dist/types/route-definition/helpers-types.d.ts +7 -2
  44. package/dist/types/route-definition/resolve-handler-use.d.ts +6 -1
  45. package/dist/types/route-definition/rewrite.d.ts +48 -5
  46. package/dist/types/route-types.d.ts +2 -2
  47. package/dist/types/router/handler-context.d.ts +14 -2
  48. package/dist/types/router/intercept-resolution.d.ts +19 -46
  49. package/dist/types/router/lazy-includes.d.ts +7 -0
  50. package/dist/types/router/match-middleware/intercept-resolution.d.ts +2 -2
  51. package/dist/types/router/middleware.d.ts +0 -16
  52. package/dist/types/router/navigation-snapshot.d.ts +2 -0
  53. package/dist/types/router/prefetch-cache-ttl.d.ts +8 -11
  54. package/dist/types/router/revalidation.d.ts +8 -1
  55. package/dist/types/router/router-context.d.ts +4 -16
  56. package/dist/types/router/router-interfaces.d.ts +0 -11
  57. package/dist/types/router/router-options.d.ts +4 -27
  58. package/dist/types/router/segment-resolution/mask-nested.d.ts +5 -3
  59. package/dist/types/router/segment-resolution/prefetch-deferral.d.ts +31 -8
  60. package/dist/types/router/segment-resolution/revalidation.d.ts +38 -4
  61. package/dist/types/router/segment-resolution.d.ts +1 -1
  62. package/dist/types/router/segment-wrappers.d.ts +3 -5
  63. package/dist/types/router/substitute-pattern-params.d.ts +3 -2
  64. package/dist/types/router/tracing.d.ts +2 -2
  65. package/dist/types/router-versions.d.ts +8 -6
  66. package/dist/types/rsc/full-payload.d.ts +14 -2
  67. package/dist/types/rsc/helpers.d.ts +8 -0
  68. package/dist/types/rsc/manifest-init.d.ts +20 -0
  69. package/dist/types/rsc/prefetch-render-error.d.ts +22 -0
  70. package/dist/types/rsc/progressive-enhancement.d.ts +20 -0
  71. package/dist/types/rsc/rewrite-passes.d.ts +12 -2
  72. package/dist/types/rsc/server-action.d.ts +6 -0
  73. package/dist/types/rsc/shell-capture-constants.d.ts +14 -4
  74. package/dist/types/rsc/shell-capture.d.ts +23 -2
  75. package/dist/types/rsc/types.d.ts +5 -8
  76. package/dist/types/segment-system.d.ts +15 -0
  77. package/dist/types/server/build-version-table.d.ts +10 -13
  78. package/dist/types/server/context.d.ts +90 -6
  79. package/dist/types/server/handle-store.d.ts +17 -6
  80. package/dist/types/server/request-context.d.ts +45 -7
  81. package/dist/types/server/request-headers.d.ts +12 -0
  82. package/dist/types/server/rewrite-trail.d.ts +8 -1
  83. package/dist/types/testing/build-versions.d.ts +0 -3
  84. package/dist/types/testing/e2e/index.d.ts +1 -1
  85. package/dist/types/testing/flight.entry.d.ts +1 -1
  86. package/dist/types/testing/index.d.ts +1 -1
  87. package/dist/types/testing/internal/client-urls-projection.d.ts +4 -3
  88. package/dist/types/testing/internal/seed-vars.d.ts +4 -4
  89. package/dist/types/testing/render-route.d.ts +21 -11
  90. package/dist/types/testing/run-loader.d.ts +7 -0
  91. package/dist/types/testing/run-middleware.d.ts +7 -0
  92. package/dist/types/testing/run-revalidate.d.ts +5 -4
  93. package/dist/types/testing/serve-shell-request.d.ts +42 -5
  94. package/dist/types/testing/shell-status.d.ts +17 -0
  95. package/dist/types/testing/vitest-stubs/version.d.ts +1 -0
  96. package/dist/types/testing/vitest.d.ts +10 -1
  97. package/dist/types/thenable-status.d.ts +0 -6
  98. package/dist/types/types/handler-context.d.ts +12 -1
  99. package/dist/types/types/loader-types.d.ts +10 -4
  100. package/dist/types/types/route-entry.d.ts +2 -1
  101. package/dist/types/urls/path-helper-types.d.ts +4 -2
  102. package/dist/types/vite/discovery/client-urls-projection.d.ts +55 -5
  103. package/dist/types/vite/discovery/state.d.ts +20 -0
  104. package/dist/types/vite/plugins/client-urls-revalidate-hoist.d.ts +28 -16
  105. package/dist/types/vite/plugins/hoist/source.d.ts +8 -7
  106. package/dist/types/vite/plugins/virtual-entries.d.ts +6 -0
  107. package/dist/types/vite/utils/prerender-utils.d.ts +7 -0
  108. package/dist/vite/index.js +6819 -6555
  109. package/package.json +1 -1
  110. package/skills/app-structure/SKILL.md +255 -0
  111. package/skills/app-structure/content-site.md +333 -0
  112. package/skills/app-structure/e-commerce.md +435 -0
  113. package/skills/app-structure/large-app.md +885 -0
  114. package/skills/app-structure/multi-app.md +280 -0
  115. package/skills/breadcrumbs/SKILL.md +6 -1
  116. package/skills/cache-guide/SKILL.md +4 -8
  117. package/skills/caching/SKILL.md +6 -6
  118. package/skills/catalog.json +12 -0
  119. package/skills/client-urls/SKILL.md +185 -32
  120. package/skills/comparison/references/framework-comparison.md +1 -1
  121. package/skills/composability/SKILL.md +10 -8
  122. package/skills/handler-use/SKILL.md +48 -9
  123. package/skills/hooks/handle-and-actions.md +104 -0
  124. package/skills/hooks/navigation.md +24 -10
  125. package/skills/hooks/urls.md +1 -1
  126. package/skills/host-router/SKILL.md +1 -1
  127. package/skills/i18n/SKILL.md +2 -1
  128. package/skills/intercept/SKILL.md +60 -29
  129. package/skills/layout/SKILL.md +6 -3
  130. package/skills/links/SKILL.md +151 -17
  131. package/skills/loader/SKILL.md +42 -19
  132. package/skills/middleware/SKILL.md +111 -15
  133. package/skills/observability/SKILL.md +11 -0
  134. package/skills/parallel/SKILL.md +5 -5
  135. package/skills/ppr/SKILL.md +103 -52
  136. package/skills/prerender/SKILL.md +4 -5
  137. package/skills/rango/SKILL.md +3 -0
  138. package/skills/response-routes/SKILL.md +4 -1
  139. package/skills/router-setup/SKILL.md +0 -2
  140. package/skills/scripts/SKILL.md +12 -1
  141. package/skills/server-actions/SKILL.md +60 -20
  142. package/skills/testing/SKILL.md +1 -0
  143. package/skills/testing/client-components.md +13 -10
  144. package/skills/testing/loader.md +25 -24
  145. package/skills/testing/middleware.md +11 -11
  146. package/skills/testing/render-handler.md +17 -17
  147. package/skills/testing/response-routes.md +4 -1
  148. package/skills/testing/serve-shell-request.md +86 -22
  149. package/skills/testing/server-actions.md +16 -16
  150. package/skills/ux-architecture/SKILL.md +980 -0
  151. package/skills/vercel/SKILL.md +1 -1
  152. package/skills/view-transitions/SKILL.md +43 -10
  153. package/src/__augment-tests__/augmented.check.ts +28 -0
  154. package/src/browser/event-controller.ts +44 -18
  155. package/src/browser/intercept-utils.ts +3 -2
  156. package/src/browser/merge-segment-loaders.ts +0 -85
  157. package/src/browser/navigation-bridge.ts +112 -84
  158. package/src/browser/navigation-client.ts +20 -15
  159. package/src/browser/navigation-store.ts +4 -2
  160. package/src/browser/navigation-transaction.ts +9 -8
  161. package/src/browser/partial-update.ts +90 -32
  162. package/src/browser/prefetch/cache.ts +8 -0
  163. package/src/browser/prefetch/fetch.ts +13 -3
  164. package/src/browser/rango-marker.ts +13 -0
  165. package/src/browser/react/NavigationProvider.tsx +25 -7
  166. package/src/browser/react/filter-segment-order.ts +13 -0
  167. package/src/browser/react/use-action.ts +9 -15
  168. package/src/browser/react/use-handle.ts +63 -71
  169. package/src/browser/react/use-href.tsx +24 -5
  170. package/src/browser/react/use-link-status.ts +1 -1
  171. package/src/browser/react/use-navigation.ts +30 -40
  172. package/src/browser/react/use-router.ts +37 -6
  173. package/src/browser/react/use-segments.ts +8 -12
  174. package/src/browser/response-adapter.ts +11 -5
  175. package/src/browser/rsc-router.tsx +3 -1
  176. package/src/browser/scroll-restoration.ts +34 -20
  177. package/src/browser/segment-reconciler.ts +11 -29
  178. package/src/browser/server-action-bridge.ts +52 -16
  179. package/src/browser/types.ts +40 -8
  180. package/src/build/generate-manifest.ts +12 -1
  181. package/src/build/route-types/codegen.ts +6 -2
  182. package/src/build/route-types/per-module-writer.ts +94 -61
  183. package/src/cache/cache-runtime.ts +9 -0
  184. package/src/cache/cache-scope.ts +2 -1
  185. package/src/cache/cf/cf-cache-store.ts +6 -7
  186. package/src/cache/cf/cf-cache-types.ts +0 -17
  187. package/src/cache/cf/cf-shell-frame.ts +3 -0
  188. package/src/cache/handle-snapshot.ts +2 -2
  189. package/src/cache/types.ts +16 -6
  190. package/src/cache/vercel/vercel-cache-store.ts +8 -19
  191. package/src/client-urls/client-urls.ts +202 -153
  192. package/src/client-urls/revalidate-registry.ts +12 -3
  193. package/src/client-urls/server-projection.ts +137 -66
  194. package/src/client-urls/types.ts +30 -15
  195. package/src/client.rsc.tsx +11 -1
  196. package/src/client.tsx +9 -1
  197. package/src/decode-loader-results.ts +64 -31
  198. package/src/handles/deferred-resolution.ts +3 -2
  199. package/src/handles/meta.ts +5 -0
  200. package/src/href-client.ts +3 -1
  201. package/src/index.rsc.ts +1 -0
  202. package/src/index.ts +12 -4
  203. package/src/loader-redirect.tsx +2 -3
  204. package/src/outlet-context.ts +7 -6
  205. package/src/prefetch-markers.ts +15 -5
  206. package/src/prerender/build-shell-capture.ts +1 -2
  207. package/src/prerender/writable-store.ts +2 -3
  208. package/src/reverse.ts +3 -3
  209. package/src/route-content-wrapper.tsx +19 -17
  210. package/src/route-definition/dsl-helpers.ts +41 -21
  211. package/src/route-definition/helpers-types.ts +7 -2
  212. package/src/route-definition/resolve-handler-use.ts +23 -10
  213. package/src/route-definition/rewrite.ts +86 -19
  214. package/src/route-types.ts +1 -2
  215. package/src/router/find-match.ts +37 -1
  216. package/src/router/handler-context.ts +85 -40
  217. package/src/router/intercept-resolution.ts +161 -307
  218. package/src/router/lazy-includes.ts +43 -27
  219. package/src/router/loader-resolution.ts +21 -1
  220. package/src/router/manifest.ts +15 -10
  221. package/src/router/match-api.ts +11 -1
  222. package/src/router/match-middleware/background-revalidation.ts +2 -4
  223. package/src/router/match-middleware/cache-lookup.ts +16 -20
  224. package/src/router/match-middleware/intercept-resolution.ts +41 -60
  225. package/src/router/match-result.ts +31 -2
  226. package/src/router/middleware.ts +6 -147
  227. package/src/router/navigation-snapshot.ts +38 -2
  228. package/src/router/prefetch-cache-ttl.ts +10 -18
  229. package/src/router/prerender-match.ts +2 -1
  230. package/src/router/revalidation.ts +24 -3
  231. package/src/router/router-context.ts +5 -18
  232. package/src/router/router-interfaces.ts +0 -13
  233. package/src/router/router-options.ts +4 -28
  234. package/src/router/segment-resolution/fresh.ts +6 -2
  235. package/src/router/segment-resolution/helpers.ts +7 -1
  236. package/src/router/segment-resolution/loader-cache.ts +4 -3
  237. package/src/router/segment-resolution/mask-nested.ts +5 -3
  238. package/src/router/segment-resolution/prefetch-deferral.ts +62 -12
  239. package/src/router/segment-resolution/revalidation.ts +75 -35
  240. package/src/router/segment-resolution.ts +0 -1
  241. package/src/router/segment-wrappers.ts +8 -13
  242. package/src/router/substitute-pattern-params.ts +3 -2
  243. package/src/router/tracing.ts +2 -2
  244. package/src/router-versions.ts +7 -7
  245. package/src/router.ts +5 -17
  246. package/src/rsc/full-payload.ts +26 -4
  247. package/src/rsc/handler.ts +86 -48
  248. package/src/rsc/helpers.ts +13 -0
  249. package/src/rsc/manifest-init.ts +56 -3
  250. package/src/rsc/prefetch-render-error.ts +38 -0
  251. package/src/rsc/progressive-enhancement.ts +161 -211
  252. package/src/rsc/rewrite-passes.ts +56 -9
  253. package/src/rsc/rsc-rendering.ts +79 -23
  254. package/src/rsc/server-action.ts +25 -2
  255. package/src/rsc/shell-capture-constants.ts +26 -3
  256. package/src/rsc/shell-capture.ts +137 -51
  257. package/src/rsc/types.ts +5 -9
  258. package/src/segment-system.tsx +125 -119
  259. package/src/server/build-version-table.ts +12 -24
  260. package/src/server/context.ts +160 -7
  261. package/src/server/handle-store.ts +80 -12
  262. package/src/server/request-context.ts +46 -6
  263. package/src/server/request-headers.ts +32 -0
  264. package/src/server/rewrite-trail.ts +55 -9
  265. package/src/ssr/ssr-root.tsx +6 -10
  266. package/src/testing/build-versions.ts +0 -3
  267. package/src/testing/dispatch.ts +5 -8
  268. package/src/testing/e2e/index.ts +1 -0
  269. package/src/testing/flight.entry.ts +1 -0
  270. package/src/testing/index.ts +1 -0
  271. package/src/testing/internal/client-urls-projection.ts +31 -11
  272. package/src/testing/internal/seed-vars.ts +4 -4
  273. package/src/testing/render-route.tsx +83 -40
  274. package/src/testing/run-loader.ts +26 -2
  275. package/src/testing/run-middleware.ts +10 -0
  276. package/src/testing/run-revalidate.ts +13 -6
  277. package/src/testing/serve-shell-request.ts +79 -9
  278. package/src/testing/shell-status.ts +40 -0
  279. package/src/testing/vitest-stubs/version.ts +1 -0
  280. package/src/testing/vitest.ts +74 -7
  281. package/src/thenable-status.ts +0 -11
  282. package/src/types/handler-context.ts +12 -1
  283. package/src/types/loader-types.ts +10 -4
  284. package/src/types/route-entry.ts +2 -1
  285. package/src/urls/include-helper.ts +38 -11
  286. package/src/urls/path-helper-types.ts +4 -2
  287. package/src/vite/discovery/client-urls-projection.ts +205 -16
  288. package/src/vite/discovery/discover-routers.ts +2 -2
  289. package/src/vite/discovery/prerender-collection.ts +2 -6
  290. package/src/vite/discovery/shell-prerender-phase.ts +1 -5
  291. package/src/vite/discovery/state.ts +26 -0
  292. package/src/vite/plugins/client-urls-revalidate-hoist.ts +190 -77
  293. package/src/vite/plugins/expose-internal-ids.ts +46 -40
  294. package/src/vite/plugins/hoist/source.ts +8 -7
  295. package/src/vite/plugins/transition-when-hoist.ts +1 -0
  296. package/src/vite/plugins/version-plugin.ts +7 -0
  297. package/src/vite/plugins/version.d.ts +7 -0
  298. package/src/vite/plugins/virtual-entries.ts +20 -0
  299. package/src/vite/rango.ts +4 -1
  300. package/src/vite/router-discovery.ts +39 -7
  301. package/src/vite/utils/prerender-utils.ts +17 -0
@@ -27,11 +27,10 @@ export type RewriteTarget =
27
27
 
28
28
  const REWRITE_RULE =
29
29
  "rewrite() is for middleware: throw it from router.use() or route " +
30
- "middleware (middleware() in urls()), before await next(), while it " +
31
- "handles a GET or HEAD page request. Loaders and route handlers can't " +
32
- "rewrite: throw rewrite() from the route's middleware instead. Intercept " +
33
- "middleware and loader fetches can't either. In a server action use " +
34
- "redirect(); for a mutation return a Response from middleware.";
30
+ "middleware (middleware() in urls()), before await next(). Loaders and " +
31
+ "route handlers can't rewrite: throw rewrite() from the route's " +
32
+ "middleware instead. Middleware in loader fetches can't either. In a " +
33
+ "server action use redirect().";
35
34
 
36
35
  export function describeRewriteTarget(target: RewriteTarget): string {
37
36
  return "path" in target
@@ -39,6 +38,17 @@ export function describeRewriteTarget(target: RewriteTarget): string {
39
38
  : `rewrite(${JSON.stringify(target.name)})`;
40
39
  }
41
40
 
41
+ /**
42
+ * Which PPR shell a rewritten request reads and captures
43
+ * (`rewrite(...).shell()`); without a call, none.
44
+ * - `"visitor"`: its own, keyed by the visitor's URL and the target, captured
45
+ * with the visitor's location and `ctx.rewrittenFrom`.
46
+ * - `"target"`: the target's shared shell. `usePathname()` reads the target's
47
+ * path on a HIT, and values set before the throw are baked in: for a page
48
+ * that renders neither (a lock or promo page).
49
+ */
50
+ export type RewriteShell = "visitor" | "target";
51
+
42
52
  /**
43
53
  * The value `rewrite()` returns, thrown by middleware. An Error, so a throw
44
54
  * where a rewrite is not supported fails loudly with the rule in its message.
@@ -46,11 +56,60 @@ export function describeRewriteTarget(target: RewriteTarget): string {
46
56
  export class Rewrite extends Error {
47
57
  name = "Rewrite" as const;
48
58
  readonly target: RewriteTarget;
59
+ /** The `shell()` choice; undefined: no shell. */
60
+ readonly shellAudience: RewriteShell | undefined;
61
+ /** `disallowMutations()`: a mutation is refused, not moved, before its action runs. */
62
+ readonly mutationsDisallowed: boolean;
49
63
 
50
- constructor(target: RewriteTarget) {
64
+ constructor(
65
+ target: RewriteTarget,
66
+ shellAudience?: RewriteShell,
67
+ mutationsDisallowed = false,
68
+ ) {
51
69
  super(`${describeRewriteTarget(target)}: ${REWRITE_RULE}`);
52
70
  Object.setPrototypeOf(this, Rewrite.prototype);
53
71
  this.target = target;
72
+ this.shellAudience = shellAudience;
73
+ this.mutationsDisallowed = mutationsDisallowed;
74
+ }
75
+
76
+ /**
77
+ * A copy that picks the PPR shell of a `ppr` target ({@link RewriteShell}),
78
+ * so a rewrite kept in a module constant keeps its own choice:
79
+ *
80
+ * ```ts
81
+ * throw rewrite("item", { id }).shell("visitor");
82
+ * ```
83
+ */
84
+ shell(audience: RewriteShell): Rewrite {
85
+ if (audience !== "visitor" && audience !== "target") {
86
+ throw new TypeError(
87
+ `${describeRewriteTarget(this.target)}.shell() takes "visitor" or "target", not ${JSON.stringify(audience)}.`,
88
+ );
89
+ }
90
+ return new Rewrite(this.target, audience, this.mutationsDisallowed);
91
+ }
92
+
93
+ /**
94
+ * A copy for a lock: global middleware's rewrite of a mutation (a server
95
+ * action, a form post, any non-GET request) is an error before the action
96
+ * runs, instead of moving the mutation to the target. GET and HEAD rewrite
97
+ * as usual.
98
+ *
99
+ * ```ts
100
+ * throw rewrite("maintenance").disallowMutations();
101
+ * ```
102
+ *
103
+ * Route middleware runs after the action, so there it cannot refuse it: the
104
+ * render moves to the target, dev warns, and production logs one error.
105
+ */
106
+ disallowMutations(): Rewrite {
107
+ if (arguments.length > 0) {
108
+ throw new TypeError(
109
+ `${describeRewriteTarget(this.target)}.disallowMutations() takes no arguments.`,
110
+ );
111
+ }
112
+ return new Rewrite(this.target, this.shellAudience, true);
54
113
  }
55
114
  }
56
115
  Object.defineProperty(Rewrite.prototype, REWRITE_BRAND, { value: true });
@@ -107,31 +166,39 @@ function createRewrite(
107
166
  * ```ts
108
167
  * router.use(async (ctx, next) => {
109
168
  * if (!locked || ctx.pathname === "/maintenance") return next();
110
- * throw rewrite("maintenance");
169
+ * throw rewrite("maintenance").disallowMutations();
111
170
  * });
112
171
  * ```
113
172
  *
114
173
  * The request runs again as the target (global middleware, route
115
174
  * middleware, render) with the same request context: values set before the
116
175
  * throw stay readable. `ctx.url` is the target; `ctx.rewrittenFrom` is the
117
- * visitor's URL. See the `/middleware` skill.
176
+ * visitor's URL. A mutation that global middleware rewrites moves to the
177
+ * target before its action runs, so the page's forms keep working; a lock
178
+ * refuses it with `.disallowMutations()`. A `ppr` target gets no shell unless
179
+ * `.shell()` picks one ({@link RewriteShell}). See the `/middleware` skill.
118
180
  */
119
181
  export const rewrite: RewriteFunction = createRewrite as RewriteFunction;
120
182
 
121
- /** A dispatch site that cannot honor a rewrite. */
122
- export type RewriteSite = "mutation" | "loader-fetch" | "intercept";
183
+ /**
184
+ * A dispatch site that cannot honor a rewrite. "mutation" is the refusal
185
+ * `.disallowMutations()` asked for, not a misuse.
186
+ */
187
+ export type RewriteSite = "mutation" | "body-read" | "loader-fetch";
123
188
 
124
189
  const SITE_MESSAGE: Record<RewriteSite, string> = {
125
190
  mutation:
126
- "middleware threw it while handling a non-GET request (a server action " +
127
- "or a form post). Return a Response for mutations instead, e.g. " +
128
- 'new Response("Maintenance", { status: 503 }).',
191
+ "refused a mutation: .disallowMutations() was set, so a server action " +
192
+ "or form post (any request but GET and HEAD) fails before its action " +
193
+ "runs. To answer it instead, return a Response for mutations from " +
194
+ 'middleware, e.g. new Response("Maintenance", { status: 503 }).',
195
+ "body-read":
196
+ "is not supported here: middleware read the request body before it " +
197
+ "threw it, and the request moves to the target with its body for the " +
198
+ "action to read. Read a copy instead: ctx.request.clone().formData().",
129
199
  "loader-fetch":
130
- "middleware threw it during a loader fetch (useFetchLoader, load(), " +
131
- "useRefreshLoaders). Return a Response there.",
132
- intercept:
133
- "intercept middleware threw it. Rewrite from router.use() or the " +
134
- "route's middleware(), or return a Response.",
200
+ "is not supported here: middleware threw it during a loader fetch " +
201
+ "(useFetchLoader, load(), useRefreshLoaders). Return a Response there.",
135
202
  };
136
203
 
137
204
  /** The error a rewrite becomes at a site that cannot honor it. */
@@ -140,7 +207,7 @@ export function rewriteNotSupported(
140
207
  site: RewriteSite,
141
208
  ): Error {
142
209
  return new Error(
143
- `${describeRewriteTarget(rewrite.target)} is not supported here: ${SITE_MESSAGE[site]}`,
210
+ `${describeRewriteTarget(rewrite.target)} ${SITE_MESSAGE[site]}`,
144
211
  { cause: rewrite },
145
212
  );
146
213
  }
@@ -160,7 +160,7 @@ export type IncludeItem = {
160
160
  * include cannot collide with siblings declared outside it.
161
161
  */
162
162
  includeScope?: string;
163
- };
163
+ } & import("./server/context.js").IncludeScopePosition;
164
164
  };
165
165
 
166
166
  /**
@@ -221,7 +221,6 @@ export type ParallelUseItem =
221
221
  | NotFoundBoundaryItem
222
222
  | TransitionItem;
223
223
  export type InterceptUseItem =
224
- | MiddlewareItem
225
224
  | LoaderItem
226
225
  | LoadingItem
227
226
  | LayoutItem
@@ -21,6 +21,30 @@ function cloneMatchResult<TEnv>(
21
21
  return r ? { ...r, params: { ...r.params } } : null;
22
22
  }
23
23
 
24
+ function describeUnownedTrieRoute(
25
+ pathname: string,
26
+ routeKey: string,
27
+ sp: string,
28
+ entries: RouteEntry[],
29
+ ): string {
30
+ const includes = entries
31
+ .filter((e) => e.staticPrefix === sp)
32
+ .map((e) => {
33
+ const prefix = (e as { _lazyPrefix?: string })._lazyPrefix;
34
+ if (prefix === undefined) return "root routes";
35
+ const names = e.lazyContext?.namePrefix;
36
+ return `include("${prefix}")${names ? ` naming "${names}.*"` : ""}`;
37
+ });
38
+ return (
39
+ `[@rangojs/router] The route trie matched "${pathname}" to route ` +
40
+ `"${routeKey}" under static prefix "${sp}", but no include there ` +
41
+ `registered that name at runtime (checked: ${includes.join(", ")}). ` +
42
+ `Build discovery and the runtime named this route differently, so it ` +
43
+ `cannot render. If this follows a route edit, reload the page; if it ` +
44
+ `persists, please report it with your route configuration.`
45
+ );
46
+ }
47
+
24
48
  export interface FindMatchDeps<TEnv = any> {
25
49
  routesEntries: RouteEntry<TEnv>[];
26
50
  // Returns a Promise only when it had to resolve an async include provider
@@ -100,7 +124,7 @@ export function createFindMatch<TEnv = any>(
100
124
  // this loop may evaluate a candidate that is NOT the route's owner.
101
125
  // Remember the failure and keep scanning: if another candidate owns
102
126
  // the route, this sibling's failure is correctly isolated (its
103
- // _lazyInflight was cleared, so a later request retries). But if NO
127
+ // in-flight import was cleared, so a later request retries). But if NO
104
128
  // candidate ends up owning it, the failing one WAS the owner — a
105
129
  // real, trie-matched route whose module failed to import — and we
106
130
  // rethrow below as a 5xx rather than silently 404ing (see finding 3:
@@ -130,6 +154,18 @@ export function createFindMatch<TEnv = any>(
130
154
  throw candidateError;
131
155
  }
132
156
 
157
+ // No candidate owns the trie's key (#1104): dev throws, production keeps the 404 fallback.
158
+ if (!entry && fallbackEntry && process.env.NODE_ENV !== "production") {
159
+ throw new Error(
160
+ describeUnownedTrieRoute(
161
+ pathname,
162
+ trieResult.routeKey,
163
+ trieResult.sp,
164
+ deps.routesEntries,
165
+ ),
166
+ );
167
+ }
168
+
133
169
  if (!entry) entry = fallbackEntry;
134
170
 
135
171
  if (!entry) {
@@ -20,6 +20,9 @@ import {
20
20
  import {
21
21
  assertCachedHeaderWriteAllowed,
22
22
  assertNonCacheableReadAllowed,
23
+ scopeReverse,
24
+ type ReverseScope,
25
+ type ScopableReverse,
23
26
  } from "../server/context.js";
24
27
  import {
25
28
  guardRequestHeaders,
@@ -117,22 +120,8 @@ function resolveRouteName(
117
120
  ? currentRoutePrefix.substring(0, lastDot)
118
121
  : currentRoutePrefix;
119
122
 
120
- // Try prefixed name at current level
121
- const prefixedName = `${prefix}.${lookupName}`;
122
- if (routeMap[prefixedName] !== undefined) {
123
- return routeMap[prefixedName];
124
- }
125
-
126
- // Walk up parent prefixes for nested includes
127
- let currentPrefix = prefix;
128
- while (currentPrefix.includes(".")) {
129
- const parentDot = currentPrefix.lastIndexOf(".");
130
- currentPrefix = currentPrefix.substring(0, parentDot);
131
- const parentPrefixedName = `${currentPrefix}.${lookupName}`;
132
- if (routeMap[parentPrefixedName] !== undefined) {
133
- return routeMap[parentPrefixedName];
134
- }
135
- }
123
+ const scoped = resolveInPrefix(lookupName, routeMap, prefix);
124
+ if (scoped !== undefined) return scoped;
136
125
 
137
126
  // Fallback: try bare name at root scope only.
138
127
  // Routes inside { name: "" } mounts are at root scope — their dot-local
@@ -159,6 +148,43 @@ function resolveRouteName(
159
148
  return routeMap[name];
160
149
  }
161
150
 
151
+ /**
152
+ * `${prefix}.${lookupName}`, then the same name under each enclosing include
153
+ * prefix (nested includes reach their parents' routes).
154
+ */
155
+ function resolveInPrefix(
156
+ lookupName: string,
157
+ routeMap: Record<string, string>,
158
+ prefix: string,
159
+ ): string | undefined {
160
+ let currentPrefix = prefix;
161
+ for (;;) {
162
+ const pattern = routeMap[`${currentPrefix}.${lookupName}`];
163
+ if (pattern !== undefined) return pattern;
164
+ const parentDot = currentPrefix.lastIndexOf(".");
165
+ if (parentDot === -1) return undefined;
166
+ currentPrefix = currentPrefix.substring(0, parentDot);
167
+ }
168
+ }
169
+
170
+ /**
171
+ * Dot-local name resolved from a segment's own include scope (ReverseScope,
172
+ * server/context.ts) instead of the matched route's name: a layout, parallel
173
+ * slot or their loader in a parent module links within that module on every
174
+ * route below it.
175
+ */
176
+ function resolveScopedName(
177
+ lookupName: string,
178
+ routeMap: Record<string, string>,
179
+ scope: ReverseScope,
180
+ ): string | undefined {
181
+ if (scope.prefix) {
182
+ const scoped = resolveInPrefix(lookupName, routeMap, scope.prefix);
183
+ if (scoped !== undefined) return scoped;
184
+ }
185
+ return scope.rootScoped ? routeMap[lookupName] : undefined;
186
+ }
187
+
162
188
  function createPrerenderPassthroughFn(
163
189
  build: boolean,
164
190
  isPassthroughRoute: boolean,
@@ -180,6 +206,26 @@ function createPrerenderPassthroughFn(
180
206
  };
181
207
  }
182
208
 
209
+ /**
210
+ * The ctx a layout's or parallel slot's handler gets: a prototype child of
211
+ * the request's one handler ctx with its own `reverse` in the segment's
212
+ * ReverseScope, so `.name` resolves in the module that declared the segment.
213
+ * The scope rides the object, not the call frame: it holds after an `await`
214
+ * and in a component the ctx is handed to. Every other field reads through
215
+ * live (`_currentSegmentId` as resolution moves it, ctx.set values).
216
+ * `_reverseScope` keys a "use cache" call that takes this ctx (cache-runtime.ts).
217
+ */
218
+ export function withReverseScope<C>(
219
+ ctx: C,
220
+ scope: ReverseScope | undefined,
221
+ ): C {
222
+ if (scope === undefined) return ctx;
223
+ const scoped = Object.create(ctx as object);
224
+ scoped.reverse = scopeReverse((ctx as any).reverse, scope);
225
+ scoped._reverseScope = scope;
226
+ return scoped;
227
+ }
228
+
183
229
  /**
184
230
  * Create a reverse function for URL generation from route names.
185
231
  * Used by both HandlerContext and MiddlewareContext.
@@ -188,41 +234,40 @@ function createPrerenderPassthroughFn(
188
234
  * generation. This enables auto-filling mount params from include() prefixes:
189
235
  * inner handlers can call ctx.reverse(".sibling") without explicitly passing
190
236
  * params that are already known from the current URL match.
191
- * Explicitly passed hrefParams take priority over currentParams.
237
+ * Explicitly passed hrefParams take priority over currentParams. A required
238
+ * param that neither supplies throws `Missing param`.
192
239
  */
193
240
  export function createReverseFunction(
194
241
  routeMap: Record<string, string>,
195
242
  currentRoutePrefix?: string,
196
243
  currentParams?: Record<string, string>,
197
244
  rootScoped?: boolean,
198
- ): (
199
- name: string,
200
- hrefParams?: Record<string, string>,
201
- search?: Record<string, unknown>,
202
- ) => string {
203
- return (name, hrefParams, search) => {
204
- // Resolve route name with namespace support
205
- const pattern = resolveRouteName(
206
- name,
207
- routeMap,
208
- currentRoutePrefix,
209
- rootScoped,
210
- );
245
+ ): ScopableReverse {
246
+ return (name, hrefParams, search, segmentScope) => {
247
+ // A dot-local name from a segment's handler or loader resolves in the
248
+ // segment's own include scope (withReverseScope); everything else from
249
+ // the route's name.
250
+ const scope = name.startsWith(".") ? segmentScope : undefined;
251
+ const pattern = scope
252
+ ? resolveScopedName(name.slice(1), routeMap, scope)
253
+ : resolveRouteName(name, routeMap, currentRoutePrefix, rootScoped);
211
254
 
212
255
  if (pattern === undefined) {
213
- throw new Error(
214
- `Unknown route: "${name}"${currentRoutePrefix ? ` (current route: ${currentRoutePrefix})` : ""}`,
215
- );
256
+ const where = scope
257
+ ? ` (segment scope: ${scope.prefix ?? "root"})`
258
+ : currentRoutePrefix
259
+ ? ` (current route: ${currentRoutePrefix})`
260
+ : "";
261
+ throw new Error(`Unknown route: "${name}"${where}`);
216
262
  }
217
263
 
218
- // Merge current request params as defaults, explicit params override
219
- const effectiveParams = currentParams
220
- ? { ...currentParams, ...hrefParams }
221
- : hrefParams;
264
+ // Merge current request params as defaults, explicit params override.
265
+ // Always substitute, even with no params at all (middleware and
266
+ // response-route reverse pass neither): a route with a required param
267
+ // must throw `Missing param`, never return its raw `:param` pattern.
268
+ const effectiveParams = { ...currentParams, ...hrefParams };
222
269
 
223
- let result = effectiveParams
224
- ? substitutePatternParams(pattern, effectiveParams, name)
225
- : pattern;
270
+ let result = substitutePatternParams(pattern, effectiveParams, name);
226
271
 
227
272
  // Append search params as query string
228
273
  if (search) {