@rangojs/router 0.0.0-experimental.dfdb0387 → 0.0.0-experimental.e16b7c00

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 (237) hide show
  1. package/README.md +120 -25
  2. package/dist/bin/rango.js +147 -57
  3. package/dist/vite/index.js +2106 -842
  4. package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  5. package/package.json +13 -8
  6. package/skills/breadcrumbs/SKILL.md +3 -1
  7. package/skills/bundle-analysis/SKILL.md +159 -0
  8. package/skills/cache-guide/SKILL.md +222 -30
  9. package/skills/caching/SKILL.md +188 -8
  10. package/skills/composability/SKILL.md +27 -2
  11. package/skills/document-cache/SKILL.md +78 -55
  12. package/skills/handler-use/SKILL.md +364 -0
  13. package/skills/hooks/SKILL.md +229 -20
  14. package/skills/host-router/SKILL.md +45 -20
  15. package/skills/i18n/SKILL.md +276 -0
  16. package/skills/intercept/SKILL.md +46 -4
  17. package/skills/layout/SKILL.md +28 -7
  18. package/skills/links/SKILL.md +247 -17
  19. package/skills/loader/SKILL.md +219 -9
  20. package/skills/middleware/SKILL.md +47 -12
  21. package/skills/migrate-nextjs/SKILL.md +582 -0
  22. package/skills/migrate-react-router/SKILL.md +769 -0
  23. package/skills/mime-routes/SKILL.md +27 -0
  24. package/skills/observability/SKILL.md +137 -0
  25. package/skills/parallel/SKILL.md +71 -6
  26. package/skills/prerender/SKILL.md +14 -33
  27. package/skills/rango/SKILL.md +236 -22
  28. package/skills/react-compiler/SKILL.md +168 -0
  29. package/skills/response-routes/SKILL.md +66 -9
  30. package/skills/route/SKILL.md +57 -4
  31. package/skills/router-setup/SKILL.md +3 -3
  32. package/skills/server-actions/SKILL.md +751 -0
  33. package/skills/streams-and-websockets/SKILL.md +283 -0
  34. package/skills/typesafety/SKILL.md +319 -27
  35. package/skills/use-cache/SKILL.md +36 -5
  36. package/skills/view-transitions/SKILL.md +294 -0
  37. package/src/__augment-tests__/augment.ts +81 -0
  38. package/src/__augment-tests__/augmented.check.ts +117 -0
  39. package/src/browser/action-coordinator.ts +53 -36
  40. package/src/browser/app-shell.ts +52 -0
  41. package/src/browser/event-controller.ts +86 -70
  42. package/src/browser/history-state.ts +21 -0
  43. package/src/browser/index.ts +3 -3
  44. package/src/browser/navigation-bridge.ts +86 -11
  45. package/src/browser/navigation-client.ts +45 -25
  46. package/src/browser/navigation-store.ts +32 -9
  47. package/src/browser/navigation-transaction.ts +10 -28
  48. package/src/browser/partial-update.ts +61 -28
  49. package/src/browser/prefetch/cache.ts +124 -26
  50. package/src/browser/prefetch/fetch.ts +129 -37
  51. package/src/browser/prefetch/queue.ts +36 -5
  52. package/src/browser/rango-state.ts +53 -13
  53. package/src/browser/react/Link.tsx +18 -13
  54. package/src/browser/react/NavigationProvider.tsx +72 -31
  55. package/src/browser/react/filter-segment-order.ts +51 -7
  56. package/src/browser/react/index.ts +3 -0
  57. package/src/browser/react/location-state-shared.ts +175 -4
  58. package/src/browser/react/location-state.ts +39 -13
  59. package/src/browser/react/use-handle.ts +17 -9
  60. package/src/browser/react/use-navigation.ts +22 -2
  61. package/src/browser/react/use-params.ts +20 -8
  62. package/src/browser/react/use-reverse.ts +106 -0
  63. package/src/browser/react/use-router.ts +22 -2
  64. package/src/browser/react/use-segments.ts +11 -8
  65. package/src/browser/response-adapter.ts +25 -0
  66. package/src/browser/rsc-router.tsx +64 -22
  67. package/src/browser/scroll-restoration.ts +22 -14
  68. package/src/browser/segment-reconciler.ts +10 -14
  69. package/src/browser/segment-structure-assert.ts +2 -2
  70. package/src/browser/server-action-bridge.ts +23 -30
  71. package/src/browser/types.ts +21 -0
  72. package/src/build/collect-fallback-refs.ts +107 -0
  73. package/src/build/generate-manifest.ts +60 -35
  74. package/src/build/generate-route-types.ts +2 -0
  75. package/src/build/index.ts +2 -0
  76. package/src/build/route-trie.ts +52 -25
  77. package/src/build/route-types/codegen.ts +4 -4
  78. package/src/build/route-types/include-resolution.ts +1 -1
  79. package/src/build/route-types/per-module-writer.ts +7 -4
  80. package/src/build/route-types/router-processing.ts +55 -14
  81. package/src/build/route-types/scan-filter.ts +1 -1
  82. package/src/build/route-types/source-scan.ts +118 -0
  83. package/src/build/runtime-discovery.ts +9 -20
  84. package/src/cache/cache-error.ts +104 -0
  85. package/src/cache/cache-policy.ts +95 -1
  86. package/src/cache/cache-runtime.ts +79 -13
  87. package/src/cache/cache-scope.ts +77 -46
  88. package/src/cache/cache-tag.ts +135 -0
  89. package/src/cache/cf/cf-cache-store.ts +1067 -176
  90. package/src/cache/cf/index.ts +4 -1
  91. package/src/cache/document-cache.ts +59 -7
  92. package/src/cache/index.ts +6 -0
  93. package/src/cache/memory-segment-store.ts +158 -14
  94. package/src/cache/tag-invalidation.ts +206 -0
  95. package/src/cache/types.ts +27 -0
  96. package/src/client.rsc.tsx +3 -0
  97. package/src/client.tsx +92 -182
  98. package/src/context-var.ts +5 -5
  99. package/src/decode-loader-results.ts +36 -0
  100. package/src/errors.ts +30 -1
  101. package/src/handle.ts +4 -6
  102. package/src/host/index.ts +2 -2
  103. package/src/host/router.ts +129 -57
  104. package/src/host/types.ts +31 -2
  105. package/src/host/utils.ts +1 -1
  106. package/src/href-client.ts +140 -20
  107. package/src/index.rsc.ts +16 -4
  108. package/src/index.ts +65 -15
  109. package/src/loader-store.ts +500 -0
  110. package/src/loader.rsc.ts +2 -5
  111. package/src/loader.ts +3 -10
  112. package/src/missing-id-error.ts +68 -0
  113. package/src/outlet-context.ts +1 -1
  114. package/src/prerender.ts +4 -4
  115. package/src/response-utils.ts +37 -0
  116. package/src/reverse.ts +65 -36
  117. package/src/route-content-wrapper.tsx +6 -28
  118. package/src/route-definition/dsl-helpers.ts +384 -257
  119. package/src/route-definition/helper-factories.ts +29 -139
  120. package/src/route-definition/helpers-types.ts +100 -28
  121. package/src/route-definition/resolve-handler-use.ts +6 -0
  122. package/src/route-definition/use-item-types.ts +32 -0
  123. package/src/route-types.ts +26 -41
  124. package/src/router/content-negotiation.ts +15 -2
  125. package/src/router/error-handling.ts +1 -1
  126. package/src/router/handler-context.ts +21 -38
  127. package/src/router/intercept-resolution.ts +4 -18
  128. package/src/router/lazy-includes.ts +8 -8
  129. package/src/router/loader-resolution.ts +19 -2
  130. package/src/router/manifest.ts +22 -13
  131. package/src/router/match-api.ts +4 -3
  132. package/src/router/match-handlers.ts +1 -0
  133. package/src/router/match-middleware/cache-lookup.ts +46 -92
  134. package/src/router/match-middleware/cache-store.ts +3 -2
  135. package/src/router/match-result.ts +53 -32
  136. package/src/router/metrics.ts +1 -1
  137. package/src/router/middleware-types.ts +15 -26
  138. package/src/router/middleware.ts +99 -84
  139. package/src/router/pattern-matching.ts +101 -17
  140. package/src/router/prerender-match.ts +3 -1
  141. package/src/router/preview-match.ts +3 -1
  142. package/src/router/request-classification.ts +4 -28
  143. package/src/router/revalidation.ts +58 -2
  144. package/src/router/router-interfaces.ts +45 -28
  145. package/src/router/router-options.ts +25 -1
  146. package/src/router/router-registry.ts +2 -5
  147. package/src/router/segment-resolution/fresh.ts +27 -6
  148. package/src/router/segment-resolution/loader-cache.ts +8 -17
  149. package/src/router/segment-resolution/revalidation.ts +147 -106
  150. package/src/router/segment-resolution/view-transition-default.ts +36 -0
  151. package/src/router/substitute-pattern-params.ts +56 -0
  152. package/src/router/trie-matching.ts +18 -13
  153. package/src/router/types.ts +8 -0
  154. package/src/router/url-params.ts +49 -0
  155. package/src/router.ts +23 -18
  156. package/src/rsc/handler-context.ts +2 -2
  157. package/src/rsc/handler.ts +38 -70
  158. package/src/rsc/helpers.ts +72 -43
  159. package/src/rsc/index.ts +1 -1
  160. package/src/rsc/origin-guard.ts +28 -10
  161. package/src/rsc/progressive-enhancement.ts +4 -0
  162. package/src/rsc/response-route-handler.ts +54 -54
  163. package/src/rsc/rsc-rendering.ts +35 -51
  164. package/src/rsc/runtime-warnings.ts +9 -10
  165. package/src/rsc/server-action.ts +17 -37
  166. package/src/rsc/ssr-setup.ts +16 -0
  167. package/src/rsc/types.ts +8 -2
  168. package/src/search-params.ts +4 -4
  169. package/src/segment-content-promise.ts +67 -0
  170. package/src/segment-loader-promise.ts +122 -0
  171. package/src/segment-system.tsx +132 -116
  172. package/src/serialize.ts +243 -0
  173. package/src/server/context.ts +143 -53
  174. package/src/server/cookie-store.ts +28 -4
  175. package/src/server/request-context.ts +46 -44
  176. package/src/ssr/index.tsx +5 -1
  177. package/src/static-handler.ts +1 -1
  178. package/src/types/cache-types.ts +13 -4
  179. package/src/types/error-types.ts +5 -1
  180. package/src/types/global-namespace.ts +39 -26
  181. package/src/types/handler-context.ts +68 -50
  182. package/src/types/index.ts +1 -0
  183. package/src/types/loader-types.ts +5 -6
  184. package/src/types/request-scope.ts +126 -0
  185. package/src/types/route-entry.ts +11 -0
  186. package/src/types/segments.ts +35 -2
  187. package/src/urls/include-helper.ts +34 -67
  188. package/src/urls/index.ts +0 -3
  189. package/src/urls/path-helper-types.ts +41 -7
  190. package/src/urls/path-helper.ts +17 -52
  191. package/src/urls/pattern-types.ts +36 -19
  192. package/src/urls/response-types.ts +22 -29
  193. package/src/urls/type-extraction.ts +26 -116
  194. package/src/urls/urls-function.ts +1 -5
  195. package/src/use-loader.tsx +413 -42
  196. package/src/vite/debug.ts +185 -0
  197. package/src/vite/discovery/bundle-postprocess.ts +6 -6
  198. package/src/vite/discovery/discover-routers.ts +101 -51
  199. package/src/vite/discovery/discovery-errors.ts +194 -0
  200. package/src/vite/discovery/gate-state.ts +171 -0
  201. package/src/vite/discovery/prerender-collection.ts +67 -26
  202. package/src/vite/discovery/route-types-writer.ts +40 -84
  203. package/src/vite/discovery/self-gen-tracking.ts +27 -1
  204. package/src/vite/discovery/state.ts +33 -0
  205. package/src/vite/discovery/virtual-module-codegen.ts +13 -23
  206. package/src/vite/index.ts +2 -0
  207. package/src/vite/plugin-types.ts +67 -0
  208. package/src/vite/plugins/cjs-to-esm.ts +8 -7
  209. package/src/vite/plugins/client-ref-dedup.ts +16 -0
  210. package/src/vite/plugins/client-ref-hashing.ts +28 -5
  211. package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
  212. package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  213. package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
  214. package/src/vite/plugins/expose-action-id.ts +54 -30
  215. package/src/vite/plugins/expose-id-utils.ts +12 -8
  216. package/src/vite/plugins/expose-ids/export-analysis.ts +100 -20
  217. package/src/vite/plugins/expose-ids/handler-transform.ts +8 -61
  218. package/src/vite/plugins/expose-ids/loader-transform.ts +3 -5
  219. package/src/vite/plugins/expose-ids/router-transform.ts +20 -3
  220. package/src/vite/plugins/expose-internal-ids.ts +496 -486
  221. package/src/vite/plugins/performance-tracks.ts +29 -25
  222. package/src/vite/plugins/use-cache-transform.ts +65 -50
  223. package/src/vite/plugins/version-injector.ts +39 -23
  224. package/src/vite/plugins/version-plugin.ts +59 -2
  225. package/src/vite/plugins/virtual-entries.ts +2 -2
  226. package/src/vite/rango.ts +116 -29
  227. package/src/vite/router-discovery.ts +750 -100
  228. package/src/vite/utils/ast-handler-extract.ts +15 -15
  229. package/src/vite/utils/banner.ts +1 -1
  230. package/src/vite/utils/bundle-analysis.ts +4 -2
  231. package/src/vite/utils/client-chunks.ts +190 -0
  232. package/src/vite/utils/forward-user-plugins.ts +193 -0
  233. package/src/vite/utils/manifest-utils.ts +21 -5
  234. package/src/vite/utils/package-resolution.ts +41 -1
  235. package/src/vite/utils/prerender-utils.ts +21 -6
  236. package/src/vite/utils/shared-utils.ts +107 -26
  237. package/src/browser/action-response-classifier.ts +0 -99
