@rangojs/router 0.0.0-experimental.b9cb8739 → 0.0.0-experimental.bd6e11bc

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 (285) hide show
  1. package/README.md +196 -43
  2. package/dist/bin/rango.js +277 -99
  3. package/dist/testing/vitest.js +48 -0
  4. package/dist/vite/index.js +2779 -1064
  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 +57 -11
  8. package/skills/breadcrumbs/SKILL.md +3 -1
  9. package/skills/bundle-analysis/SKILL.md +159 -0
  10. package/skills/cache-guide/SKILL.md +243 -21
  11. package/skills/caching/SKILL.md +155 -6
  12. package/skills/composability/SKILL.md +27 -2
  13. package/skills/document-cache/SKILL.md +78 -55
  14. package/skills/handler-use/SKILL.md +364 -0
  15. package/skills/hooks/SKILL.md +229 -20
  16. package/skills/host-router/SKILL.md +45 -20
  17. package/skills/i18n/SKILL.md +276 -0
  18. package/skills/intercept/SKILL.md +46 -4
  19. package/skills/layout/SKILL.md +28 -7
  20. package/skills/links/SKILL.md +249 -17
  21. package/skills/loader/SKILL.md +273 -53
  22. package/skills/middleware/SKILL.md +49 -12
  23. package/skills/migrate-nextjs/SKILL.md +562 -0
  24. package/skills/migrate-react-router/SKILL.md +769 -0
  25. package/skills/mime-routes/SKILL.md +27 -0
  26. package/skills/observability/SKILL.md +137 -0
  27. package/skills/parallel/SKILL.md +197 -6
  28. package/skills/prerender/SKILL.md +123 -100
  29. package/skills/rango/SKILL.md +242 -22
  30. package/skills/react-compiler/SKILL.md +168 -0
  31. package/skills/response-routes/SKILL.md +66 -9
  32. package/skills/route/SKILL.md +88 -4
  33. package/skills/router-setup/SKILL.md +90 -5
  34. package/skills/server-actions/SKILL.md +751 -0
  35. package/skills/streams-and-websockets/SKILL.md +283 -0
  36. package/skills/testing/SKILL.md +716 -0
  37. package/skills/typesafety/SKILL.md +329 -27
  38. package/skills/use-cache/SKILL.md +34 -5
  39. package/skills/view-transitions/SKILL.md +294 -0
  40. package/src/__augment-tests__/augment.ts +81 -0
  41. package/src/__augment-tests__/augmented.check.ts +117 -0
  42. package/src/__internal.ts +1 -1
  43. package/src/browser/action-coordinator.ts +53 -36
  44. package/src/browser/app-shell.ts +52 -0
  45. package/src/browser/app-version.ts +14 -0
  46. package/src/browser/event-controller.ts +91 -70
  47. package/src/browser/history-state.ts +21 -0
  48. package/src/browser/index.ts +3 -3
  49. package/src/browser/navigation-bridge.ts +102 -16
  50. package/src/browser/navigation-client.ts +164 -59
  51. package/src/browser/navigation-store.ts +75 -17
  52. package/src/browser/navigation-transaction.ts +21 -37
  53. package/src/browser/partial-update.ts +139 -38
  54. package/src/browser/prefetch/cache.ts +175 -15
  55. package/src/browser/prefetch/fetch.ts +180 -33
  56. package/src/browser/prefetch/queue.ts +123 -20
  57. package/src/browser/prefetch/resource-ready.ts +77 -0
  58. package/src/browser/rango-state.ts +53 -13
  59. package/src/browser/react/Link.tsx +81 -9
  60. package/src/browser/react/NavigationProvider.tsx +110 -33
  61. package/src/browser/react/context.ts +7 -2
  62. package/src/browser/react/filter-segment-order.ts +51 -7
  63. package/src/browser/react/index.ts +3 -0
  64. package/src/browser/react/location-state-shared.ts +175 -4
  65. package/src/browser/react/location-state.ts +39 -13
  66. package/src/browser/react/use-handle.ts +23 -64
  67. package/src/browser/react/use-navigation.ts +22 -2
  68. package/src/browser/react/use-params.ts +20 -8
  69. package/src/browser/react/use-reverse.ts +106 -0
  70. package/src/browser/react/use-router.ts +43 -10
  71. package/src/browser/react/use-segments.ts +11 -8
  72. package/src/browser/response-adapter.ts +25 -0
  73. package/src/browser/rsc-router.tsx +191 -74
  74. package/src/browser/scroll-restoration.ts +41 -14
  75. package/src/browser/segment-reconciler.ts +36 -9
  76. package/src/browser/segment-structure-assert.ts +2 -2
  77. package/src/browser/server-action-bridge.ts +31 -36
  78. package/src/browser/types.ts +57 -5
  79. package/src/build/collect-fallback-refs.ts +107 -0
  80. package/src/build/generate-manifest.ts +65 -40
  81. package/src/build/generate-route-types.ts +5 -0
  82. package/src/build/index.ts +2 -0
  83. package/src/build/route-trie.ts +52 -25
  84. package/src/build/route-types/codegen.ts +4 -4
  85. package/src/build/route-types/include-resolution.ts +9 -2
  86. package/src/build/route-types/per-module-writer.ts +7 -4
  87. package/src/build/route-types/router-processing.ts +278 -88
  88. package/src/build/route-types/scan-filter.ts +9 -2
  89. package/src/build/route-types/source-scan.ts +118 -0
  90. package/src/build/runtime-discovery.ts +9 -20
  91. package/src/cache/cache-runtime.ts +15 -11
  92. package/src/cache/cache-scope.ts +76 -49
  93. package/src/cache/cf/cf-cache-store.ts +501 -18
  94. package/src/cache/cf/index.ts +5 -1
  95. package/src/cache/document-cache.ts +17 -7
  96. package/src/cache/index.ts +1 -0
  97. package/src/cache/taint.ts +55 -0
  98. package/src/client.rsc.tsx +3 -0
  99. package/src/client.tsx +94 -238
  100. package/src/context-var.ts +72 -2
  101. package/src/debug.ts +2 -2
  102. package/src/decode-loader-results.ts +36 -0
  103. package/src/errors.ts +30 -1
  104. package/src/handle.ts +65 -12
  105. package/src/host/index.ts +2 -2
  106. package/src/host/router.ts +129 -57
  107. package/src/host/types.ts +31 -2
  108. package/src/host/utils.ts +1 -1
  109. package/src/href-client.ts +140 -20
  110. package/src/index.rsc.ts +12 -5
  111. package/src/index.ts +61 -11
  112. package/src/loader-store.ts +500 -0
  113. package/src/loader.rsc.ts +2 -5
  114. package/src/loader.ts +3 -10
  115. package/src/missing-id-error.ts +68 -0
  116. package/src/outlet-context.ts +1 -1
  117. package/src/prerender/store.ts +5 -4
  118. package/src/prerender.ts +141 -80
  119. package/src/response-utils.ts +37 -0
  120. package/src/reverse.ts +65 -15
  121. package/src/route-content-wrapper.tsx +6 -28
  122. package/src/route-definition/dsl-helpers.ts +435 -260
  123. package/src/route-definition/helper-factories.ts +29 -139
  124. package/src/route-definition/helpers-types.ts +110 -34
  125. package/src/route-definition/index.ts +3 -0
  126. package/src/route-definition/redirect.ts +11 -3
  127. package/src/route-definition/resolve-handler-use.ts +155 -0
  128. package/src/route-definition/use-item-types.ts +32 -0
  129. package/src/route-map-builder.ts +7 -1
  130. package/src/route-types.ts +37 -41
  131. package/src/router/basename.ts +14 -0
  132. package/src/router/content-negotiation.ts +113 -1
  133. package/src/router/error-handling.ts +1 -1
  134. package/src/router/find-match.ts +4 -2
  135. package/src/router/handler-context.ts +77 -38
  136. package/src/router/intercept-resolution.ts +15 -22
  137. package/src/router/lazy-includes.ts +12 -9
  138. package/src/router/loader-resolution.ts +174 -22
  139. package/src/router/logging.ts +5 -2
  140. package/src/router/manifest.ts +31 -16
  141. package/src/router/match-api.ts +128 -192
  142. package/src/router/match-handlers.ts +63 -20
  143. package/src/router/match-middleware/background-revalidation.ts +30 -2
  144. package/src/router/match-middleware/cache-lookup.ts +136 -106
  145. package/src/router/match-middleware/cache-store.ts +54 -10
  146. package/src/router/match-middleware/intercept-resolution.ts +9 -7
  147. package/src/router/match-middleware/segment-resolution.ts +61 -5
  148. package/src/router/match-result.ts +125 -10
  149. package/src/router/metrics.ts +7 -2
  150. package/src/router/middleware-types.ts +21 -34
  151. package/src/router/middleware.ts +103 -90
  152. package/src/router/navigation-snapshot.ts +182 -0
  153. package/src/router/pattern-matching.ts +101 -17
  154. package/src/router/prerender-match.ts +110 -10
  155. package/src/router/preview-match.ts +32 -102
  156. package/src/router/request-classification.ts +286 -0
  157. package/src/router/revalidation.ts +58 -2
  158. package/src/router/route-snapshot.ts +245 -0
  159. package/src/router/router-context.ts +6 -1
  160. package/src/router/router-interfaces.ts +77 -28
  161. package/src/router/router-options.ts +76 -11
  162. package/src/router/router-registry.ts +2 -5
  163. package/src/router/segment-resolution/fresh.ts +223 -24
  164. package/src/router/segment-resolution/helpers.ts +29 -24
  165. package/src/router/segment-resolution/loader-cache.ts +1 -0
  166. package/src/router/segment-resolution/revalidation.ts +466 -285
  167. package/src/router/segment-resolution/view-transition-default.ts +36 -0
  168. package/src/router/segment-wrappers.ts +2 -0
  169. package/src/router/substitute-pattern-params.ts +56 -0
  170. package/src/router/telemetry.ts +99 -0
  171. package/src/router/trie-matching.ts +18 -13
  172. package/src/router/types.ts +9 -0
  173. package/src/router/url-params.ts +49 -0
  174. package/src/router.ts +91 -23
  175. package/src/rsc/handler-context.ts +2 -2
  176. package/src/rsc/handler.ts +440 -381
  177. package/src/rsc/helpers.ts +91 -43
  178. package/src/rsc/index.ts +1 -1
  179. package/src/rsc/loader-fetch.ts +23 -3
  180. package/src/rsc/manifest-init.ts +5 -1
  181. package/src/rsc/origin-guard.ts +28 -10
  182. package/src/rsc/progressive-enhancement.ts +18 -2
  183. package/src/rsc/response-route-handler.ts +46 -53
  184. package/src/rsc/rsc-rendering.ts +41 -48
  185. package/src/rsc/runtime-warnings.ts +9 -10
  186. package/src/rsc/server-action.ts +25 -37
  187. package/src/rsc/ssr-setup.ts +18 -2
  188. package/src/rsc/types.ts +17 -3
  189. package/src/search-params.ts +4 -4
  190. package/src/segment-content-promise.ts +67 -0
  191. package/src/segment-loader-promise.ts +122 -0
  192. package/src/segment-system.tsx +219 -67
  193. package/src/serialize.ts +243 -0
  194. package/src/server/context.ts +277 -61
  195. package/src/server/cookie-store.ts +28 -4
  196. package/src/server/handle-store.ts +19 -0
  197. package/src/server/loader-registry.ts +9 -8
  198. package/src/server/request-context.ts +204 -60
  199. package/src/ssr/index.tsx +9 -1
  200. package/src/static-handler.ts +19 -7
  201. package/src/testing/cache-status.ts +166 -0
  202. package/src/testing/collect-handle.ts +63 -0
  203. package/src/testing/dispatch.ts +440 -0
  204. package/src/testing/dom.entry.ts +22 -0
  205. package/src/testing/e2e/fixture.ts +154 -0
  206. package/src/testing/e2e/index.ts +149 -0
  207. package/src/testing/e2e/matchers.ts +51 -0
  208. package/src/testing/e2e/page-helpers.ts +272 -0
  209. package/src/testing/e2e/parity.ts +306 -0
  210. package/src/testing/e2e/server.ts +183 -0
  211. package/src/testing/flight-matchers.ts +104 -0
  212. package/src/testing/flight-runtime.d.ts +21 -0
  213. package/src/testing/flight.entry.ts +22 -0
  214. package/src/testing/flight.ts +182 -0
  215. package/src/testing/generated-routes.ts +223 -0
  216. package/src/testing/index.ts +106 -0
  217. package/src/testing/internal/context.ts +255 -0
  218. package/src/testing/render-route.tsx +565 -0
  219. package/src/testing/run-loader.ts +296 -0
  220. package/src/testing/run-middleware.ts +179 -0
  221. package/src/testing/vitest-stubs/cloudflare-email.ts +9 -0
  222. package/src/testing/vitest-stubs/cloudflare-workers.ts +21 -0
  223. package/src/testing/vitest-stubs/plugin-rsc.ts +16 -0
  224. package/src/testing/vitest-stubs/version.ts +5 -0
  225. package/src/testing/vitest.ts +183 -0
  226. package/src/types/cache-types.ts +4 -4
  227. package/src/types/global-namespace.ts +39 -26
  228. package/src/types/handler-context.ts +194 -72
  229. package/src/types/index.ts +1 -0
  230. package/src/types/loader-types.ts +41 -15
  231. package/src/types/request-scope.ts +126 -0
  232. package/src/types/route-entry.ts +19 -1
  233. package/src/types/segments.ts +37 -1
  234. package/src/urls/include-helper.ts +34 -67
  235. package/src/urls/index.ts +0 -3
  236. package/src/urls/path-helper-types.ts +50 -9
  237. package/src/urls/path-helper.ts +63 -63
  238. package/src/urls/pattern-types.ts +48 -19
  239. package/src/urls/response-types.ts +25 -22
  240. package/src/urls/type-extraction.ts +26 -116
  241. package/src/urls/urls-function.ts +1 -5
  242. package/src/use-loader.tsx +487 -44
  243. package/src/vite/debug.ts +185 -0
  244. package/src/vite/discovery/bundle-postprocess.ts +34 -37
  245. package/src/vite/discovery/discover-routers.ts +105 -51
  246. package/src/vite/discovery/discovery-errors.ts +194 -0
  247. package/src/vite/discovery/gate-state.ts +171 -0
  248. package/src/vite/discovery/prerender-collection.ts +188 -93
  249. package/src/vite/discovery/route-types-writer.ts +40 -84
  250. package/src/vite/discovery/self-gen-tracking.ts +27 -1
  251. package/src/vite/discovery/state.ts +46 -6
  252. package/src/vite/discovery/virtual-module-codegen.ts +13 -23
  253. package/src/vite/index.ts +6 -0
  254. package/src/vite/plugin-types.ts +111 -72
  255. package/src/vite/plugins/cjs-to-esm.ts +8 -7
  256. package/src/vite/plugins/client-ref-dedup.ts +16 -0
  257. package/src/vite/plugins/client-ref-hashing.ts +28 -5
  258. package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
  259. package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  260. package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
  261. package/src/vite/plugins/expose-action-id.ts +55 -33
  262. package/src/vite/plugins/expose-id-utils.ts +24 -8
  263. package/src/vite/plugins/expose-ids/export-analysis.ts +100 -20
  264. package/src/vite/plugins/expose-ids/handler-transform.ts +12 -35
  265. package/src/vite/plugins/expose-ids/loader-transform.ts +3 -5
  266. package/src/vite/plugins/expose-ids/router-transform.ts +20 -3
  267. package/src/vite/plugins/expose-internal-ids.ts +544 -317
  268. package/src/vite/plugins/performance-tracks.ts +92 -0
  269. package/src/vite/plugins/refresh-cmd.ts +88 -26
  270. package/src/vite/plugins/use-cache-transform.ts +65 -50
  271. package/src/vite/plugins/version-injector.ts +39 -23
  272. package/src/vite/plugins/version-plugin.ts +72 -3
  273. package/src/vite/plugins/virtual-entries.ts +2 -2
  274. package/src/vite/rango.ts +265 -226
  275. package/src/vite/router-discovery.ts +920 -137
  276. package/src/vite/utils/ast-handler-extract.ts +15 -15
  277. package/src/vite/utils/banner.ts +4 -4
  278. package/src/vite/utils/bundle-analysis.ts +4 -2
  279. package/src/vite/utils/client-chunks.ts +190 -0
  280. package/src/vite/utils/forward-user-plugins.ts +193 -0
  281. package/src/vite/utils/manifest-utils.ts +21 -5
  282. package/src/vite/utils/package-resolution.ts +41 -1
  283. package/src/vite/utils/prerender-utils.ts +38 -5
  284. package/src/vite/utils/shared-utils.ts +109 -27
  285. package/src/browser/action-response-classifier.ts +0 -99
