@rangojs/router 0.0.0-experimental.8a4d0430 → 0.0.0-experimental.8bcfea43

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 (174) hide show
  1. package/AGENTS.md +4 -0
  2. package/README.md +126 -38
  3. package/dist/bin/rango.js +138 -50
  4. package/dist/vite/index.js +1171 -461
  5. package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  6. package/package.json +19 -16
  7. package/skills/breadcrumbs/SKILL.md +3 -1
  8. package/skills/cache-guide/SKILL.md +32 -0
  9. package/skills/caching/SKILL.md +45 -4
  10. package/skills/handler-use/SKILL.md +362 -0
  11. package/skills/hooks/SKILL.md +28 -20
  12. package/skills/intercept/SKILL.md +20 -0
  13. package/skills/layout/SKILL.md +22 -0
  14. package/skills/links/SKILL.md +91 -17
  15. package/skills/loader/SKILL.md +88 -45
  16. package/skills/middleware/SKILL.md +34 -3
  17. package/skills/migrate-nextjs/SKILL.md +560 -0
  18. package/skills/migrate-react-router/SKILL.md +765 -0
  19. package/skills/parallel/SKILL.md +185 -0
  20. package/skills/prerender/SKILL.md +110 -68
  21. package/skills/rango/SKILL.md +24 -22
  22. package/skills/response-routes/SKILL.md +8 -0
  23. package/skills/route/SKILL.md +55 -0
  24. package/skills/router-setup/SKILL.md +87 -2
  25. package/skills/streams-and-websockets/SKILL.md +283 -0
  26. package/skills/typesafety/SKILL.md +13 -1
  27. package/src/__internal.ts +1 -1
  28. package/src/browser/app-shell.ts +52 -0
  29. package/src/browser/app-version.ts +14 -0
  30. package/src/browser/event-controller.ts +5 -0
  31. package/src/browser/navigation-bridge.ts +90 -16
  32. package/src/browser/navigation-client.ts +167 -59
  33. package/src/browser/navigation-store.ts +68 -9
  34. package/src/browser/navigation-transaction.ts +11 -9
  35. package/src/browser/partial-update.ts +113 -17
  36. package/src/browser/prefetch/cache.ts +184 -16
  37. package/src/browser/prefetch/fetch.ts +180 -33
  38. package/src/browser/prefetch/policy.ts +6 -0
  39. package/src/browser/prefetch/queue.ts +123 -20
  40. package/src/browser/prefetch/resource-ready.ts +77 -0
  41. package/src/browser/rango-state.ts +53 -13
  42. package/src/browser/react/Link.tsx +81 -9
  43. package/src/browser/react/NavigationProvider.tsx +89 -14
  44. package/src/browser/react/context.ts +7 -2
  45. package/src/browser/react/use-handle.ts +9 -58
  46. package/src/browser/react/use-navigation.ts +22 -2
  47. package/src/browser/react/use-params.ts +11 -1
  48. package/src/browser/react/use-router.ts +29 -9
  49. package/src/browser/rsc-router.tsx +168 -65
  50. package/src/browser/scroll-restoration.ts +41 -42
  51. package/src/browser/segment-reconciler.ts +36 -9
  52. package/src/browser/server-action-bridge.ts +8 -6
  53. package/src/browser/types.ts +49 -5
  54. package/src/build/generate-manifest.ts +6 -6
  55. package/src/build/generate-route-types.ts +3 -0
  56. package/src/build/route-trie.ts +50 -24
  57. package/src/build/route-types/include-resolution.ts +8 -1
  58. package/src/build/route-types/router-processing.ts +223 -74
  59. package/src/build/route-types/scan-filter.ts +8 -1
  60. package/src/cache/cache-runtime.ts +15 -11
  61. package/src/cache/cache-scope.ts +48 -7
  62. package/src/cache/cf/cf-cache-store.ts +455 -15
  63. package/src/cache/cf/index.ts +5 -1
  64. package/src/cache/document-cache.ts +17 -7
  65. package/src/cache/index.ts +1 -0
  66. package/src/cache/taint.ts +55 -0
  67. package/src/client.tsx +84 -230
  68. package/src/context-var.ts +72 -2
  69. package/src/debug.ts +2 -2
  70. package/src/handle.ts +40 -0
  71. package/src/index.rsc.ts +6 -1
  72. package/src/index.ts +49 -6
  73. package/src/outlet-context.ts +1 -1
  74. package/src/prerender/store.ts +5 -4
  75. package/src/prerender.ts +138 -77
  76. package/src/response-utils.ts +28 -0
  77. package/src/reverse.ts +27 -2
  78. package/src/route-definition/dsl-helpers.ts +240 -40
  79. package/src/route-definition/helpers-types.ts +67 -19
  80. package/src/route-definition/index.ts +3 -0
  81. package/src/route-definition/redirect.ts +11 -3
  82. package/src/route-definition/resolve-handler-use.ts +155 -0
  83. package/src/route-map-builder.ts +7 -1
  84. package/src/route-types.ts +18 -0
  85. package/src/router/content-negotiation.ts +100 -1
  86. package/src/router/find-match.ts +4 -2
  87. package/src/router/handler-context.ts +101 -25
  88. package/src/router/intercept-resolution.ts +11 -4
  89. package/src/router/lazy-includes.ts +10 -7
  90. package/src/router/loader-resolution.ts +159 -21
  91. package/src/router/logging.ts +5 -2
  92. package/src/router/manifest.ts +31 -16
  93. package/src/router/match-api.ts +127 -192
  94. package/src/router/match-middleware/background-revalidation.ts +30 -2
  95. package/src/router/match-middleware/cache-lookup.ts +94 -17
  96. package/src/router/match-middleware/cache-store.ts +53 -10
  97. package/src/router/match-middleware/intercept-resolution.ts +9 -7
  98. package/src/router/match-middleware/segment-resolution.ts +61 -5
  99. package/src/router/match-result.ts +104 -10
  100. package/src/router/metrics.ts +6 -1
  101. package/src/router/middleware-types.ts +8 -30
  102. package/src/router/middleware.ts +36 -10
  103. package/src/router/navigation-snapshot.ts +182 -0
  104. package/src/router/pattern-matching.ts +60 -9
  105. package/src/router/prerender-match.ts +110 -10
  106. package/src/router/preview-match.ts +30 -102
  107. package/src/router/request-classification.ts +310 -0
  108. package/src/router/route-snapshot.ts +245 -0
  109. package/src/router/router-context.ts +6 -1
  110. package/src/router/router-interfaces.ts +36 -4
  111. package/src/router/router-options.ts +37 -11
  112. package/src/router/segment-resolution/fresh.ts +198 -20
  113. package/src/router/segment-resolution/helpers.ts +29 -24
  114. package/src/router/segment-resolution/loader-cache.ts +1 -0
  115. package/src/router/segment-resolution/revalidation.ts +438 -300
  116. package/src/router/segment-wrappers.ts +2 -0
  117. package/src/router/trie-matching.ts +10 -4
  118. package/src/router/types.ts +1 -0
  119. package/src/router/url-params.ts +49 -0
  120. package/src/router.ts +60 -8
  121. package/src/rsc/handler.ts +478 -374
  122. package/src/rsc/helpers.ts +69 -41
  123. package/src/rsc/loader-fetch.ts +23 -3
  124. package/src/rsc/manifest-init.ts +5 -1
  125. package/src/rsc/progressive-enhancement.ts +16 -2
  126. package/src/rsc/response-route-handler.ts +14 -1
  127. package/src/rsc/rsc-rendering.ts +19 -1
  128. package/src/rsc/server-action.ts +10 -0
  129. package/src/rsc/ssr-setup.ts +2 -2
  130. package/src/rsc/types.ts +9 -1
  131. package/src/segment-content-promise.ts +67 -0
  132. package/src/segment-loader-promise.ts +122 -0
  133. package/src/segment-system.tsx +109 -23
  134. package/src/server/context.ts +166 -17
  135. package/src/server/handle-store.ts +19 -0
  136. package/src/server/loader-registry.ts +9 -8
  137. package/src/server/request-context.ts +194 -60
  138. package/src/ssr/index.tsx +4 -0
  139. package/src/static-handler.ts +18 -6
  140. package/src/types/cache-types.ts +4 -4
  141. package/src/types/handler-context.ts +137 -65
  142. package/src/types/loader-types.ts +41 -15
  143. package/src/types/request-scope.ts +126 -0
  144. package/src/types/route-entry.ts +19 -1
  145. package/src/types/segments.ts +2 -0
  146. package/src/urls/include-helper.ts +24 -14
  147. package/src/urls/path-helper-types.ts +39 -6
  148. package/src/urls/path-helper.ts +48 -13
  149. package/src/urls/pattern-types.ts +12 -0
  150. package/src/urls/response-types.ts +18 -16
  151. package/src/use-loader.tsx +77 -5
  152. package/src/vite/debug.ts +55 -0
  153. package/src/vite/discovery/bundle-postprocess.ts +30 -33
  154. package/src/vite/discovery/discover-routers.ts +5 -1
  155. package/src/vite/discovery/prerender-collection.ts +128 -74
  156. package/src/vite/discovery/state.ts +13 -6
  157. package/src/vite/index.ts +4 -0
  158. package/src/vite/plugin-types.ts +51 -79
  159. package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
  160. package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  161. package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
  162. package/src/vite/plugins/expose-action-id.ts +1 -3
  163. package/src/vite/plugins/expose-id-utils.ts +12 -0
  164. package/src/vite/plugins/expose-ids/handler-transform.ts +30 -0
  165. package/src/vite/plugins/expose-internal-ids.ts +257 -40
  166. package/src/vite/plugins/performance-tracks.ts +86 -0
  167. package/src/vite/plugins/refresh-cmd.ts +88 -26
  168. package/src/vite/plugins/version-plugin.ts +13 -1
  169. package/src/vite/rango.ts +204 -217
  170. package/src/vite/router-discovery.ts +335 -64
  171. package/src/vite/utils/banner.ts +4 -4
  172. package/src/vite/utils/package-resolution.ts +41 -1
  173. package/src/vite/utils/prerender-utils.ts +37 -5
  174. package/src/vite/utils/shared-utils.ts +3 -2