@@ -0,0 +1,243 @@
1
+ /**
2
+ * Wire-type serialization transforms.
3
+ *
4
+ * The type a handler or loader returns on the server is frequently NOT the type
5
+ * a client receives after serialization. These transforms model that boundary so
6
+ * consumer-facing types (e.g. `Rango.PathResponse`) describe the wire value, not
7
+ * the source value.
8
+ *
9
+ * Two serializers, two transforms — they are intentionally NOT interchangeable:
10
+ *
11
+ * - `JsonSerialize<T>` models plain `JSON.stringify` (`path.json()` /
12
+ * `fetch().then(r => r.json())`). Lossy: `Date -> string`, `undefined` /
13
+ * functions / symbols dropped, `Map`/`Set` -> `{}`. `bigint` *throws* (no wire
14
+ * value), so it collapses the whole result to `never`. Honors `toJSON()`.
15
+ * - `FlightSerialize<T>` models React RSC Flight (loaders, RSC props, cache).
16
+ * High fidelity: `Date`/`Map`/`Set`/`bigint`/typed arrays/`Promise` are
17
+ * preserved; ordinary functions and non-global symbols do not cross.
18
+ *
19
+ * ## Overriding (full-transform replacement)
20
+ *
21
+ * Because `Rango.JsonSerialize` / `Rango.FlightSerialize` are type *aliases*, TS
22
+ * cannot let you redefine them directly (aliases don't merge). Instead each alias
23
+ * consults a generic override slot — augment it with a single member that is your
24
+ * complete transform. Delegate to the built-in for the cases you don't change:
25
+ *
26
+ * ```ts
27
+ * declare global {
28
+ * namespace Rango {
29
+ * interface FlightSerializeOverride<T> {
30
+ * app: T extends Money ? number : Rango.FlightSerializeBuiltin<T>;
31
+ * }
32
+ * }
33
+ * }
34
+ * // now Rango.FlightSerialize<Money> is number; everything else is the built-in.
35
+ * ```
36
+ *
37
+ * Provide exactly one member: the slot is read as `Override<T>[keyof Override<T>]`,
38
+ * so multiple members union (and conflict). The built-in recurses through the
39
+ * override-aware alias, so an override applies at every nesting level.
40
+ */
41
+
42
+ import type { ReactNode } from "react";
43
+
44
+ type JsonPrimitive = string | number | boolean | null;
45
+
46
+ type AnyFunction = (...args: never[]) => unknown;
47
+
48
+ // --- JSON ---------------------------------------------------------------------
49
+
50
+ /**
51
+ * Internal marker for a value that makes `JSON.stringify` throw (`bigint`, or a
52
+ * `toJSON()` returning one). Distinct from `never`, which means "omitted":
53
+ * `undefined`/function/symbol-valued keys are dropped, and such array slots
54
+ * become `null`. A throwing value has no valid JSON wire form, so it propagates
55
+ * up through every container and is excluded at the public boundary (`bigint`
56
+ * alone -> `never`; `{ id: bigint }` -> `never`).
57
+ */
58
+ declare const JSON_THROWS: unique symbol;
59
+ type JsonThrows = typeof JSON_THROWS;
60
+
61
+ /** True if union `U` contains the throw marker. */
62
+ type HasThrow<U> = [Extract<U, JsonThrows>] extends [never] ? false : true;
63
+
64
+ /** Map a JSON array/tuple: propagate a throw; else omitted elements become null. */
65
+ type JsonSerializeArray<T extends readonly unknown[]> =
66
+ HasThrow<{ [K in keyof T]: JsonRawResolve<T[K]> }[number]> extends true
67
+ ? JsonThrows
68
+ : {
69
+ [K in keyof T]: [JsonRawResolve<T[K]>] extends [never]
70
+ ? null
71
+ : JsonRawResolve<T[K]>;
72
+ };
73
+
74
+ /** Map a JSON object: propagate a throw; else drop omitted keys. */
75
+ type JsonSerializeObject<T> =
76
+ HasThrow<{ [K in keyof T]: JsonRawResolve<T[K]> }[keyof T]> extends true
77
+ ? JsonThrows
78
+ : {
79
+ [K in keyof T as [JsonRawResolve<T[K]>] extends [never]
80
+ ? never
81
+ : K]: JsonRawResolve<T[K]>;
82
+ };
83
+
84
+ /**
85
+ * Built-in JSON rules, *raw* (may yield the throw marker). Honors `toJSON()` (so
86
+ * `Date -> string` and any class with `toJSON()` serialize correctly), preserves
87
+ * JSON primitives and literals, omits functions / symbols / `undefined`,
88
+ * collapses `Map`/`Set` to `{}`, and marks `bigint` as throwing. Recurses through
89
+ * the override-aware resolver, so registered overrides apply at every level.
90
+ */
91
+ type JsonSerializeBuiltinRaw<T> = T extends {
92
+ toJSON(...args: never[]): infer R;
93
+ }
94
+ ? JsonRawResolve<R>
95
+ : T extends JsonPrimitive
96
+ ? T
97
+ : T extends bigint
98
+ ? JsonThrows
99
+ : T extends AnyFunction
100
+ ? never
101
+ : T extends symbol
102
+ ? never
103
+ : T extends undefined
104
+ ? never
105
+ : T extends readonly unknown[]
106
+ ? JsonSerializeArray<T>
107
+ : T extends ReadonlyMap<unknown, unknown>
108
+ ? {}
109
+ : T extends ReadonlySet<unknown>
110
+ ? {}
111
+ : T extends object
112
+ ? JsonSerializeObject<T>
113
+ : never;
114
+
115
+ /** Override-aware raw JSON resolution (the recursion entry). */
116
+ type JsonRawResolve<T> = [keyof Rango.JsonSerializeOverride<T>] extends [never]
117
+ ? JsonSerializeBuiltinRaw<T>
118
+ : Rango.JsonSerializeOverride<T>[keyof Rango.JsonSerializeOverride<T>];
119
+
120
+ /**
121
+ * Model the result of round-tripping a value through `JSON.stringify` /
122
+ * `JSON.parse`. A registered `Rango.JsonSerializeOverride` replaces the transform
123
+ * wholesale; otherwise the built-in rules apply. Throwing values collapse to
124
+ * `never`.
125
+ */
126
+ export type JsonSerialize<T> = Exclude<JsonRawResolve<T>, JsonThrows>;
127
+
128
+ // --- Flight -------------------------------------------------------------------
129
+
130
+ /**
131
+ * Built-in Flight rules. Mirrors React's `ReactClientValue` contract: primitives
132
+ * including `bigint`, `undefined`, `null`, symbols, `Date`, `ArrayBuffer` and
133
+ * typed-array views, `Map`, `Set`, `FormData`, `Blob`, `Promise`,
134
+ * `ReadableStream`, and (async) iterables are preserved; ordinary functions
135
+ * resolve to `never`. JSX (`ReactNode`, and the async-node union
136
+ * `ReactNode | Promise<ReactNode>`) is preserved as-is via a non-distributive
137
+ * leaf, so handle/loader returns that carry JSX round-trip unchanged. Recurses
138
+ * through the override-aware `FlightSerialize`.
139
+ *
140
+ * The source of truth is React's own contract, which is intentionally NOT
141
+ * semver-stable across RSC framework APIs — this tracks the React version Rango
142
+ * pins. See:
143
+ * https://react.dev/reference/rsc/use-client#serializable-types-returned-by-server-components
144
+ *
145
+ * Type-level limitations (not detectable structurally, so not modeled): class
146
+ * instances and null-prototype objects are rejected by React at runtime but pass
147
+ * here as their structural shape; non-global symbols are rejected at runtime but
148
+ * `symbol` is preserved here; Server Functions would need an override to be
149
+ * distinguished from ordinary functions (which resolve to `never`).
150
+ */
151
+ type FlightSerializeBuiltinRaw<T> = [T] extends [ReactNode | Promise<ReactNode>]
152
+ ? T
153
+ : T extends string | number | boolean | bigint | symbol | null | undefined
154
+ ? T
155
+ : T extends AnyFunction
156
+ ? never
157
+ : T extends Date
158
+ ? Date
159
+ : T extends ArrayBuffer
160
+ ? ArrayBuffer
161
+ : T extends ArrayBufferView
162
+ ? T
163
+ : T extends FormData
164
+ ? FormData
165
+ : T extends Blob
166
+ ? Blob
167
+ : T extends Map<infer K, infer V>
168
+ ? Map<FlightSerialize<K>, FlightSerialize<V>>
169
+ : T extends Set<infer V>
170
+ ? Set<FlightSerialize<V>>
171
+ : T extends Promise<infer V>
172
+ ? Promise<FlightSerialize<V>>
173
+ : T extends ReadableStream<infer V>
174
+ ? ReadableStream<FlightSerialize<V>>
175
+ : T extends readonly unknown[]
176
+ ? { [K in keyof T]: FlightSerialize<T[K]> }
177
+ : T extends AsyncIterable<infer V>
178
+ ? AsyncIterable<FlightSerialize<V>>
179
+ : T extends Iterable<infer V>
180
+ ? Iterable<FlightSerialize<V>>
181
+ : T extends object
182
+ ? { [K in keyof T]: FlightSerialize<T[K]> }
183
+ : never;
184
+
185
+ /**
186
+ * Model React RSC Flight serialization. A registered `Rango.FlightSerializeOverride`
187
+ * replaces the transform wholesale; otherwise the built-in rules apply.
188
+ */
189
+ export type FlightSerialize<T> = [
190
+ keyof Rango.FlightSerializeOverride<T>,
191
+ ] extends [never]
192
+ ? FlightSerializeBuiltinRaw<T>
193
+ : Rango.FlightSerializeOverride<T>[keyof Rango.FlightSerializeOverride<T>];
194
+
195
+ // Module-scoped aliases so the ambient `Rango.*` members below can reference the
196
+ // module-level transforms without the global namespace shadowing the names.
197
+ type GlobalJsonSerialize<T> = JsonSerialize<T>;
198
+ type GlobalJsonSerializeBuiltin<T> = JsonSerializeBuiltinRaw<T>;
199
+ type GlobalFlightSerialize<T> = FlightSerialize<T>;
200
+ type GlobalFlightSerializeBuiltin<T> = FlightSerializeBuiltinRaw<T>;
201
+
202
+ /**
203
+ * Ambient serialization transforms and their override slots on the `Rango`
204
+ * namespace. Available with no import wherever the router's types are in scope,
205
+ * alongside `Rango.Path` / `Rango.PathResponse`.
206
+ *
207
+ * `Rango.JsonSerialize` is what `Rango.PathResponse` applies; `Rango.FlightSerialize`
208
+ * is exposed for RSC/loader/cache wire types and must NOT be used for `path.json()`.
209
+ * `Rango.JsonSerializeBuiltin` / `Rango.FlightSerializeBuiltin` are the defaults,
210
+ * exported so an override can delegate to them.
211
+ */
212
+ declare global {
213
+ namespace Rango {
214
+ /**
215
+ * Full-transform override slot for `Rango.JsonSerialize`. Empty by default;
216
+ * augment with one member that is your complete transform (delegate to
217
+ * `Rango.JsonSerializeBuiltin<T>` for the cases you don't change).
218
+ */
219
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
220
+ interface JsonSerializeOverride<T> {}
221
+
222
+ /**
223
+ * Full-transform override slot for `Rango.FlightSerialize`. Empty by default;
224
+ * augment with one member that is your complete transform (delegate to
225
+ * `Rango.FlightSerializeBuiltin<T>` for the cases you don't change).
226
+ */
227
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
228
+ interface FlightSerializeOverride<T> {}
229
+
230
+ /** Wire type after `JSON.stringify` (`path.json()` / `fetch().json()`). */
231
+ type JsonSerialize<T> = GlobalJsonSerialize<T>;
232
+ /**
233
+ * Built-in `JsonSerialize` rules, for an override to delegate to. Raw: a
234
+ * `bigint`-bearing type yields the internal throw marker here, which
235
+ * `Rango.JsonSerialize` excludes to `never` at the boundary.
236
+ */
237
+ type JsonSerializeBuiltin<T> = GlobalJsonSerializeBuiltin<T>;
238
+ /** Wire type after RSC Flight serialization (loaders / RSC props / cache). */
239
+ type FlightSerialize<T> = GlobalFlightSerialize<T>;
240
+ /** Built-in `FlightSerialize` rules, for an override to delegate to. */
241
+ type FlightSerializeBuiltin<T> = GlobalFlightSerializeBuiltin<T>;
242
+ }
243
+ }
@@ -10,7 +10,7 @@ import type {
10
10
  ShouldRevalidateFn,
11
11
  TransitionConfig,
12
12
  } from "../types";