@@ -11,12 +11,16 @@ import {
11
11
  getContext,
12
12
  getNamePrefix,
13
13
  getUrlPrefix,
14
+ requireDslContext,
14
15
  type EntryData,
16
+ type EntryPropDatas,
17
+ type EntryPropSegments,
18
+ type HelperContext,
15
19
  type InterceptEntry,
16
20
  } from "../server/context";
17
21
  import { invariant } from "../errors";
18
22
  import { isCachedFunction } from "../cache/taint.js";
19
- import { RSCRouterContext } from "../server/context";
23
+ import { RangoContext } from "../server/context";
20
24
  import { isStaticHandler } from "../static-handler.js";
21
25
  import RootLayout from "../server/root-layout";
22
26
  import type {
@@ -37,6 +41,8 @@ import type {
37
41
  UseItems,
38
42
  } from "../route-types.js";
39
43
  import type { RouteHelpers } from "./helpers-types.js";
44
+ import { resolveHandlerUse, mergeHandlerUse } from "./resolve-handler-use.js";
45
+ import { ALL_USE_ITEM_TYPES } from "./use-item-types.js";
40
46
 
41
47
  /**
42
48
  * Check if an item contains routes (directly or inside nested structures like cache).
@@ -54,19 +60,111 @@ const hasRoutesInItem = (item: AllUseItems): boolean => {
54
60
  if (item.type === "layout" && item.uses) {
55
61
  return item.uses.some((child) => hasRoutesInItem(child));
56
62
  }
63
+ if (item.type === "middleware" && item.uses) {
64
+ return item.uses.some((child) => hasRoutesInItem(child));
65
+ }
57
66
  return false;
58
67
  };
59
68
 
69
+ /**
70
+ * Fresh empty collections shared by every from-scratch segment entry. Returns
71
+ * new arrays/objects per call so no two entries share mutable references.
72
+ * mountPath is intentionally NOT included here — each call site adds it from
73
+ * getUrlPrefix() where applicable: the route() and transition() helpers add
74
+ * none, while path() (which also builds a `type: "route"` entry) and the
75
+ * structural helpers (layout/cache/middleware/parallel) do.
76
+ */
77
+ const emptySegmentBase = (): EntryPropDatas &
78
+ EntryPropSegments & { loading: undefined } => ({
79
+ loading: undefined,
80
+ middleware: [],
81
+ revalidate: [],
82
+ errorBoundary: [],
83
+ notFoundBoundary: [],
84
+ layout: [],
85
+ parallel: {},
86
+ intercept: [],
87
+ loader: [],
88
+ });
89
+
90
+ /**
91
+ * Run a children/use callback as a nested scope, flatten the result, and assert
92
+ * every item is a valid use item. `kind` preserves the existing error wording
93
+ * ("use()" vs "children" callback).
94
+ */
95
+ function runAndValidateUseItems(
96
+ store: ReturnType<typeof getContext>,
97
+ namespace: string,
98
+ entry: EntryData,
99
+ cb: () => any,
100
+ label: string,
101
+ kind: "use" | "children",
102
+ ): AllUseItems[] {
103
+ const result = store.run(namespace, entry, cb)?.flat(3);
104
+ return validateUseItems(result, namespace, label, kind);
105
+ }
106
+
107
+ /** Assert an already-invoked, flattened callback result is a use-item array. */
108
+ function validateUseItems(
109
+ result: any,
110
+ namespace: string,
111
+ label: string,
112
+ kind: "use" | "children",
113
+ ): AllUseItems[] {
114
+ invariant(
115
+ Array.isArray(result) && result.every((item) => isValidUseItem(item)),
116
+ `${label}() ${kind === "use" ? "use()" : "children"} callback must return an array of use items [${namespace}]`,
117
+ );
118
+ return result as AllUseItems[];
119
+ }
120
+
121
+ /** True when a children/use result contains no routes (directly or nested). */
122
+ const isOrphan = (result: AllUseItems[]): boolean =>
123
+ !result.some((item) => item != null && hasRoutesInItem(item));
124
+
125
+ /**
126
+ * Register a routeless structural entry as an orphan sibling: clear its parent
127
+ * pointer so it leaves the middleware/parent-pointer chain (LOAD-BEARING — see
128
+ * docs/tree-structure.md) and push it onto the parent's layout[] so it renders
129
+ * as a wrapper. Used by cache()/middleware()/transition(); layout() runs extra
130
+ * validation and registers inline.
131
+ */
132
+ const attachOrphanSibling = (
133
+ parent: EntryData | null,
134
+ entry: EntryData,
135
+ ): void => {
136
+ entry.parent = null;
137
+ if (parent && "layout" in parent) parent.layout.push(entry);
138
+ };
139
+
140
+ /**
141
+ * Run `fn` with `ctx.parent` temporarily redirected to `temp` — a satellite
142
+ * entry that captures the attachments declared by a use() callback — restoring
143
+ * the original parent afterward, including on throw. loader()/intercept() each
144
+ * build their own tempParent shape (intercept keeps a loading get/set accessor
145
+ * and a captured-layouts array); this only centralizes the save/restore.
146
+ */
147
+ function withParent<T>(ctx: HelperContext, temp: EntryData, fn: () => T): T {
148
+ const original = ctx.parent;
149
+ ctx.parent = temp;
150
+ try {
151
+ return fn();
152
+ } finally {
153
+ ctx.parent = original;
154
+ }
155
+ }
156
+
60
157
  const revalidate: RouteHelpers<any, any>["revalidate"] = (fn) => {
61
- const ctx = getContext().getStore();
62
- if (!ctx) throw new Error("revalidate() must be called inside map()");
158
+ const { store, ctx } = requireDslContext(
159
+ "revalidate() must be called inside urls()",
160
+ );
63
161
 
64
162
  // Attach to last entry in stack
65
163
  const parent = ctx.parent;
66
164
  if (!parent || !("revalidate" in parent)) {
67
165
  invariant(false, "No parent entry available for revalidate()");
68
166
  }
69
- const name = `$${getContext().getNextIndex("revalidate")}`;
167
+ const name = `$${store.getNextIndex("revalidate")}`;
70
168
  parent.revalidate.push(fn);
71
169
  return { name, type: "revalidate" } as RevalidateItem;
72
170
  };
@@ -104,15 +202,16 @@ const revalidate: RouteHelpers<any, any>["revalidate"] = (fn) => {
104
202
  * ```
105
203
  */
106
204
  const errorBoundary: RouteHelpers<any, any>["errorBoundary"] = (fallback) => {
107
- const ctx = getContext().getStore();
108
- if (!ctx) throw new Error("errorBoundary() must be called inside map()");
205
+ const { store, ctx } = requireDslContext(
206
+ "errorBoundary() must be called inside urls()",
207
+ );
109
208
 
110
209
  // Attach to parent entry in stack
111
210
  const parent = ctx.parent;
112
211
  if (!parent || !("errorBoundary" in parent)) {
113
212
  invariant(false, "No parent entry available for errorBoundary()");
114
213
  }
115
- const name = `$${getContext().getNextIndex("errorBoundary")}`;
214
+ const name = `$${store.getNextIndex("errorBoundary")}`;
116
215
  parent.errorBoundary.push(fallback);
117
216
  return { name, type: "errorBoundary" } as ErrorBoundaryItem;
118
217
  };
@@ -151,15 +250,16 @@ const errorBoundary: RouteHelpers<any, any>["errorBoundary"] = (fallback) => {
151
250
  const notFoundBoundary: RouteHelpers<any, any>["notFoundBoundary"] = (
152
251
  fallback,
153
252
  ) => {
154
- const ctx = getContext().getStore();
155
- if (!ctx) throw new Error("notFoundBoundary() must be called inside map()");
253
+ const { store, ctx } = requireDslContext(
254
+ "notFoundBoundary() must be called inside urls()",
255
+ );
156
256
 
157
257
  // Attach to parent entry in stack
158
258
  const parent = ctx.parent;
159
259
  if (!parent || !("notFoundBoundary" in parent)) {
160
260
  invariant(false, "No parent entry available for notFoundBoundary()");
161
261
  }
162
- const name = `$${getContext().getNextIndex("notFoundBoundary")}`;
262
+ const name = `$${store.getNextIndex("notFoundBoundary")}`;
163
263
  parent.notFoundBoundary.push(fallback);
164
264
  return { name, type: "notFoundBoundary" } as NotFoundBoundaryItem;
165
265
  };
@@ -173,8 +273,9 @@ const notFoundBoundary: RouteHelpers<any, any>["notFoundBoundary"] = (
173
273
  * for the intercept to activate.
174
274
  */
175
275
  const when: RouteHelpers<any, any>["when"] = (fn) => {
176
- const ctx = getContext().getStore();
177
- if (!ctx) throw new Error("when() must be called inside intercept()");
276
+ const { store, ctx } = requireDslContext(
277
+ "when() must be called inside intercept()",
278
+ );
178
279
 
179
280
  // The when() function needs to be captured by the intercept's tempParent
180
281
  // which should have a `when` array. If not present, we're not inside intercept()
@@ -186,7 +287,7 @@ const when: RouteHelpers<any, any>["when"] = (fn) => {
186
287
  );
187
288
  }
188
289
 
189
- const name = `$${getContext().getNextIndex("when")}`;
290
+ const name = `$${store.getNextIndex("when")}`;
190
291
  parent.when.push(fn);
191
292
  return { name, type: "when" } as WhenItem;
192
293
  };
@@ -213,9 +314,9 @@ const cache: RouteHelpers<any, any>["cache"] = (
213
314
  | (() => UseItems<AllUseItems>),
214
315
  maybeChildren?: () => UseItems<AllUseItems>,
215
316
  ) => {
216
- const store = getContext();
217
- const ctx = store.getStore();
218
- if (!ctx) throw new Error("cache() must be called inside map()");
317
+ const { store, ctx } = requireDslContext(
318
+ "cache() must be called inside urls()",
319
+ );
219
320
 
220
321
  // Handle overloaded signature
221
322
  let options: PartialCacheOptions | false;
@@ -228,7 +329,7 @@ const cache: RouteHelpers<any, any>["cache"] = (
228
329
  } else if (typeof optionsOrChildren === "string") {
229
330
  // cache('profileName') or cache('profileName', () => [...])
230
331
  // Resolve from context-scoped profiles (set per-router via HelperContext).
231
- const ctxStore = RSCRouterContext.getStore();
332
+ const ctxStore = RangoContext.getStore();
232
333
  const profile = ctxStore?.cacheProfiles?.[optionsOrChildren];
233
334
  invariant(
234
335
  profile,
@@ -267,26 +368,18 @@ const cache: RouteHelpers<any, any>["cache"] = (
267
368
  // Create orphan cache entry (like orphan layout)
268
369
  // Subsequent siblings in the same array will attach to this entry
269
370
  const namespace = `${ctx.namespace}.${cacheIndex}`;
270
- const cacheUrlPrefix = getUrlPrefix();
371
+ const urlPrefix = getUrlPrefix();
271
372
 
272
373
  const entry = {
374
+ ...emptySegmentBase(),
273
375
  id: namespace,
274
376
  shortCode: store.getShortCode("cache"),
275
377
  type: "cache",
276
378
  parent: parent, // link to current parent for hierarchy
277
379
  cache: cacheConfig,
278
380
  handler: RootLayout,
279
- loading: undefined, // Allow loading() to attach loading state
280
- middleware: [],
281
- revalidate: [],
282
- errorBoundary: [],
283
- notFoundBoundary: [],
284
- layout: [],
285
- parallel: [],
286
- intercept: [],
287
- loader: [],
288
- ...(cacheUrlPrefix ? { mountPath: cacheUrlPrefix } : {}),
289
- } as EntryData;
381
+ ...(urlPrefix ? { mountPath: urlPrefix } : {}),
382
+ } satisfies EntryData;
290
383
 
291
384
  // Attach to parent's layout array (cache entries are structural like layouts)
292
385
  if (parent && "layout" in parent) {
@@ -300,13 +393,23 @@ const cache: RouteHelpers<any, any>["cache"] = (
300
393
  return { name: namespace, type: "cache" } as CacheItem;
301
394
  }
302
395
 
396
+ // Inside a loader() use() callback, only the direct form — cache()/cache(opts)/
397
+ // cache("profile") — writes cache config to the loader entry. The wrapper
398
+ // form creates a structural cache boundary with its own children scope, which
399
+ // has no effect on the loader and would silently no-op.
400
+ invariant(
401
+ !(ctx.parent && (ctx.parent as any).type === "loader"),
402
+ "cache() wrapper form is not valid inside loader() use(). Use cache({...}) without children to configure the loader's cache.",
403
+ );
404
+
303
405
  // With children: create a cache entry (like layout with caching semantics)
304
406
  const namespace = `${ctx.namespace}.${cacheIndex}`;
305
407
  const cacheShortCode = store.getShortCode("cache");
306
408
 
307
- const cacheUrlPrefix2 = getUrlPrefix();
409
+ const urlPrefix = getUrlPrefix();
308
410
 
309
411
  const entry = {
412
+ ...emptySegmentBase(),
310
413
  id: namespace,
311
414
  shortCode: cacheShortCode,
312
415
  type: "cache",
@@ -314,48 +417,57 @@ const cache: RouteHelpers<any, any>["cache"] = (
314
417
  cache: cacheConfig,
315
418
  // Cache entries render like layouts (with Outlet as default handler)
316
419
  handler: RootLayout, // RootLayout just renders <Outlet />
317
- loading: undefined, // Allow loading() to attach loading state
318
- middleware: [],
319
- revalidate: [],
320
- errorBoundary: [],
321
- notFoundBoundary: [],
322
- layout: [],
323
- parallel: [],
324
- intercept: [],
325
- loader: [],
326
- ...(cacheUrlPrefix2 ? { mountPath: cacheUrlPrefix2 } : {}),
327
- } as EntryData;
420
+ ...(urlPrefix ? { mountPath: urlPrefix } : {}),
421
+ } satisfies EntryData;
328
422
 
329
423
  // Run children with cache entry as parent
330
- const result = store.run(namespace, entry, children)?.flat(3);
331
-
332
- invariant(
333
- Array.isArray(result) && result.every((item) => isValidUseItem(item)),
334
- `cache() children callback must return an array of use items [${namespace}]`,
424
+ const result = runAndValidateUseItems(
425
+ store,
426
+ namespace,
427
+ entry,
428
+ children,
429
+ "cache",
430
+ "children",
335
431
  );
336
432
 
337
- // Check if this cache has routes (including nested caches/layouts)
338
- const hasRoutes =
339
- result &&
340
- Array.isArray(result) &&
341
- result.some((item) => hasRoutesInItem(item));
342
-
343
- if (!hasRoutes) {
344
- const parent = ctx.parent;
345
- if (parent && "layout" in parent) {
346
- // Attach to parent's layout array (cache entries are structural like layouts)
347
- entry.parent = null;
348
- parent.layout.push(entry);
349
- }
350
- }
433
+ // Cache entries are structural like layouts: with no routes inside, register
434
+ // as an orphan sibling.
435
+ if (isOrphan(result)) attachOrphanSibling(ctx.parent, entry);
351
436
 
352
437
  return { name: namespace, type: "cache", uses: result } as CacheItem;
353
438
  };
354
439
 
355
- const middleware: RouteHelpers<any, any>["middleware"] = (...fn) => {
440
+ const middleware: RouteHelpers<any, any>["middleware"] = (...args: any[]) => {
441
+ // Four call forms:
442
+ // middleware(fn) — single fn, sibling
443
+ // middleware(fn, () => [...]) — single fn, wrapping
444
+ // middleware([fn1, fn2]) — array, sibling
445
+ // middleware([fn1, fn2], () => [...]) — array, wrapping
446
+ const isArray = Array.isArray(args[0]);
447
+
448
+ // Reject the removed variadic form before executing anything.
449
+ // middleware(fn1, fn2, fn3) — 3+ args, always wrong.
450
+ // middleware(fn1, fn2) where fn2 is a middleware fn (length >= 1), not a
451
+ // children callback (length === 0) — legacy two-fn form, reject early.
452
+ if (
453
+ args.length > 2 ||
454
+ (!isArray &&
455
+ args.length === 2 &&
456
+ typeof args[1] === "function" &&
457
+ args[1].length > 0)
458
+ ) {
459
+ throw new Error(
460
+ "middleware() no longer accepts variadic arguments. " +
461
+ "Use middleware([fn1, fn2, ...]) instead of middleware(fn1, fn2, ...).",
462
+ );
463
+ }
464
+
465
+ const fns: MiddlewareFn<any>[] = isArray ? args[0] : [args[0]];
466
+ const children: (() => any[]) | undefined =
467
+ typeof args[1] === "function" ? args[1] : undefined;
468
+
356
469
  // Prevent "use cache" functions from being used as middleware.
357
- // Checked before context validation — this is a static invariant.
358
- for (const f of fn) {
470
+ for (const f of fns) {
359
471
  if (isCachedFunction(f)) {
360
472
  throw new Error(
361
473
  `A "use cache" function cannot be used as middleware. ` +
@@ -366,23 +478,68 @@ const middleware: RouteHelpers<any, any>["middleware"] = (...fn) => {
366
478
  }
367
479
  }
368
480
 
369
- const ctx = getContext().getStore();
370
- if (!ctx) throw new Error("middleware() must be called inside map()");
481
+ const { store, ctx } = requireDslContext(
482
+ "middleware() must be called inside urls()",
483
+ );
371
484
 
372
- // Attach to last entry in stack
373
- const parent = ctx.parent;
374
- if (!parent || !("middleware" in parent)) {
375
- invariant(false, "No parent entry available for middleware()");
485
+ if (!children) {
486
+ // Sibling mode: attach to parent entry
487
+ const parent = ctx.parent;
488
+ if (!parent || !("middleware" in parent)) {
489
+ invariant(false, "No parent entry available for middleware()");
490
+ }
491
+ const name = `$${store.getNextIndex("middleware")}`;
492
+ parent.middleware.push(...fns);
493
+ return { name, type: "middleware" } as MiddlewareItem;
376
494
  }
377
- const name = `$${getContext().getNextIndex("middleware")}`;
378
- parent.middleware.push(...fn);
379
- return { name, type: "middleware" } as MiddlewareItem;
495
+
496
+ // Wrapping mode: create a transparent layout that carries the middleware
497
+ const mwIndex = store.getNextIndex("middleware");
498
+ const namespace = `${ctx.namespace}.${mwIndex}`;
499
+
500
+ const urlPrefix = getUrlPrefix();
501
+ const entry = {
502
+ ...emptySegmentBase(),
503
+ id: namespace,
504
+ shortCode: store.getShortCode("layout"),
505
+ type: "layout",
506
+ parent: ctx.parent,
507
+ handler: RootLayout,
508
+ middleware: [...fns],
509
+ ...(urlPrefix ? { mountPath: urlPrefix } : {}),
510
+ } satisfies EntryData;
511
+
512
+ // Run children callback. If the second arg was actually a middleware fn
513
+ // (old variadic form: middleware(mw1, mw2)), this will return a non-array
514
+ // and the invariant below gives a clear migration error.
515
+ const rawResult = store.run(namespace, entry, children);
516
+
517
+ invariant(
518
+ Array.isArray(rawResult),
519
+ "middleware(fn, children) expects the second argument to return an array of use items. " +
520
+ "To pass multiple middleware, use middleware([fn1, fn2]).",
521
+ );
522
+
523
+ const result = validateUseItems(
524
+ rawResult.flat(3),
525
+ namespace,
526
+ "middleware",
527
+ "children",
528
+ );
529
+
530
+ if (isOrphan(result)) attachOrphanSibling(ctx.parent, entry);
531
+
532
+ return {
533
+ name: namespace,
534
+ type: "middleware",
535
+ uses: result,
536
+ } as MiddlewareItem;
380
537
  };
381
538
 
382
539
  const parallel: RouteHelpers<any, any>["parallel"] = (slots, use) => {
383
- const store = getContext();
384
- const ctx = store.getStore();
385
- if (!ctx) throw new Error("parallel() must be called inside map()");
540
+ const { store, ctx } = requireDslContext(
541
+ "parallel() must be called inside urls()",
542
+ );
386
543
 
387
544
  if (!ctx.parent || !ctx.parent?.parallel) {
388
545
  invariant(false, "No parent entry available for parallel()");
@@ -393,15 +550,29 @@ const parallel: RouteHelpers<any, any>["parallel"] = (slots, use) => {
393
550
  "parallel() cannot be nested inside another parallel()",
394
551
  );
395
552
 
553
+ const slotNames = Object.keys(slots as Record<string, any>) as `@${string}`[];
554
+
396
555
  const namespace = `${ctx.namespace}.$${store.getNextIndex("parallel")}`;
397
556
 
398
- // Unwrap any static handler definitions in parallel slots
557
+ // Unwrap slot values. A slot value can be:
558
+ // - a Handler / ReactNode (legacy form)
559
+ // - a Static() definition (build-time only)
560
+ // - a slot descriptor `{ handler, use? }` for slot-local overrides
561
+ // The descriptor's `use` runs after the broadcast `use` for that slot,
562
+ // so single-assignment items like `loading()` placed there win without
563
+ // affecting siblings.
399
564
  const unwrappedSlots: Record<string, any> = {};
565
+ const slotLocalUses: Record<string, (() => any[]) | undefined> = {};
400
566
  let hasStaticSlot = false;
401
567
  const staticSlotIds: Record<string, string> = {};
402
- for (const [slotName, slotHandler] of Object.entries(
568
+ for (const [slotName, rawSlot] of Object.entries(
403
569
  slots as Record<string, any>,
404
570
  )) {
571
+ let slotHandler: any = rawSlot;
572
+ if (isSlotDescriptor(rawSlot)) {
573
+ slotHandler = rawSlot.handler;
574
+ slotLocalUses[slotName] = rawSlot.use;
575
+ }
405
576
  if (isStaticHandler(slotHandler)) {
406
577
  hasStaticSlot = true;
407
578
  unwrappedSlots[slotName] = slotHandler.handler;
@@ -420,20 +591,12 @@ const parallel: RouteHelpers<any, any>["parallel"] = (slots, use) => {
420
591
  // Create full EntryData for parallel with its own loaders/revalidate/loading
421
592
  const parallelUrlPrefix = getUrlPrefix();
422
593
  const entry = {
594
+ ...emptySegmentBase(),
423
595
  id: namespace,
424
596
  shortCode: store.getShortCode("parallel"),
425
597
  type: "parallel",
426
598
  parent: null, // Parallels don't participate in parent chain traversal
427
599
  handler: unwrappedSlots,
428
- loading: undefined, // Allow loading() to attach loading state
429
- middleware: [],
430
- revalidate: [],
431
- errorBoundary: [],
432
- notFoundBoundary: [],
433
- layout: [],
434
- parallel: [],
435
- intercept: [],
436
- loader: [],
437
600
  ...(parallelUrlPrefix ? { mountPath: parallelUrlPrefix } : {}),
438
601
  ...(hasStaticSlot
439
602
  ? {
@@ -445,19 +608,86 @@ const parallel: RouteHelpers<any, any>["parallel"] = (slots, use) => {
445
608
  : {}),
446
609
  } satisfies EntryData;
447
610
 
448
- // Run use callback if provided to collect loaders, revalidate, loading
449
- if (use && typeof use === "function") {
450
- const result = store.run(namespace, entry, use)?.flat(3);
451
- invariant(
452
- Array.isArray(result) && result.every((item) => isValidUseItem(item)),
453
- `parallel() use() callback must return an array of use items [${namespace}]`,
611
+ for (const slotName of slotNames) {
612
+ const slotEntry = {
613
+ ...entry,
614
+ handler: { [slotName]: unwrappedSlots[slotName]! },
615
+ middleware: [...entry.middleware],
616
+ revalidate: [...entry.revalidate],
617
+ errorBoundary: [...entry.errorBoundary],
618
+ notFoundBoundary: [...entry.notFoundBoundary],
619
+ layout: [...entry.layout],
620
+ parallel: { ...entry.parallel },
621
+ intercept: [...entry.intercept],
622
+ loader: [...entry.loader],
623
+ ...(entry.staticHandlerIds?.[slotName]
624
+ ? {
625
+ isStaticPrerender: true as const,
626
+ staticHandlerIds: { [slotName]: entry.staticHandlerIds[slotName]! },
627
+ }
628
+ : {
629
+ isStaticPrerender: undefined,
630
+ staticHandlerIds: undefined,
631
+ }),
632
+ } satisfies EntryData;
633
+
634
+ // Per-slot merge order (narrowest-scope-wins for single-assignment items
635
+ // like loading()):
636
+ // 1. handler.use — defaults baked into the handler
637
+ // 2. shared `use` — broadcast at the parallel() call site
638
+ // 3. slot-local `use` — per-slot override via `{ handler, use }` descriptor
639
+ // Items that accumulate (loader, middleware, revalidate, …) compose
640
+ // across all three layers regardless of order.
641
+ const rawSlot = (slots as Record<string, any>)[slotName];
642
+ const slotHandlerForUse = isSlotDescriptor(rawSlot)
643
+ ? rawSlot.handler
644
+ : rawSlot;
645
+ const slotHandlerUse = resolveHandlerUse(slotHandlerForUse);
646
+ const slotLocalUse = slotLocalUses[slotName];
647
+ const explicitUse = combineExplicitUses(use, slotLocalUse);
648
+ const slotMergedUse = mergeHandlerUse(
649
+ slotHandlerUse,
650
+ explicitUse,
651
+ "parallel",
454
652
  );
455
- }
653
+ if (slotMergedUse) {
654
+ runAndValidateUseItems(
655
+ store,
656
+ namespace,
657
+ slotEntry,
658
+ slotMergedUse,
659
+ "parallel",
660
+ "use",
661
+ );
662
+ }
456
663
 
457
- ctx.parent.parallel.push(entry);
664
+ ctx.parent.parallel[slotName] = slotEntry;
665
+ }
458
666
  return { name: namespace, type: "parallel" } as ParallelItem;
459
667
  };
460
668
 
669
+ function isSlotDescriptor(
670
+ value: unknown,
671
+ ): value is { handler: unknown; use?: () => any[] } {
672
+ return (
673
+ typeof value === "object" &&
674
+ value !== null &&
675
+ !("__brand" in value) &&
676
+ "handler" in value &&
677
+ typeof (value as any).handler !== "undefined"
678
+ );
679
+ }
680
+
681
+ function combineExplicitUses(
682
+ sharedUse: (() => any[]) | undefined,
683
+ slotLocalUse: (() => any[]) | undefined,
684
+ ): (() => any[]) | undefined {
685
+ if (!sharedUse && !slotLocalUse) return undefined;
686
+ if (!slotLocalUse) return sharedUse;
687
+ if (!sharedUse) return slotLocalUse;
688
+ return () => [...sharedUse(), ...slotLocalUse()];
689
+ }
690
+
461
691
  /**
462
692
  * Intercept helper - defines an intercepting route for soft navigation
463
693
  */
@@ -467,9 +697,9 @@ const intercept = (
467
697
  handler: any,
468
698
  use?: () => any[],
469
699
  ) => {
470
- const store = getContext();
471
- const ctx = store.getStore();
472
- if (!ctx) throw new Error("intercept() must be called inside map()");
700
+ const { store, ctx } = requireDslContext(
701
+ "intercept() must be called inside urls()",
702
+ );
473
703
 
474
704
  if (!ctx.parent || !ctx.parent?.intercept) {
475
705
  invariant(false, "No parent entry available for intercept()");
@@ -502,17 +732,19 @@ const intercept = (
502
732
  when: [], // Selector conditions for conditional interception
503
733
  };
504
734
 
505
- // Run use callback if provided to collect loaders, revalidate, middleware, etc.
506
- if (use && typeof use === "function") {
507
- // Create a temporary parent context for the use() callback
508
- // so that middleware, loader, revalidate attach to the intercept entry
509
- const originalParent = ctx.parent;
735
+ // Merge handler.use defaults with explicit use
736
+ const handlerUseFn = resolveHandlerUse(handler);
737
+ const mergedUse = mergeHandlerUse(handlerUseFn, use, "intercept");
510
738
 
511
- // Capture layouts in a temporary array
739
+ // Run merged use callback to collect loaders, revalidate, middleware, etc.
740
+ if (mergedUse) {
741
+ // Capture layout() calls into a temporary array
512
742
  const capturedLayouts: EntryData[] = [];
513
743
 
744
+ // Temporary parent so middleware/loader/revalidate/when attach to the
745
+ // intercept entry; the loading get/set accessor mirrors writes onto `entry`.
514
746
  const tempParent = {
515
- ...originalParent,
747
+ ...ctx.parent,
516
748
  middleware: entry.middleware,
517
749
  revalidate: entry.revalidate,
518
750
  errorBoundary: entry.errorBoundary,
@@ -520,7 +752,6 @@ const intercept = (
520
752
  loader: entry.loader,
521
753
  layout: capturedLayouts, // Capture layout() calls
522
754
  when: entry.when, // Capture when() conditions
523
- // Use getter/setter to capture loading on the entry
524
755
  get loading() {
525
756
  return entry.loading;
526
757
  },
@@ -528,12 +759,10 @@ const intercept = (
528
759
  entry.loading = value;
529
760
  },
530
761
  };
531
- ctx.parent = tempParent as EntryData;
532
762
 
533
- const result = use()?.flat(3);
534
-
535
- // Restore original parent
536
- ctx.parent = originalParent;
763
+ const result = withParent(ctx, tempParent as EntryData, () =>
764
+ mergedUse()?.flat(3),
765
+ );
537
766
 
538
767
  // Extract layout from captured layouts (use first one if multiple)
539
768
  // Layout inside intercept should always be ReactNode or Handler, not Record slots
@@ -543,10 +772,7 @@ const intercept = (
543
772
  | Handler<any, any, any>;
544
773
  }
545
774
 
546
- invariant(
547
- Array.isArray(result) && result.every((item) => isValidUseItem(item)),
548
- `intercept() use() callback must return an array of use items [${namespace}]`,
549
- );
775
+ validateUseItems(result, namespace, "intercept", "use");
550
776
  }
551
777
 
552
778
  ctx.parent.intercept.push(entry);
@@ -556,10 +782,10 @@ const intercept = (
556
782
  /**
557
783
  * Loader helper - attaches a loader to the current entry
558
784
  */
559
- const loaderFn: RouteHelpers<any, any>["loader"] = (loaderDef, use) => {
560
- const store = getContext();
561
- const ctx = store.getStore();
562
- if (!ctx) throw new Error("loader() must be called inside map()");
785
+ const loader: RouteHelpers<any, any>["loader"] = (loaderDef, use) => {
786
+ const { store, ctx } = requireDslContext(
787
+ "loader() must be called inside urls()",
788
+ );
563
789
 
564
790
  // Attach to last entry in stack
565
791
  if (!ctx.parent || !ctx.parent?.loader) {
@@ -574,25 +800,28 @@ const loaderFn: RouteHelpers<any, any>["loader"] = (loaderDef, use) => {
574
800
  revalidate: [] as ShouldRevalidateFn<any, any>[],
575
801
  };
576
802
 
577
- // If use() callback provided, run it to collect revalidation rules and cache config
578
- if (use && typeof use === "function") {
579
- // Temporarily set context for revalidate()/cache() calls to target this loader
580
- const originalParent = ctx.parent;
803
+ // Merge handler.use defaults (attached to the loader definition) with explicit use
804
+ const handlerUseFn = resolveHandlerUse(loaderDef);
805
+ const mergedUse = mergeHandlerUse(handlerUseFn, use, "loader");
806
+
807
+ // If any use callback is in effect, run it to collect revalidation rules and cache config
808
+ if (mergedUse) {
581
809
  // Create a temporary "parent" with type "loader" so cache() can detect it.
582
810
  // Save existing .cache to distinguish inherited config from newly set config.
583
- const parentCache = (originalParent as any).cache;
811
+ const parentCache = (ctx.parent as any).cache;
584
812
  const tempParent = {
585
- ...originalParent,
813
+ ...ctx.parent,
586
814
  type: "loader",
587
815
  revalidate: loaderEntry.revalidate,
588
816
  };
589
- ctx.parent = tempParent as EntryData;
590
817
 
591
- const result = use()?.flat(3);
818
+ const result = withParent(ctx, tempParent as EntryData, () =>
819
+ mergedUse()?.flat(3),
820
+ );
592
821
 
593
822
  // Copy cache config only if cache() was called during the use() callback.
594
- // The spread from originalParent may carry an inherited .cache from
595
- // a parent cache() boundary — only copy if it was newly set.
823
+ // The spread may carry an inherited .cache from a parent cache() boundary —
824
+ // only copy if it was newly set.
596
825
  if (
597
826
  (tempParent as any).cache &&
598
827
  (tempParent as any).cache !== parentCache
@@ -600,13 +829,7 @@ const loaderFn: RouteHelpers<any, any>["loader"] = (loaderDef, use) => {
600
829
  (loaderEntry as any).cache = (tempParent as any).cache;
601
830
  }
602
831
 
603
- // Restore original parent
604
- ctx.parent = originalParent;
605
-
606
- invariant(
607
- Array.isArray(result) && result.every((item) => isValidUseItem(item)),
608
- `loader() use() callback must return an array of use items [${name}]`,
609
- );
832
+ validateUseItems(result, name, "loader", "use");
610
833
  }
611
834
 
612
835
  ctx.parent.loader.push(loaderEntry);
@@ -617,21 +840,25 @@ const loaderFn: RouteHelpers<any, any>["loader"] = (loaderDef, use) => {
617
840
  * Loading helper - attaches a loading component to the current entry
618
841
  * Loading components are static (no context) and shown during navigation
619
842
  */
620
- const loadingFn: RouteHelpers<any, any>["loading"] = (component, options) => {
621
- const store = getContext();
622
- const ctx = store.getStore();
623
- if (!ctx) throw new Error("loading() must be called inside map()");
843
+ const loading: RouteHelpers<any, any>["loading"] = (component, options) => {
844
+ const { store, ctx } = requireDslContext(
845
+ "loading() must be called inside urls()",
846
+ );
624
847
 
625
848
  const parent = ctx.parent;
626
849
  if (!parent || !("loading" in parent)) {
627
850
  invariant(false, "No parent entry available for loading()");
628
851
  }
629
852
 
853
+ // Unwrap function form: loading(() => <Skeleton />) → loading(<Skeleton />)
854
+ const resolved =
855
+ typeof component === "function" ? (component as () => any)() : component;
856
+
630
857
  // If ssr: false and we're in SSR, set loading to false
631
858
  if (options?.ssr === false && ctx.isSSR) {
632
859
  parent.loading = false;
633
860
  } else {
634
- parent.loading = component;
861
+ parent.loading = resolved;
635
862
  }
636
863
 
637
864
  const name = `$${store.getNextIndex("loading")}`;
@@ -639,10 +866,13 @@ const loadingFn: RouteHelpers<any, any>["loading"] = (component, options) => {
639
866
  };
640
867
 
641
868
  /**
642
- * Transition helper - attaches a ViewTransition config to the current entry
643
- * or wraps a group of routes in a transparent layout with ViewTransition
869
+ * Transition helper - opts the entry (or a wrapped group of routes) into
870
+ * transition-driven navigation by attaching a TransitionConfig. This drives the
871
+ * commit through startTransition (content hold on all React versions) and, on
872
+ * experimental React, places a `<ViewTransition>` boundary unless
873
+ * `viewTransition: false`. See skills/view-transitions for the matrix.
644
874
  */
645
- const transitionFn = (
875
+ const transition = (
646
876
  configOrChildren?: TransitionConfig | (() => UseItems<AllUseItems>),
647
877
  maybeChildren?: () => UseItems<AllUseItems>,
648
878
  ): TransitionItem => {
@@ -656,9 +886,9 @@ const transitionFn = (
656
886
  const children: (() => UseItems<AllUseItems>) | undefined =
657
887
  typeof configOrChildren === "function" ? configOrChildren : maybeChildren;
658
888
 
659
- const store = getContext();
660
- const ctx = store.getStore();
661
- if (!ctx) throw new Error("transition() must be called inside map()");
889
+ const { store, ctx } = requireDslContext(
890
+ "transition() must be called inside urls()",
891
+ );
662
892
 
663
893
  const name = `$${store.getNextIndex("transition")}`;
664
894
 
@@ -675,68 +905,43 @@ const transitionFn = (
675
905
  // Position 2: wrapper — create a transparent layout with transition config
676
906
  const namespace = `${ctx.namespace}.${store.getNextIndex("transition")}`;
677
907
  const entry = {
908
+ ...emptySegmentBase(),
678
909
  id: namespace,
679
910
  shortCode: store.getShortCode("layout"),
680
911
  type: "layout",
681
912
  parent: ctx.parent,
682
913
  handler: RootLayout,
683
- loading: undefined,
684
914
  transition: config,
685
- middleware: [],
686
- revalidate: [],
687
- errorBoundary: [],
688
- notFoundBoundary: [],
689
- layout: [],
690
- parallel: [],
691
- intercept: [],
692
- loader: [],
693
- } as EntryData;
694
-
695
- const result = store.run(namespace, entry, children)?.flat(3);
915
+ } satisfies EntryData;
696
916
 
697
- invariant(
698
- Array.isArray(result) && result.every((item) => isValidUseItem(item)),
699
- `transition() children callback must return an array of use items [${namespace}]`,
917
+ const result = runAndValidateUseItems(
918
+ store,
919
+ namespace,
920
+ entry,
921
+ children,
922
+ "transition",
923
+ "children",
700
924
  );
701
925
 
702
- const hasRoutes =
703
- result &&
704
- Array.isArray(result) &&
705
- result.some((item) => hasRoutesInItem(item));
706
-
707
- if (!hasRoutes) {
708
- const parent = ctx.parent;
709
- if (parent && "layout" in parent) {
710
- entry.parent = null;
711
- parent.layout.push(entry);
712
- }
713
- }
926
+ if (isOrphan(result)) attachOrphanSibling(ctx.parent, entry);
714
927
 
715
928
  return { name: namespace, type: "transition" } as TransitionItem;
716
929
  };
717
930
 
718
- const routeFn: RouteHelpers<any, any>["route"] = (name, handler, use) => {
719
- const store = getContext();
720
- const ctx = store.getStore();
721
- if (!ctx) throw new Error("route() must be called inside map()");
931
+ const route: RouteHelpers<any, any>["route"] = (name, handler, use) => {
932
+ const { store, ctx } = requireDslContext(
933
+ "route() must be called inside urls()",
934
+ );
722
935
 
723
936
  const namespace = `${ctx.namespace}.${store.getNextIndex("route")}.${name}`;
724
937
 
725
938
  const entry = {
939
+ ...emptySegmentBase(),
726
940
  id: namespace,
727
941
  shortCode: store.getShortCode("route"),
728
942
  type: "route",
729
943
  parent: ctx.parent,
730
- handler,
731
- loading: undefined, // Allow loading() to attach loading state
732
- middleware: [],
733
- revalidate: [],
734
- errorBoundary: [],
735
- notFoundBoundary: [],
736
- layout: [],
737
- parallel: [],
738
- intercept: [],
739
- loader: [],
944
+ handler: handler as unknown as Handler<any, any, any>,
740
945
  } satisfies EntryData;
741
946
 
742
947
  /* We will throw if user is registring same route name twice */
@@ -746,12 +951,18 @@ const routeFn: RouteHelpers<any, any>["route"] = (name, handler, use) => {
746
951
  );
747
952
  /* Register route entry */
748
953
  ctx.manifest.set(name, entry);
954
+ /* Merge handler.use defaults with explicit use */
955
+ const handlerUseFn = resolveHandlerUse(handler);
956
+ const mergedUse = mergeHandlerUse(handlerUseFn, use, "route");
749
957
  /* Run use and attach handlers */
750
- if (use && typeof use === "function") {
751
- const result = store.run(namespace, entry, use)?.flat(3);
752
- invariant(
753
- Array.isArray(result) && result.every((item) => isValidUseItem(item)),
754
- `route() use() callback must return an array of use items [${namespace}]`,
958
+ if (mergedUse) {
959
+ const result = runAndValidateUseItems(
960
+ store,
961
+ namespace,
962
+ entry,
963
+ mergedUse,
964
+ "route",
965
+ "use",
755
966
  );
756
967
  return { name: namespace, type: "route", uses: result } as RouteItem;
757
968
  }
@@ -761,9 +972,9 @@ const routeFn: RouteHelpers<any, any>["route"] = (name, handler, use) => {
761
972
  };
762
973
 
763
974
  const layout: RouteHelpers<any, any>["layout"] = (handler, use) => {
764
- const store = getContext();
765
- const ctx = store.getStore();
766
- if (!ctx) throw new Error("layout() must be called inside map()");
975
+ const { store, ctx } = requireDslContext(
976
+ "layout() must be called inside urls()",
977
+ );
767
978
 
768
979
  invariant(
769
980
  !ctx.parent || ctx.parent.type !== "parallel",
@@ -781,20 +992,12 @@ const layout: RouteHelpers<any, any>["layout"] = (handler, use) => {
781
992
 
782
993
  const urlPrefix = getUrlPrefix();
783
994
  const entry = {
995
+ ...emptySegmentBase(),
784
996
  id: namespace,
785
997
  shortCode,
786
998
  type: "layout",
787
999
  parent: ctx.parent,
788
1000
  handler: unwrappedHandler,
789
- loading: undefined, // Allow loading() to attach loading state
790
- middleware: [],
791
- revalidate: [],
792
- errorBoundary: [],
793
- notFoundBoundary: [],
794
- parallel: [],
795
- intercept: [],
796
- layout: [],
797
- loader: [],
798
1001
  ...(urlPrefix ? { mountPath: urlPrefix } : {}),
799
1002
  ...(isStatic
800
1003
  ? {
@@ -809,14 +1012,20 @@ const layout: RouteHelpers<any, any>["layout"] = (handler, use) => {
809
1012
  (handler as any).$$routePrefix = ctx.namePrefix;
810
1013
  }
811
1014
 
812
- // Run use callback if provided
813
- let result: AllUseItems[] | undefined;
814
- if (use && typeof use === "function") {
815
- result = store.run(namespace, entry, use)?.flat(3);
1015
+ // Merge handler.use defaults with explicit use
1016
+ const handlerUseFn = resolveHandlerUse(handler);
1017
+ const mergedUse = mergeHandlerUse(handlerUseFn, use, "layout");
816
1018
 
817
- invariant(
818
- Array.isArray(result) && result.every((item) => isValidUseItem(item)),
819
- `layout() use() callback must return an array of use items [${namespace}]`,
1019
+ // Run merged use callback if present
1020
+ let result: AllUseItems[] | undefined;
1021
+ if (mergedUse) {
1022
+ result = runAndValidateUseItems(
1023
+ store,
1024
+ namespace,
1025
+ entry,
1026
+ mergedUse,
1027
+ "layout",
1028
+ "use",
820
1029
  );
821
1030
  }
822
1031
 
@@ -858,9 +1067,7 @@ const layout: RouteHelpers<any, any>["layout"] = (handler, use) => {
858
1067
  `Orphan layouts can only be defined inside route or layout > check [${namespace}]`,
859
1068
  );
860
1069
 
861
- // Clear parent pointer for orphan layouts to prevent duplicate processing
862
- entry.parent = null;
863
- parent.layout.push(entry);
1070
+ attachOrphanSibling(parent, entry);
864
1071
  }
865
1072
  }
866
1073
 
@@ -873,33 +1080,15 @@ const layout: RouteHelpers<any, any>["layout"] = (handler, use) => {
873
1080
  } as LayoutItem;
874
1081
  };
875
1082
 
876
- const isValidUseItem = (item: any): item is AllUseItems | undefined | null => {
877
- return (
878
- typeof item === "undefined" ||
879
- item === null ||
880
- (item &&
881
- typeof item === "object" &&
882
- "type" in item &&
883
- [
884
- "layout",
885
- "route",
886
- "middleware",
887
- "revalidate",
888
- "parallel",
889
- "intercept",
890
- "loader",
891
- "loading",
892
- "errorBoundary",
893
- "notFoundBoundary",
894
- "when",
895
- "cache",
896
- "transition",
897
- "include", // For urls() include() helper
898
- ].includes(item.type))
899
- );
900
- };
1083
+ const isValidUseItem = (item: any): item is AllUseItems | undefined | null =>
1084
+ item == null ||
1085
+ (typeof item === "object" &&
1086
+ "type" in item &&
1087
+ ALL_USE_ITEM_TYPES.has(item.type));
901
1088
 
902
- // Global helper exports for direct import from @rangojs/router
1089
+ // DSL helpers exported for direct import from @rangojs/router and for
1090
+ // assembly into the RouteHelpers object in helper-factories.ts. The route-item
1091
+ // types are discriminated by their `type` literal, so the helpers carry no brand.
903
1092
  export {
904
1093
  layout,
905
1094
  cache,
@@ -910,25 +1099,11 @@ export {
910
1099
  when,
911
1100
  errorBoundary,
912
1101
  notFoundBoundary,
913
- loaderFn as loader,
914
- loadingFn as loading,
915
- transitionFn as transition,
916
- };
917
-
918
- const isOrphanLayout = (item: AllUseItems): boolean => {
919
- return (
920
- item.type === "layout" &&
921
- !item.uses?.some((child) => hasRoutesInItem(child))
922
- );
923
- };
924
-
925
- // Internal exports used by helper-factories.ts
926
- export {
927
- routeFn,
928
- loaderFn,
929
- loadingFn,
930
- transitionFn,
931
- hasRoutesInItem,
1102
+ route,
1103
+ loader,
1104
+ loading,
1105
+ transition,
932
1106
  isValidUseItem,
933
- isOrphanLayout,
1107
+ emptySegmentBase,
1108
+ runAndValidateUseItems,
934
1109
  };