package/src/index.rsc.ts CHANGED
@@ -100,6 +100,7 @@ export type {
100
100
  LayoutUseItem,
101
101
  AllUseItems,
102
102
  UseItems,
103
+ HandlerUseItem,
103
104
  } from "./route-types.js";
104
105
 
105
106
  // Handle API
@@ -114,8 +115,9 @@ export { nonce } from "./rsc/nonce.js";
114
115
  // Pre-render handler API
115
116
  export {
116
117
  Prerender,
118
+ Passthrough,
117
119
  type PrerenderHandlerDefinition,
118
- type PrerenderPassthroughContext,
120
+ type PassthroughHandlerDefinition,
119
121
  type PrerenderOptions,
120
122
  type BuildContext,
121
123
  type StaticBuildContext,
@@ -170,6 +172,9 @@ export type { PublicRequestContext as RequestContext } from "./server/request-co
170
172
  import type { PublicRequestContext } from "./server/request-context.js";
171
173
  import type { DefaultEnv } from "./types/global-namespace.js";
172
174
 
175
+ // Shared base for every user-facing request context (mirrors index.ts).
176
+ export type { RequestScope, ExecutionContext } from "./types/request-scope.js";
177
+
173
178
  export const getRequestContext: <
174
179
  TEnv = DefaultEnv,
175
180
  >() => PublicRequestContext<TEnv> = _getRequestContextInternal;
package/src/index.ts CHANGED
@@ -88,6 +88,7 @@ export type {
88
88
  LayoutUseItem,
89
89
  AllUseItems,
90
90
  UseItems,
91
+ HandlerUseItem,
91
92
  } from "./route-types.js";
92
93
 
93
94
  // Response route types (usable in both server and client contexts)
@@ -146,17 +147,52 @@ export { createVar, type ContextVar } from "./context-var.js";
146
147
  export { nonce } from "./rsc/nonce.js";
147
148
 
148
149
  /**
149
- * Error-throwing stub for server-only `Prerender` function.
150
+ * SSR/client stub for server-only `Prerender` function.
151
+ *
152
+ * Returns a lightweight stub object instead of throwing so that the
153
+ * production SSR build can safely bundle the RSC entry chunk — the SSR
154
+ * bundler resolves `@rangojs/router` to this (SSR) entry, so Prerender
155
+ * calls in RSC code must not crash at module-evaluation time.
156
+ */
157
+ export function Prerender(
158
+ _handler?: any,
159
+ _optionsOrId?: any,
160
+ __injectedId?: string,
161
+ ): any {
162
+ const id =
163
+ typeof _optionsOrId === "string" ? _optionsOrId : __injectedId || "";
164
+ return { __brand: "prerenderHandler" as const, $$id: id };
165
+ }
166
+
167
+ /**
168
+ * SSR/client stub for server-only `Passthrough` function.
150
169
  */
151
- export function Prerender(): never {
152
- throw serverOnlyStubError("Prerender");
170
+ export function Passthrough(
171
+ _handler?: any,
172
+ _optionsOrId?: any,
173
+ __injectedId?: string,
174
+ ): any {
175
+ const id =
176
+ typeof _optionsOrId === "string" ? _optionsOrId : __injectedId || "";
177
+ return { __brand: "passthroughHandler" as const, $$id: id };
153
178
  }
154
179
 
155
180
  /**
156
- * Error-throwing stub for server-only `Static` function.
181
+ * SSR/client stub for server-only `Static` function.
182
+ *
183
+ * Returns a lightweight stub object instead of throwing so that the
184
+ * production SSR build can safely bundle the RSC entry chunk — the SSR
185
+ * bundler resolves `@rangojs/router` to this (SSR) entry, so Static
186
+ * calls in RSC code must not crash at module-evaluation time.
157
187
  */
158
- export function Static(): never {
159
- throw serverOnlyStubError("Static");
188
+ export function Static(
189
+ _handler?: any,
190
+ _optionsOrId?: any,
191
+ __injectedId?: string,
192
+ ): any {
193
+ const id =
194
+ typeof _optionsOrId === "string" ? _optionsOrId : __injectedId || "";
195
+ return { __brand: "staticHandler" as const, $$id: id };
160
196
  }
161
197
 
162
198
  /**
@@ -228,6 +264,9 @@ export function transition(): never {
228
264
  // Request context type (safe for client)
229
265
  export type { PublicRequestContext as RequestContext } from "./server/request-context.js";
230
266
 
267
+ // Shared base for every user-facing request context.
268
+ export type { RequestScope, ExecutionContext } from "./types/request-scope.js";
269
+
231
270
  // Cookie store types (safe for client)
232
271
  export type {
233
272
  CookieStore,
@@ -235,6 +274,10 @@ export type {
235
274
  ReadonlyHeaders,
236
275
  } from "./server/cookie-store.js";
237
276
 
277
+ // Built-in handles (universal — work on both server and client)
278
+ export { Meta } from "./handles/meta.js";
279
+ export { Breadcrumbs } from "./handles/breadcrumbs.js";
280
+
238
281
  // Meta types
239
282
  export type { MetaDescriptor, MetaDescriptorBase } from "./router/types.js";
240
283
 
@@ -1,4 +1,4 @@
1
- import { Context, createContext, type ReactNode } from "react";
1
+ import { type Context, createContext, type ReactNode } from "react";
2
2
  import type { ResolvedSegment } from "./types";
3
3
 
4
4
  export interface OutletContextValue {
@@ -121,10 +121,11 @@ export function createPrerenderStore(): PrerenderStore | null {
121
121
  if (!mod) return null;
122
122
  const specifier = mod.default[key];
123
123
  if (!specifier) return null;
124
- return mod
125
- .loadPrerenderAsset(specifier)
126
- .then((asset) => asset.default)
127
- .catch(() => null);
124
+ // Let asset load errors propagate — a missing/corrupted artifact
125
+ // for a key that exists in the manifest is a build/deploy error
126
+ // and should surface as a 500, not be silently swallowed as null
127
+ // (which the handler stub would misreport as a 404).
128
+ return mod.loadPrerenderAsset(specifier).then((asset) => asset.default);
128
129
  });
129
130
  cache.set(key, promise);
130
131
  return promise;
package/src/prerender.ts CHANGED
@@ -36,6 +36,7 @@ import type { Handle } from "./handle.js";
36
36
  import type { ContextVar } from "./context-var.js";
37
37
  import type { ReverseFunction } from "./reverse.js";
38
38
  import type { DefaultReverseRouteMap } from "./types/global-namespace.js";
39
+ import type { UseItems, HandlerUseItem } from "./route-types.js";
39
40
  import { isCachedFunction } from "./cache/taint.js";
40
41
 
41
42
  // -- Named route resolution types -------------------------------------------
@@ -105,13 +106,6 @@ type ResolvePrerenderParams<
105
106
  // -- Types ------------------------------------------------------------------
106
107
 
107
108
  export interface PrerenderOptions {
108
- /**
109
- * Keep handler in server bundle for live fallback (default: false).
110
- * false: handler replaced with stub, source-only APIs excluded from bundle.
111
- * true: handler stays in bundle, unknown params render live at request time.
112
- */
113
- passthrough?: boolean;
114
-
115
109
  /**
116
110
  * Maximum number of param sets to render in parallel (default: 1).
117
111
  * Only applies to dynamic Prerender handlers with getParams().
@@ -131,8 +125,8 @@ export interface PrerenderOptions {
131
125
 
132
126
  /**
133
127
  * Context passed to Prerender() handlers at build time.
134
- * Has a synthetic URL from getParams, params, and pathname.
135
- * No request, env, headers, cookies.
128
+ * Has a synthetic URL from getParams, params, pathname, and optionally env.
129
+ * No request, headers, cookies.
136
130
  */
137
131
  export interface BuildContext<TParams> {
138
132
  /** Params extracted from the route pattern (populated from getParams). */
@@ -141,6 +135,23 @@ export interface BuildContext<TParams> {
141
135
  /** True during build-time pre-rendering, false during passthrough live render. */
142
136
  build: true;
143
137
 
138
+ /**
139
+ * True when running in Vite dev mode (on-demand prerender), false during
140
+ * production `vite build`. Use this to branch on runtime mode without
141
+ * changing build semantics.
142
+ */
143
+ dev: boolean;
144
+
145
+ /**
146
+ * Build-time environment bindings (KV, D1, etc.) supplied by the Vite plugin.
147
+ * Only available when `buildEnv` is configured in rango() options.
148
+ * Throws with a clear error if not configured.
149
+ *
150
+ * This is NOT the live request env — it is shared across all prerender
151
+ * invocations for the build.
152
+ */
153
+ env: DefaultEnv;
154
+
144
155
  /** Read a variable set by getParams or a parent handler. */
145
156
  get: {
146
157
  <T>(contextVar: ContextVar<T>): T | undefined;
@@ -173,8 +184,8 @@ export interface BuildContext<TParams> {
173
184
 
174
185
  /**
175
186
  * Signal that this param set should not produce a local prerender artifact.
176
- * At runtime the handler runs live instead. Only valid on routes declared
177
- * with `{ passthrough: true }`.
187
+ * At runtime the live handler runs instead. Only valid on routes wrapped
188
+ * with `Passthrough()`.
178
189
  */
179
190
  passthrough: () => PrerenderPassthroughResult;
180
191
  }
@@ -187,6 +198,17 @@ export interface StaticBuildContext {
187
198
  /** Always true for Static handlers at build time. */
188
199
  build: true;
189
200
 
201
+ /**
202
+ * True when running in Vite dev mode, false during production build.
203
+ */
204
+ dev: boolean;
205
+
206
+ /**
207
+ * Build-time environment bindings supplied by the Vite plugin.
208
+ * Only available when `buildEnv` is configured in rango() options.
209
+ */
210
+ env: DefaultEnv;
211
+
190
212
  /** Read a variable (available for type consistency with BuildContext). */
191
213
  get: {
192
214
  <T>(contextVar: ContextVar<T>): T | undefined;
@@ -214,6 +236,17 @@ export interface GetParamsContext {
214
236
  /** Always true during build-time getParams execution. */
215
237
  build: true;
216
238
 
239
+ /**
240
+ * True when running in Vite dev mode, false during production build.
241
+ */
242
+ dev: boolean;
243
+
244
+ /**
245
+ * Build-time environment bindings supplied by the Vite plugin.
246
+ * Only available when `buildEnv` is configured in rango() options.
247
+ */
248
+ env: DefaultEnv;
249
+
217
250
  /** Set a variable that will be available to each handler invocation via ctx.get(). */
218
251
  set: {
219
252
  <T>(contextVar: ContextVar<T>, value: T): void;
@@ -224,23 +257,6 @@ export interface GetParamsContext {
224
257
  reverse: BuildReverseFunction;
225
258
  }
226
259
 
227
- /**
228
- * Context type for passthrough Prerender handlers.
229
- *
230
- * When `passthrough: true`, the handler runs both at build time and at request
231
- * time. The context is a full `HandlerContext` with `build: boolean`:
232
- * - `ctx.build === true`: build-time, env/request/res throw at runtime
233
- * - `ctx.build === false`: live request, full context available
234
- *
235
- * For `passthrough: false` (default), handlers receive `BuildContext` only.
236
- */
237
- export type PrerenderPassthroughContext<
238
- TParams = {},
239
- TEnv = DefaultEnv,
240
- > = HandlerContext<TParams, TEnv> & {
241
- passthrough: () => PrerenderPassthroughResult;
242
- };
243
-
244
260
  export interface PrerenderHandlerDefinition<
245
261
  TParams extends Record<string, any> = any,
246
262
  > {
@@ -253,6 +269,8 @@ export interface PrerenderHandlerDefinition<
253
269
  getParams?: (ctx: GetParamsContext) => Promise<TParams[]> | TParams[];
254
270
  /** Pre-render options. */
255
271
  options?: PrerenderOptions;
272
+ /** Composable default DSL items merged when the handler is mounted. */
273
+ use?: () => UseItems<HandlerUseItem>;
256
274
  }
257
275
 
258
276
  // -- Overloads --------------------------------------------------------------
@@ -263,7 +281,7 @@ export interface PrerenderHandlerDefinition<
263
281
  // Explicit params work as before:
264
282
  // Prerender<{ slug: string }> → params = { slug: string }
265
283
 
266
- // Overload 1: Static handler, no passthrough (build-time only)
284
+ // Overload 1: Static handler (build-time only)
267
285
  export function Prerender<
268
286
  T extends
269
287
  | keyof DefaultPrerenderRouteMap
@@ -273,34 +291,15 @@ export function Prerender<
273
291
  >(
274
292
  handler: (
275
293
  ctx: BuildContext<ResolvePrerenderParams<T, TRouteMap>>,
276
- ) => ReactNode | Promise<ReactNode>,
277
- options?: PrerenderOptions & { passthrough?: false },
278
- __injectedId?: string,
279
- ): PrerenderHandlerDefinition<ResolvePrerenderParams<T, TRouteMap>>;
280
-
281
- // Overload 2: Static handler, passthrough (build + live — full HandlerContext)
282
- export function Prerender<
283
- T extends
284
- | keyof DefaultPrerenderRouteMap
285
- | `.${keyof TRouteMap & string}`
286
- | Record<string, any> = {},
287
- TRouteMap extends {} = DefaultPrerenderRouteMap,
288
- TEnv = DefaultEnv,
289
- >(
290
- handler: (
291
- ctx: PrerenderPassthroughContext<
292
- ResolvePrerenderParams<T, TRouteMap>,
293
- TEnv
294
- >,
295
294
  ) =>
296
295
  | ReactNode
297
296
  | PrerenderPassthroughResult
298
297
  | Promise<ReactNode | PrerenderPassthroughResult>,
299
- options: PrerenderOptions & { passthrough: true },
298
+ options?: PrerenderOptions,
300
299
  __injectedId?: string,
301
300
  ): PrerenderHandlerDefinition<ResolvePrerenderParams<T, TRouteMap>>;
302
301
 
303
- // Overload 3: Dynamic handler, no passthrough (build-time only)
302
+ // Overload 2: Dynamic handler (build-time only)
304
303
  export function Prerender<
305
304
  T extends
306
305
  | keyof DefaultPrerenderRouteMap
@@ -315,35 +314,11 @@ export function Prerender<
315
314
  | ResolvePrerenderParams<T, TRouteMap>[],
316
315
  handler: (
317
316
  ctx: BuildContext<ResolvePrerenderParams<T, TRouteMap>>,
318
- ) => ReactNode | Promise<ReactNode>,
319
- options?: PrerenderOptions & { passthrough?: false },
320
- __injectedId?: string,
321
- ): PrerenderHandlerDefinition<ResolvePrerenderParams<T, TRouteMap>>;
322
-
323
- // Overload 4: Dynamic handler, passthrough (build + live — full HandlerContext)
324
- export function Prerender<
325
- T extends
326
- | keyof DefaultPrerenderRouteMap
327
- | `.${keyof TRouteMap & string}`
328
- | Record<string, any>,
329
- TRouteMap extends {} = DefaultPrerenderRouteMap,
330
- TEnv = DefaultEnv,
331
- >(
332
- getParams: (
333
- ctx: GetParamsContext,
334
- ) =>
335
- | Promise<ResolvePrerenderParams<T, TRouteMap>[]>
336
- | ResolvePrerenderParams<T, TRouteMap>[],
337
- handler: (
338
- ctx: PrerenderPassthroughContext<
339
- ResolvePrerenderParams<T, TRouteMap>,
340
- TEnv
341
- >,
342
317
  ) =>
343
318
  | ReactNode
344
319
  | PrerenderPassthroughResult
345
320
  | Promise<ReactNode | PrerenderPassthroughResult>,
346
- options: PrerenderOptions & { passthrough: true },
321
+ options?: PrerenderOptions,
347
322
  __injectedId?: string,
348
323
  ): PrerenderHandlerDefinition<ResolvePrerenderParams<T, TRouteMap>>;
349
324
 
@@ -422,7 +397,7 @@ export function Prerender<TParams extends Record<string, any>>(
422
397
  /**
423
398
  * Sentinel returned by `ctx.passthrough()` to signal that a specific param set
424
399
  * should not produce a local prerender artifact. The build skips writing the
425
- * entry; at runtime the handler runs live (requires `{ passthrough: true }`).
400
+ * entry; at runtime the Passthrough live handler runs instead.
426
401
  */
427
402
  export const PRERENDER_PASSTHROUGH: Readonly<{
428
403
  __brand: "prerenderPassthrough";
@@ -446,7 +421,7 @@ export function isPrerenderPassthrough(
446
421
  );
447
422
  }
448
423
 
449
- // -- Type guard -------------------------------------------------------------
424
+ // -- Type guards ------------------------------------------------------------
450
425
 
451
426
  /**
452
427
  * Type guard to check if a value is a PrerenderHandlerDefinition.
@@ -461,3 +436,89 @@ export function isPrerenderHandler(
461
436
  (value as { __brand: unknown }).__brand === "prerenderHandler"
462
437
  );
463
438
  }
439
+
440
+ // -- Passthrough wrapper ----------------------------------------------------
441
+
442
+ /**
443
+ * A prerender route with a live fallback handler for unknown params at runtime.
444
+ *
445
+ * Wraps a `Prerender(...)` definition with a separate handler that runs at
446
+ * request time for params not covered by `getParams()`.
447
+ *
448
+ * - Build time: `prerenderDef` provides getParams + build handler.
449
+ * - Runtime: `liveHandler` runs for unknown params with full HandlerContext.
450
+ *
451
+ * @example
452
+ * ```ts
453
+ * const BlogPrerender = Prerender(
454
+ * async () => [{ slug: "getting-started" }, { slug: "api-reference" }],
455
+ * async (ctx) => <BlogPost slug={ctx.params.slug} />,
456
+ * );
457
+ *
458
+ * // In route definition:
459
+ * path("/blog/:slug", Passthrough(BlogPrerender, async (ctx) => {
460
+ * const post = await ctx.env.DB.get(ctx.params.slug);
461
+ * return <BlogPost slug={ctx.params.slug} post={post} />;
462
+ * }))
463
+ * ```
464
+ */
465
+ export interface PassthroughHandlerDefinition<
466
+ TParams extends Record<string, any> = any,
467
+ TEnv = DefaultEnv,
468
+ > {
469
+ readonly __brand: "passthroughHandler";
470
+ /** The underlying prerender definition (build-time rendering). */
471
+ prerenderDef: PrerenderHandlerDefinition<TParams>;
472
+ /** Live handler for runtime fallback on unknown params. */
473
+ liveHandler: (
474
+ ctx: HandlerContext<TParams, TEnv>,
475
+ ) => ReactNode | Promise<ReactNode> | Response | Promise<Response>;
476
+ /** Composable default DSL items merged when the handler is mounted. */
477
+ use?: () => UseItems<HandlerUseItem>;
478
+ }
479
+
480
+ export function Passthrough<
481
+ TParams extends Record<string, any>,
482
+ TEnv = DefaultEnv,
483
+ >(
484
+ prerenderDef: PrerenderHandlerDefinition<TParams>,
485
+ liveHandler: (
486
+ ctx: HandlerContext<TParams, TEnv>,
487
+ ) => ReactNode | Promise<ReactNode> | Response | Promise<Response>,
488
+ ): PassthroughHandlerDefinition<TParams, TEnv>;
489
+
490
+ // Implementation
491
+ export function Passthrough<
492
+ TParams extends Record<string, any>,
493
+ TEnv = DefaultEnv,
494
+ >(
495
+ prerenderDef: PrerenderHandlerDefinition<TParams>,
496
+ liveHandler: (
497
+ ctx: HandlerContext<TParams, TEnv>,
498
+ ) => ReactNode | Promise<ReactNode> | Response | Promise<Response>,
499
+ ): PassthroughHandlerDefinition<TParams, TEnv> {
500
+ if (!isPrerenderHandler(prerenderDef)) {
501
+ throw new Error(
502
+ "[rsc-router] Passthrough: first argument must be a Prerender() definition.",
503
+ );
504
+ }
505
+ return {
506
+ __brand: "passthroughHandler" as const,
507
+ prerenderDef,
508
+ liveHandler,
509
+ };
510
+ }
511
+
512
+ /**
513
+ * Type guard to check if a value is a PassthroughHandlerDefinition.
514
+ */
515
+ export function isPassthroughHandler(
516
+ value: unknown,
517
+ ): value is PassthroughHandlerDefinition {
518
+ return (
519
+ typeof value === "object" &&
520
+ value !== null &&
521
+ "__brand" in value &&
522
+ (value as { __brand: unknown }).__brand === "passthroughHandler"
523
+ );
524
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Runtime-neutral Response shape utilities.
3
+ *
4
+ * Kept at the src/ root so both `router/` and `rsc/` can depend on it
5
+ * without creating a cross-layer import cycle.
6
+ */
7
+
8
+ /**
9
+ * True when a Response represents a WebSocket upgrade handoff and must not
10
+ * be reconstructed or mutated:
11
+ *
12
+ * - Status 101 (Switching Protocols) is outside the standard Response
13
+ * constructor's 200–599 range, so `new Response(body, { status: 101 })`
14
+ * throws RangeError on Node/undici and any spec-compliant runtime.
15
+ * - Cloudflare's workerd attaches a non-standard `webSocket` property on
16
+ * the upgrade Response (e.g. from `acceptWebSocket`/`handleWebSocketUpgrade`
17
+ * or the `agents` library's `routeAgentRequest`). That property is dropped
18
+ * by a `new Response(...)` copy, breaking the upgrade even on workerd
19
+ * where the status range is relaxed.
20
+ *
21
+ * Callers should short-circuit header/body merges for these responses.
22
+ */
23
+ export function isWebSocketUpgradeResponse(response: Response): boolean {
24
+ return (
25
+ response.status === 101 ||
26
+ (response as unknown as { webSocket?: unknown }).webSocket != null
27
+ );
28
+ }
package/src/reverse.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { ExtractParams } from "./types.js";
2
2
  import type { SearchSchema, ResolveSearchSchema } from "./search-params.js";
3
3
  import { serializeSearchParams } from "./search-params.js";
4
+ import { encodePathSegment } from "./router/url-params.js";
4
5
 
5
6
  /**
6
7
  * Sanitize prefix string by removing leading slash
@@ -305,16 +306,40 @@ export function createReverse<TRoutes extends Record<string, string>>(
305
306
  if (params) {
306
307
  // Replace :param placeholders with actual values
307
308
  // Strip constraint syntax: :param(a|b) -> use "param" as key
309
+ // Optional params (:param?) are omitted when not provided
310
+ let hadOmittedOptional = false;
308
311
  result = result.replace(
309
- /:([a-zA-Z_][a-zA-Z0-9_]*)(\([^)]*\))?\??/g,
312
+ /:([a-zA-Z_][a-zA-Z0-9_]*)(\([^)]*\))?(\?)/g,
313
+ (_, key, _constraint, optional) => {
314
+ const value = params[key];
315
+ // Empty string is treated as omitted — the trie matcher fills
316
+ // unmatched optional params with "" (not undefined), so reverse
317
+ // must collapse those segments instead of leaving empty slots.
318
+ if (value === undefined || value === "") {
319
+ hadOmittedOptional = true;
320
+ return "";
321
+ }
322
+ return encodePathSegment(value);
323
+ },
324
+ );
325
+ // Second pass: required params (no trailing ?)
326
+ result = result.replace(
327
+ /:([a-zA-Z_][a-zA-Z0-9_]*)(\([^)]*\))?(?!\?)/g,
310
328
  (_, key) => {
311
329
  const value = params[key];
312
330
  if (value === undefined) {
313
331
  throw new Error(`Missing param "${key}" for route "${name}"`);
314
332
  }
315
- return encodeURIComponent(value);
333
+ return encodePathSegment(value);
316
334
  },
317
335
  );
336
+ // Clean up slashes only when an optional param was actually omitted,
337
+ // so intentional trailing-slash patterns like "/blog/" are preserved.
338
+ if (hadOmittedOptional) {
339
+ const hadTrailingSlash = pattern.length > 1 && pattern.endsWith("/");
340
+ result = result.replace(/\/\/+/g, "/").replace(/\/+$/, "") || "/";
341
+ if (hadTrailingSlash && !result.endsWith("/")) result += "/";
342
+ }
318
343
  }
319
344
 
320
345
  // Append search params as query string