13
- import { invariant } from "../errors";
13
+ import { invariant, DslContextError } from "../errors";
14
14
  import type { DefaultRouteName } from "../types/global-namespace.js";
15
15
 
16
16
  // ============================================================================
@@ -40,7 +40,7 @@ export interface MetricsStore {
40
40
  metrics: PerformanceMetric[];
41
41
  }
42
42
  // ============================================================================
43
- // RSC Router Context
43
+ // Rango Context
44
44
  // ============================================================================
45
45
 
46
46
  /**
@@ -71,6 +71,10 @@ export type EntryPropCommon = {
71
71
  };
72
72
 
73
73
  /**
74
+ * Attachments resolved by walking the parent chain, not owned by the entry:
75
+ * middleware composes downward; revalidate and the error/notFound boundaries are
76
+ * resolved by nearest-ancestor lookup. Inherited, not a single execution chain.
77
+ *
74
78
  * @internal This type is an implementation detail and may change without notice.
75
79
  */
76
80
  export type EntryPropDatas = {
@@ -80,6 +84,16 @@ export type EntryPropDatas = {
80
84
  notFoundBoundary: (ReactNode | NotFoundBoundaryHandler)[];
81
85
  };
82
86
 
87
+ /**
88
+ * Render-time presentation fields shared by every entry variant.
89
+ *
90
+ * @internal This type is an implementation detail and may change without notice.
91
+ */
92
+ export type EntryPropRender = {
93
+ loading?: ReactNode | false;
94
+ transition?: TransitionConfig;
95
+ };
96
+
83
97
  /**
84
98
  * Loader entry stored in EntryData
85
99
  * Contains the loader definition and its revalidation rules
@@ -158,11 +172,9 @@ export type InterceptEntry = {
158
172
  };
159
173
 
160
174
  export interface ParallelEntryData
161
- extends EntryPropCommon, EntryPropDatas, EntryPropSegments {
175
+ extends EntryPropCommon, EntryPropDatas, EntryPropSegments, EntryPropRender {
162
176
  type: "parallel";
163
177
  handler: Record<`@${string}`, Handler<any, any, any> | ReactNode>;
164
- loading?: ReactNode | false;
165
- transition?: TransitionConfig;
166
178
  /** Set when any parallel slot is a Static definition */
167
179
  isStaticPrerender?: true;
168
180
  /** Per-slot static handler $$ids for build-time store lookup */
@@ -171,6 +183,13 @@ export interface ParallelEntryData
171
183
 
172
184
  export type ParallelEntries = Partial<Record<`@${string}`, ParallelEntryData>>;
173
185
 
186
+ /**
187
+ * This entry's own structural children plus its owned loaders. `loader` lives
188
+ * here (not in EntryPropDatas) because loaders are owned by the entry, not
189
+ * inherited from ancestors.
190
+ *
191
+ * @internal This type is an implementation detail and may change without notice.
192
+ */
174
193
  export type EntryPropSegments = {
175
194
  loader: LoaderEntry[];
176
195
  layout: EntryData[];
@@ -182,8 +201,6 @@ export type EntryData =
182
201
  | ({
183
202
  type: "route";
184
203
  handler: Handler<any, any, any>;
185
- loading?: ReactNode | false;
186
- transition?: TransitionConfig;
187
204
  /** URL pattern for this route (used by path() in urls()) */
188
205
  pattern?: string;
189
206
  /** Set when handler is a Prerender definition */
@@ -205,29 +222,28 @@ export type EntryData =
205
222
  responseType?: string;
206
223
  } & EntryPropCommon &
207
224
  EntryPropDatas &
208
- EntryPropSegments)
225
+ EntryPropSegments &
226
+ EntryPropRender)
209
227
  | ({
210
228
  type: "layout";
211
229
  handler: ReactNode | Handler<any, any, any>;
212
- loading?: ReactNode | false;
213
- transition?: TransitionConfig;
214
230
  /** Set when handler is a Static definition (build-time only) */
215
231
  isStaticPrerender?: true;
216
232
  /** Static handler $$id for build-time store lookup */
217
233
  staticHandlerId?: string;
218
234
  } & EntryPropCommon &
219
235
  EntryPropDatas &
220
- EntryPropSegments)
236
+ EntryPropSegments &
237
+ EntryPropRender)
221
238
  | ParallelEntryData
222
239
  | ({
223
240
  type: "cache";
224
241
  /** Cache entries create cache boundaries and render like layouts (with Outlet) */
225
242
  handler: ReactNode | Handler<any, any, any>;
226
- loading?: ReactNode | false;
227
- transition?: TransitionConfig;
228
243
  } & EntryPropCommon &
229
244
  EntryPropDatas &
230
- EntryPropSegments);
245
+ EntryPropSegments &
246
+ EntryPropRender);
231
247
 
232
248
  /**
233
249
  * Tracked include info for build-time manifest generation
@@ -280,6 +296,22 @@ interface HelperContext {
280
296
  /** True when resolving handlers inside a cache() DSL boundary.
281
297
  * Read by ctx.get() to guard non-cacheable variable reads. */
282
298
  insideCacheScope?: boolean;
299
+ /**
300
+ * Include scope string applied to direct-descendant shortCodes.
301
+ *
302
+ * Each `include(...)` call allocates a sibling-positional token like `I0`,
303
+ * `I1` from its parent's include counter and stores the composed scope
304
+ * (`${parentScope}I${idx}`) in its lazyContext. When the include's handler
305
+ * evaluates lazily, the store's `includeScope` is set from that context so
306
+ * every direct-descendant shortCode is generated as
307
+ * `${parent.shortCode}${includeScope}${prefix}${index}` — preventing
308
+ * collisions with siblings declared outside the include.
309
+ *
310
+ * The scope is NOT propagated through `store.run(...)`, so layouts /
311
+ * parallels / caches inside the include absorb the scope into their own
312
+ * shortCodes and their children start fresh.
313
+ */
314
+ includeScope?: string;
283
315
  }
284
316
  // Use a global symbol key so the AsyncLocalStorage instance survives HMR
285
317
  // module re-evaluation. Without this, Vite's RSC module runner may create
@@ -287,10 +319,28 @@ interface HelperContext {
287
319
  // hold references to the old instance — causing getStore() to return
288
320
  // undefined even inside a run() callback.
289
321
  const RSC_CONTEXT_KEY = Symbol.for("rangojs-router:rsc-context");
290
- export const RSCRouterContext: AsyncLocalStorage<HelperContext> = ((
322
+ export const RangoContext: AsyncLocalStorage<HelperContext> = ((
291
323
  globalThis as any
292
324
  )[RSC_CONTEXT_KEY] ??= new AsyncLocalStorage<HelperContext>());
293
325
 
326
+ /** shortCode prefix letter per entry type (e.g. "L0", "R2", "M1C0"). */
327
+ const SHORT_CODE_PREFIX: Record<
328
+ "layout" | "parallel" | "route" | "loader" | "cache",
329
+ string
330
+ > = {
331
+ layout: "L",
332
+ parallel: "P",
333
+ route: "R",
334
+ loader: "D",
335
+ cache: "C",
336
+ };
337
+
338
+ /** Post-increment a named per-store counter, returning the prior value. */
339
+ function bumpCounter(store: HelperContext, key: string): number {
340
+ store.counters[key] ??= 0;
341
+ return store.counters[key]++;
342
+ }
343
+
294
344
  export const getContext = (): {
295
345
  context: AsyncLocalStorage<HelperContext>;
296
346
  getStore: () => HelperContext;
@@ -314,12 +364,12 @@ export const getContext = (): {
314
364
  callback: (...args: any[]) => T,
315
365
  ) => T;
316
366
  } => {
317
- const context = RSCRouterContext;
367
+ const context = RangoContext;
318
368
 
319
369
  return {
320
370
  context,
321
371
  getOrCreateStore: (forRoute?: string): HelperContext => {
322
- let store = RSCRouterContext.getStore();
372
+ let store = RangoContext.getStore();
323
373
  if (!store) {
324
374
  store = {
325
375
  manifest: new Map<string, EntryData>(),
@@ -339,7 +389,7 @@ export const getContext = (): {
339
389
  const store = context.getStore();
340
390
  if (!store) {
341
391
  throw new Error(
342
- "RSC Router context store is not available. Make sure to run within RSC Router context.",
392
+ "Rango context store is not available. Make sure to run within Rango context.",
343
393
  );
344
394
  }
345
395
  return store;
@@ -356,48 +406,36 @@ export const getContext = (): {
356
406
  type: (string & {}) | "layout" | "parallel" | "middleware" | "revalidate",
357
407
  ) => {
358
408
  const store = context.getStore();
359
- invariant(store, "No context RSCRouterContext available");
360
- store.counters[type] ??= 0;
361
- const index = store.counters[type];
362
- store.counters[type] = index + 1;
363
- return `$${type}.${index}`;
409
+ invariant(store, "No context RangoContext available");
410
+ return `$${type}.${bumpCounter(store, type)}`;
364
411
  },
365
412
  getShortCode: (
366
413
  type: "layout" | "parallel" | "route" | "loader" | "cache",
367
414
  ) => {
368
415
  const store = context.getStore();
369
- invariant(store, "No context RSCRouterContext available");
416
+ invariant(store, "No context RangoContext available");
370
417
 
371
418
  const parent = store.parent;
372
- const prefix =
373
- type === "layout"
374
- ? "L"
375
- : type === "parallel"
376
- ? "P"
377
- : type === "loader"
378
- ? "D"
379
- : type === "cache"
380
- ? "C"
381
- : "R";
419
+ const prefix = SHORT_CODE_PREFIX[type];
382
420
  const mountPrefix =
383
421
  store.mountIndex !== undefined ? `M${store.mountIndex}` : "";
384
422
 
423
+ const includeScope = store.includeScope ?? "";
424
+
385
425
  if (!parent) {
386
426
  // Root entry: prefix with mount index and use mount-scoped counter
387
427
  const counterKey = mountPrefix
388
428
  ? `${mountPrefix}_root_${type}`
389
429
  : `root_${type}`;
390
- store.counters[counterKey] ??= 0;
391
- const index = store.counters[counterKey];
392
- store.counters[counterKey] = index + 1;
393
- return `${mountPrefix}${prefix}${index}`;
430
+ return `${mountPrefix}${prefix}${bumpCounter(store, counterKey)}`;
394
431
  } else {
395
- // Child entry: use parent-scoped counter (parent already has M prefix)
396
- const counterKey = `${parent.shortCode}_${type}`;
397
- store.counters[counterKey] ??= 0;
398
- const index = store.counters[counterKey];
399
- store.counters[counterKey] = index + 1;
400
- return `${parent.shortCode}${prefix}${index}`;
432
+ // Child entry: use parent-scoped counter with includeScope appended.
433
+ // When we're evaluating a lazy include's direct children, includeScope
434
+ // is a per-include token like "I0" / "I1I0" that partitions the
435
+ // parent's counter namespace so routes inside one include cannot
436
+ // collide with siblings declared outside it.
437
+ const counterKey = `${parent.shortCode}${includeScope}_${type}`;
438
+ return `${parent.shortCode}${includeScope}${prefix}${bumpCounter(store, counterKey)}`;
401
439
  }
402
440
  },
403
441
  runWithStore: <T>(
@@ -424,6 +462,7 @@ export const getContext = (): {
424
462
  rootScoped: store.rootScoped,
425
463
  trackedIncludes: store.trackedIncludes,
426
464
  cacheProfiles: store.cacheProfiles,
465
+ includeScope: store.includeScope,
427
466
  },
428
467
  callback,
429
468
  );
@@ -470,6 +509,31 @@ export const getContext = (): {
470
509
  };
471
510
  };
472
511
 
512
+ /**
513
+ * Acquire the active DSL build context, throwing `message` if a helper was
514
+ * called outside a urls()/map() builder. Returns the store API and the live
515
+ * HelperContext so callers avoid a second getContext() lookup.
516
+ */
517
+ export function requireDslContext(message: string): {
518
+ store: ReturnType<typeof getContext>;
519
+ ctx: HelperContext;
520
+ } {
521
+ const store = getContext();
522
+ const ctx = store.context.getStore();
523
+ if (!ctx) {
524
+ // The only reason the store is absent here is that a route-definition helper
525
+ // ran with no active RangoContext — i.e. outside a urls()/map() builder.
526
+ // Record that as the cause so the throw is self-explanatory, not a bare
527
+ // "must be called inside urls()" with no indication of the mechanism.
528
+ throw new DslContextError(message, {
529
+ cause:
530
+ "RangoContext store is undefined: a route-definition helper was called " +
531
+ "outside an active urls()/map() builder.",
532
+ });
533
+ }
534
+ return { store, ctx };
535
+ }
536
+
473
537
  /**
474
538
  * Run a callback with specific URL and name prefixes
475
539
  * Used by include() to apply prefixes to nested patterns
@@ -479,7 +543,7 @@ export function runWithPrefixes<T>(
479
543
  namePrefix: string | undefined,
480
544
  callback: () => T,
481
545
  ): T {
482
- const store = RSCRouterContext.getStore();
546
+ const store = RangoContext.getStore();
483
547
  if (!store) {
484
548
  throw new Error("runWithPrefixes must be called within router context");
485
549
  }
@@ -524,7 +588,7 @@ export function runWithPrefixes<T>(
524
588
  ? (store.rootScoped ?? false)
525
589
  : store.rootScoped;
526
590
 
527
- return RSCRouterContext.run(
591
+ return RangoContext.run(
528
592
  {
529
593
  ...store,
530
594
  urlPrefix: combinedUrlPrefix,
@@ -539,7 +603,7 @@ export function runWithPrefixes<T>(
539
603
  * Get current URL prefix from context
540
604
  */
541
605
  export function getUrlPrefix(): string {
542
- const store = RSCRouterContext.getStore();
606
+ const store = RangoContext.getStore();
543
607
  return store?.urlPrefix || "";
544
608
  }
545
609
 
@@ -547,7 +611,7 @@ export function getUrlPrefix(): string {
547
611
  * Get current name prefix from context
548
612
  */
549
613
  export function getNamePrefix(): string | undefined {
550
- const store = RSCRouterContext.getStore();
614
+ const store = RangoContext.getStore();
551
615
  return store?.namePrefix;
552
616
  }
553
617
 
@@ -556,7 +620,7 @@ export function getNamePrefix(): string | undefined {
556
620
  * Returns true at root or inside { name: "" } includes, false inside named includes.
557
621
  */
558
622
  export function getRootScoped(): boolean {
559
- const store = RSCRouterContext.getStore();
623
+ const store = RangoContext.getStore();
560
624
  return store?.rootScoped ?? true;
561
625
  }
562
626
 
@@ -653,7 +717,7 @@ export function getParallelSlotCount(
653
717
  * ```
654
718
  */
655
719
  export function track(label: string, depth?: number): () => void {
656
- const store = RSCRouterContext.getStore();
720
+ const store = RangoContext.getStore();
657
721
 
658
722
  // No-op if context unavailable or metrics not enabled
659
723
  if (!store?.metrics?.enabled) {
@@ -676,25 +740,40 @@ export function track(label: string, depth?: number): () => void {
676
740
 
677
741
  /**
678
742
  * Separate ALS for tracking loader execution scope.
679
- * Uses a dedicated ALS (not RSCRouterContext) to avoid issues with
680
- * nested RSCRouterContext.run() calls in Vite's module runner.
743
+ * Uses a dedicated ALS (not RangoContext) to avoid issues with
744
+ * nested RangoContext.run() calls in Vite's module runner.
681
745
  */
682
746
  const LOADER_SCOPE_KEY = Symbol.for("rangojs-router:loader-scope");
683
747
  const loaderScopeALS: AsyncLocalStorage<{ active: true }> = ((
684
748
  globalThis as any
685
749
  )[LOADER_SCOPE_KEY] ??= new AsyncLocalStorage<{ active: true }>());
686
750
 
751
+ // Purity-only scope: marks that a loader FUNCTION BODY is executing, regardless
752
+ // of how the loader was invoked (DSL via runInsideLoaderScope, or handler-
753
+ // invoked via ctx.use). Consulted ONLY by isInsideCacheScope() to exempt
754
+ // request-scoped reads. It deliberately does NOT affect isInsideLoaderScope(),
755
+ // so rendered()/barrier/deadlock gating (which must distinguish DSL from
756
+ // handler-invoked loaders) is unchanged.
757
+ const LOADER_BODY_SCOPE_KEY = Symbol.for("rangojs-router:loader-body-scope");
758
+ const loaderBodyScopeALS: AsyncLocalStorage<{ active: true }> = ((
759
+ globalThis as any
760
+ )[LOADER_BODY_SCOPE_KEY] ??= new AsyncLocalStorage<{ active: true }>());
761
+
687
762
  /**
688
763
  * Check if the current execution is inside a cache() DSL boundary.
689
764
  * Returns false inside loader execution — loaders are always fresh
690
765
  * (never cached), so non-cacheable reads are safe.
691
766
  */
692
767
  export function isInsideCacheScope(): boolean {
693
- if (RSCRouterContext.getStore()?.insideCacheScope !== true) return false;
768
+ if (RangoContext.getStore()?.insideCacheScope !== true) return false;
694
769
  // Loaders are always fresh — even inside a cache() boundary, the loader
695
770
  // function re-executes on every request. Skip the guard when running
696
771
  // inside a loader.
697
772
  if (loaderScopeALS.getStore()?.active) return false;
773
+ // Also exempt handler-invoked loaders: their bodies run in a loader-body
774
+ // scope (not the DSL loader scope above), so request-scoped reads inside any
775
+ // loader — however invoked — are safe (loaders always re-run fresh).
776
+ if (loaderBodyScopeALS.getStore()?.active) return false;
698
777
  return true;
699
778
  }
700
779
 
@@ -715,3 +794,14 @@ export function isInsideLoaderScope(): boolean {
715
794
  export function runInsideLoaderScope<T>(fn: () => T): T {
716
795
  return loaderScopeALS.run({ active: true }, fn);
717
796
  }
797
+
798
+ /**
799
+ * Run `fn` inside a loader BODY scope. Marks loader-function execution for the
800
+ * cache-purity guard only (isInsideCacheScope), WITHOUT affecting
801
+ * isInsideLoaderScope()/rendered() gating. Applied to every loader body (DSL
802
+ * and handler-invoked via ctx.use) so request-scoped reads inside a loader
803
+ * never trip the cache-scope guards — loaders always run fresh.
804
+ */
805
+ export function runInsideLoaderBodyScope<T>(fn: () => T): T {
806
+ return loaderBodyScopeALS.run({ active: true }, fn);
807
+ }