@rangojs/router 0.0.0-experimental.88a3b2f7 → 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 (102) hide show
  1. package/README.md +50 -20
  2. package/dist/vite/index.js +647 -176
  3. package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  4. package/package.json +7 -5
  5. package/skills/breadcrumbs/SKILL.md +3 -1
  6. package/skills/handler-use/SKILL.md +362 -0
  7. package/skills/hooks/SKILL.md +28 -20
  8. package/skills/intercept/SKILL.md +20 -0
  9. package/skills/layout/SKILL.md +22 -0
  10. package/skills/links/SKILL.md +88 -16
  11. package/skills/loader/SKILL.md +35 -2
  12. package/skills/middleware/SKILL.md +32 -3
  13. package/skills/migrate-nextjs/SKILL.md +560 -0
  14. package/skills/migrate-react-router/SKILL.md +765 -0
  15. package/skills/parallel/SKILL.md +59 -0
  16. package/skills/rango/SKILL.md +24 -22
  17. package/skills/response-routes/SKILL.md +8 -0
  18. package/skills/route/SKILL.md +24 -0
  19. package/skills/streams-and-websockets/SKILL.md +283 -0
  20. package/skills/typesafety/SKILL.md +3 -1
  21. package/src/browser/app-shell.ts +52 -0
  22. package/src/browser/navigation-bridge.ts +72 -4
  23. package/src/browser/navigation-client.ts +64 -13
  24. package/src/browser/navigation-store.ts +25 -1
  25. package/src/browser/partial-update.ts +34 -3
  26. package/src/browser/prefetch/cache.ts +129 -21
  27. package/src/browser/prefetch/fetch.ts +148 -16
  28. package/src/browser/prefetch/queue.ts +36 -5
  29. package/src/browser/rango-state.ts +53 -13
  30. package/src/browser/react/Link.tsx +30 -2
  31. package/src/browser/react/NavigationProvider.tsx +50 -11
  32. package/src/browser/react/use-navigation.ts +22 -2
  33. package/src/browser/react/use-params.ts +11 -1
  34. package/src/browser/react/use-router.ts +8 -1
  35. package/src/browser/rsc-router.tsx +34 -6
  36. package/src/browser/segment-reconciler.ts +36 -14
  37. package/src/browser/types.ts +13 -0
  38. package/src/build/route-trie.ts +50 -24
  39. package/src/cache/cf/cf-cache-store.ts +5 -7
  40. package/src/client.tsx +84 -230
  41. package/src/index.rsc.ts +3 -0
  42. package/src/index.ts +44 -9
  43. package/src/outlet-context.ts +1 -1
  44. package/src/response-utils.ts +28 -0
  45. package/src/reverse.ts +7 -3
  46. package/src/route-definition/dsl-helpers.ts +180 -24
  47. package/src/route-definition/helpers-types.ts +61 -14
  48. package/src/route-definition/resolve-handler-use.ts +6 -0
  49. package/src/route-types.ts +7 -0
  50. package/src/router/handler-context.ts +24 -4
  51. package/src/router/lazy-includes.ts +6 -6
  52. package/src/router/loader-resolution.ts +73 -46
  53. package/src/router/manifest.ts +22 -13
  54. package/src/router/match-api.ts +3 -3
  55. package/src/router/match-middleware/cache-lookup.ts +10 -5
  56. package/src/router/match-middleware/segment-resolution.ts +1 -1
  57. package/src/router/match-result.ts +82 -4
  58. package/src/router/middleware-types.ts +2 -22
  59. package/src/router/middleware.ts +32 -4
  60. package/src/router/pattern-matching.ts +60 -9
  61. package/src/router/segment-resolution/fresh.ts +52 -0
  62. package/src/router/segment-resolution/revalidation.ts +69 -1
  63. package/src/router/trie-matching.ts +10 -4
  64. package/src/router/url-params.ts +49 -0
  65. package/src/router.ts +1 -2
  66. package/src/rsc/handler.ts +21 -9
  67. package/src/rsc/helpers.ts +69 -41
  68. package/src/rsc/loader-fetch.ts +23 -3
  69. package/src/rsc/progressive-enhancement.ts +12 -2
  70. package/src/rsc/response-route-handler.ts +14 -1
  71. package/src/rsc/rsc-rendering.ts +12 -1
  72. package/src/rsc/server-action.ts +8 -0
  73. package/src/rsc/types.ts +1 -0
  74. package/src/segment-content-promise.ts +67 -0
  75. package/src/segment-loader-promise.ts +122 -0
  76. package/src/segment-system.tsx +11 -61
  77. package/src/server/context.ts +26 -3
  78. package/src/server/handle-store.ts +19 -0
  79. package/src/server/request-context.ts +64 -56
  80. package/src/types/handler-context.ts +2 -34
  81. package/src/types/loader-types.ts +5 -6
  82. package/src/types/request-scope.ts +126 -0
  83. package/src/types/route-entry.ts +11 -0
  84. package/src/types/segments.ts +1 -1
  85. package/src/urls/include-helper.ts +24 -14
  86. package/src/urls/path-helper-types.ts +34 -5
  87. package/src/urls/response-types.ts +2 -10
  88. package/src/use-loader.tsx +77 -5
  89. package/src/vite/debug.ts +55 -0
  90. package/src/vite/discovery/prerender-collection.ts +124 -83
  91. package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
  92. package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  93. package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
  94. package/src/vite/plugins/expose-id-utils.ts +12 -0
  95. package/src/vite/plugins/expose-ids/handler-transform.ts +30 -0
  96. package/src/vite/plugins/expose-internal-ids.ts +257 -40
  97. package/src/vite/plugins/performance-tracks.ts +4 -6
  98. package/src/vite/rango.ts +49 -14
  99. package/src/vite/router-discovery.ts +186 -26
  100. package/src/vite/utils/banner.ts +1 -1
  101. package/src/vite/utils/package-resolution.ts +41 -1
  102. package/src/vite/utils/prerender-utils.ts +20 -6
package/src/client.tsx CHANGED
@@ -13,7 +13,6 @@ import {
13
13
  type ClientErrorBoundaryFallbackProps,
14
14
  type ErrorInfo,
15
15
  type LoaderDefinition,
16
- type LoaderFn,
17
16
  type ResolvedSegment,
18
17
  } from "./types";
19
18
  import {
@@ -22,6 +21,83 @@ import {
22
21
  } from "./route-content-wrapper.js";
23
22
  import { OutletProvider } from "./outlet-provider.js";
24
23
  import { MountContextProvider } from "./browser/react/mount-context.js";
24
+ import { getMemoizedContentPromise } from "./segment-content-promise.js";
25
+
26
+ /**
27
+ * Render the content for a named parallel/intercept slot segment.
28
+ *
29
+ * Shared by Outlet (with `name` prop) and ParallelOutlet — both resolve a
30
+ * segment from context.parallel by slot name and then render it through the
31
+ * same layout/loader/mountPath wrapping pipeline.
32
+ */
33
+ function renderSlotContent(segment: ResolvedSegment | null): ReactNode {
34
+ if (!segment) return null;
35
+
36
+ const content: ReactNode =
37
+ segment.loading || segment.component instanceof Promise ? (
38
+ <RouteContentWrapper
39
+ content={getMemoizedContentPromise(segment.component)}
40
+ fallback={segment.loading}
41
+ segmentId={segment.id}
42
+ />
43
+ ) : (
44
+ (segment.component ?? null)
45
+ );
46
+
47
+ const hasOwnLoaders = !!(segment.loaderDataPromise && segment.loaderIds);
48
+ const loaderWrapped = hasOwnLoaders ? (
49
+ <LoaderBoundary
50
+ loaderDataPromise={segment.loaderDataPromise!}
51
+ loaderIds={segment.loaderIds!}
52
+ fallback={segment.loading}
53
+ outletKey={segment.id + "-loader"}
54
+ outletContent={null}
55
+ segment={segment}
56
+ >
57
+ {content}
58
+ </LoaderBoundary>
59
+ ) : null;
60
+
61
+ let result: ReactNode;
62
+ if (segment.layout) {
63
+ // Layout renders immediately; if loaders exist, the LoaderBoundary becomes
64
+ // the outlet content so layout's <Outlet /> suspends until loaders resolve.
65
+ result = (
66
+ <OutletProvider
67
+ content={hasOwnLoaders ? loaderWrapped : content}
68
+ segment={segment}
69
+ >
70
+ {segment.layout}
71
+ </OutletProvider>
72
+ );
73
+ } else if (hasOwnLoaders) {
74
+ // No layout but has loaders — wrap content with LoaderBoundary for useLoader context.
75
+ // Common for intercept routes that use useLoader without a custom layout.
76
+ result = loaderWrapped;
77
+ } else {
78
+ result = content;
79
+ }
80
+
81
+ if (segment.mountPath) {
82
+ return (
83
+ <MountContextProvider value={segment.mountPath}>
84
+ {result}
85
+ </MountContextProvider>
86
+ );
87
+ }
88
+
89
+ return result;
90
+ }
91
+
92
+ function useSlotSegment(
93
+ context: OutletContextValue | null,
94
+ name: `@${string}` | undefined,
95
+ ): ResolvedSegment | null {
96
+ return useMemo(() => {
97
+ if (!name || !context?.parallel) return null;
98
+ return context.parallel.find((seg) => seg.slot === name) ?? null;
99
+ }, [context, name]);
100
+ }
25
101
 
26
102
  /**
27
103
  * Outlet component - renders child content in layouts
@@ -62,95 +138,10 @@ import { MountContextProvider } from "./browser/react/mount-context.js";
62
138
  */
63
139
  export function Outlet({ name }: { name?: `@${string}` } = {}): ReactNode {
64
140
  const context = useContext(OutletContext);
141
+ const namedSegment = useSlotSegment(context, name);
65
142
 
66
- // If name provided, render parallel/intercept content for that slot
67
143
  if (name) {
68
- const segment = context?.parallel?.find((seg) => seg.slot === name) ?? null;
69
-
70
- if (!segment) return null;
71
-
72
- // Determine the content to render
73
- let content: ReactNode;
74
- if (segment.loading || segment.component instanceof Promise) {
75
- // Use RouteContentWrapper to handle Suspense wrapping properly
76
- content = (
77
- <RouteContentWrapper
78
- content={
79
- segment.component instanceof Promise
80
- ? segment.component
81
- : Promise.resolve(segment.component)
82
- }
83
- fallback={segment.loading}
84
- segmentId={segment.id}
85
- />
86
- );
87
- } else {
88
- content = segment.component ?? null;
89
- }
90
-
91
- let result: ReactNode;
92
-
93
- // If segment has a layout, wrap appropriately
94
- if (segment.layout) {
95
- // Check if this segment has loaders that need streaming
96
- // The layout renders immediately, LoaderBoundary becomes the outlet content
97
- // When layout renders <Outlet />, it gets the LoaderBoundary which suspends
98
- if (segment.loaderDataPromise && segment.loaderIds) {
99
- const loaderAwareContent = (
100
- <LoaderBoundary
101
- loaderDataPromise={segment.loaderDataPromise}
102
- loaderIds={segment.loaderIds}
103
- fallback={segment.loading}
104
- outletKey={segment.id + "-loader"}
105
- outletContent={null}
106
- segment={segment}
107
- >
108
- {content}
109
- </LoaderBoundary>
110
- );
111
-
112
- result = (
113
- <OutletProvider content={loaderAwareContent} segment={segment}>
114
- {segment.layout}
115
- </OutletProvider>
116
- );
117
- } else {
118
- // No loaders - wrap in OutletProvider so layout can use <Outlet />
119
- result = (
120
- <OutletProvider content={content} segment={segment}>
121
- {segment.layout}
122
- </OutletProvider>
123
- );
124
- }
125
- } else if (segment.loaderDataPromise && segment.loaderIds) {
126
- // No layout but has loaders - wrap content with LoaderBoundary for useLoader context
127
- // This is common for intercept routes that use useLoader without a custom layout
128
- result = (
129
- <LoaderBoundary
130
- loaderDataPromise={segment.loaderDataPromise}
131
- loaderIds={segment.loaderIds}
132
- fallback={segment.loading}
133
- outletKey={segment.id + "-loader"}
134
- outletContent={null}
135
- segment={segment}
136
- >
137
- {content}
138
- </LoaderBoundary>
139
- );
140
- } else {
141
- result = content;
142
- }
143
-
144
- // Wrap with MountContextProvider for include() scoped parallel/intercept slots
145
- if (segment.mountPath) {
146
- return (
147
- <MountContextProvider value={segment.mountPath}>
148
- {result}
149
- </MountContextProvider>
150
- );
151
- }
152
-
153
- return result;
144
+ return renderSlotContent(namedSegment);
154
145
  }
155
146
 
156
147
  // Default: render child content
@@ -164,6 +155,7 @@ export function Outlet({ name }: { name?: `@${string}` } = {}): ReactNode {
164
155
 
165
156
  return content;
166
157
  }
158
+
167
159
  /**
168
160
  * ParallelOutlet component - renders content for a named parallel slot
169
161
  *
@@ -188,94 +180,9 @@ export function Outlet({ name }: { name?: `@${string}` } = {}): ReactNode {
188
180
  */
189
181
  export function ParallelOutlet({ name }: { name: `@${string}` }): ReactNode {
190
182
  const context = useContext(OutletContext);
191
- const segment = useMemo(() => {
192
- if (!context?.parallel) return null;
193
- return context.parallel.find((seg) => seg.slot === name) ?? null;
194
- }, [context, name]);
183
+ const segment = useSlotSegment(context, name);
195
184
 
196
- if (!segment) return null;
197
-
198
- // Determine the content to render
199
- let content: ReactNode;
200
- if (segment.loading || segment.component instanceof Promise) {
201
- // Use RouteContentWrapper to handle Suspense wrapping properly
202
- content = (
203
- <RouteContentWrapper
204
- content={
205
- segment.component instanceof Promise
206
- ? segment.component
207
- : Promise.resolve(segment.component)
208
- }
209
- fallback={segment.loading}
210
- segmentId={segment.id}
211
- />
212
- );
213
- } else {
214
- content = segment.component ?? null;
215
- }
216
-
217
- let result: ReactNode;
218
-
219
- // If segment has a layout, wrap appropriately
220
- if (segment.layout) {
221
- // Check if this segment has loaders that need streaming
222
- // The layout renders immediately, LoaderBoundary becomes the outlet content
223
- if (segment.loaderDataPromise && segment.loaderIds) {
224
- const loaderAwareContent = (
225
- <LoaderBoundary
226
- loaderDataPromise={segment.loaderDataPromise}
227
- loaderIds={segment.loaderIds}
228
- fallback={segment.loading}
229
- outletKey={segment.id + "-loader"}
230
- outletContent={null}
231
- segment={segment}
232
- >
233
- {content}
234
- </LoaderBoundary>
235
- );
236
-
237
- result = (
238
- <OutletProvider content={loaderAwareContent} segment={segment}>
239
- {segment.layout}
240
- </OutletProvider>
241
- );
242
- } else {
243
- // No loaders - wrap in OutletProvider so layout can use <Outlet />
244
- result = (
245
- <OutletProvider content={content} segment={segment}>
246
- {segment.layout}
247
- </OutletProvider>
248
- );
249
- }
250
- } else if (segment.loaderDataPromise && segment.loaderIds) {
251
- // No layout but has loaders - wrap content with LoaderBoundary for useLoader context
252
- // This is common for intercept routes that use useLoader without a custom layout
253
- result = (
254
- <LoaderBoundary
255
- loaderDataPromise={segment.loaderDataPromise}
256
- loaderIds={segment.loaderIds}
257
- fallback={segment.loading}
258
- outletKey={segment.id + "-loader"}
259
- outletContent={null}
260
- segment={segment}
261
- >
262
- {content}
263
- </LoaderBoundary>
264
- );
265
- } else {
266
- result = content;
267
- }
268
-
269
- // Wrap with MountContextProvider for include() scoped parallel/intercept slots
270
- if (segment.mountPath) {
271
- return (
272
- <MountContextProvider value={segment.mountPath}>
273
- {result}
274
- </MountContextProvider>
275
- );
276
- }
277
-
278
- return result;
185
+ return renderSlotContent(segment);
279
186
  }
280
187
 
281
188
  // OutletProvider is defined in outlet-provider.tsx to break a circular
@@ -313,57 +220,6 @@ export {
313
220
  type UseLoaderOptions,
314
221
  } from "./use-loader.js";
315
222
 
316
- /**
317
- * Client-safe createLoader factory
318
- *
319
- * Creates a loader definition that can be used with useLoader().
320
- * This is the client-side version that only stores the $$id - the function
321
- * is ignored since loaders only execute on the server.
322
- *
323
- * The $$id is injected by the exposeLoaderId Vite plugin. In most cases,
324
- * you should import the loader directly from the server file rather than
325
- * creating a reference manually.
326
- *
327
- * @param fn - Loader function (ignored on client, kept for API compatibility)
328
- * @param _fetchable - Optional fetchable flag (ignored on client)
329
- * @param __injectedId - $$id injected by Vite plugin
330
- *
331
- * @example
332
- * ```tsx
333
- * "use client";
334
- * import { useLoader } from "rsc-router/client";
335
- * import { CartLoader } from "../loaders/cart"; // Import from server file
336
- *
337
- * export function CartIcon() {
338
- * const cart = useLoader(CartLoader);
339
- * return <span>Cart ({cart?.items.length ?? 0})</span>;
340
- * }
341
- * ```
342
- */
343
- // Overload 1: With function only (not fetchable)
344
- export function createLoader<T>(
345
- fn: LoaderFn<T, Record<string, string | undefined>, any>,
346
- ): LoaderDefinition<Awaited<T>, Record<string, string | undefined>>;
347
-
348
- // Overload 2: With function and fetchable flag
349
- export function createLoader<T>(
350
- fn: LoaderFn<T, Record<string, string | undefined>, any>,
351
- fetchable: true,
352
- ): LoaderDefinition<Awaited<T>, Record<string, string | undefined>>;
353
-
354
- // Implementation - function is ignored at runtime on client
355
- // The $$id is injected by Vite plugin as hidden third parameter
356
- export function createLoader(
357
- _fn: LoaderFn<any, Record<string, string | undefined>, any>,
358
- _fetchable?: true,
359
- __injectedId?: string,
360
- ): LoaderDefinition<any, Record<string, string | undefined>> {
361
- return {
362
- __brand: "loader",
363
- $$id: __injectedId || "",
364
- };
365
- }
366
-
367
223
  /**
368
224
  * Props for the ErrorBoundary component
369
225
  */
@@ -534,10 +390,8 @@ export {
534
390
  type ScrollRestorationProps,
535
391
  } from "./browser/react/ScrollRestoration.js";
536
392
 
537
- // Handle API - for accumulating data across route segments
538
- export { createHandle, isHandle, type Handle } from "./handle.js";
539
-
540
- // Handle data hook
393
+ // Handle data hook (client-side only createHandle/isHandle are server APIs from the root export)
394
+ export { type Handle } from "./handle.js";
541
395
  export { useHandle } from "./browser/react/use-handle.js";
542
396
 
543
397
  // Built-in handles
package/src/index.rsc.ts CHANGED
@@ -172,6 +172,9 @@ export type { PublicRequestContext as RequestContext } from "./server/request-co
172
172
  import type { PublicRequestContext } from "./server/request-context.js";
173
173
  import type { DefaultEnv } from "./types/global-namespace.js";
174
174
 
175
+ // Shared base for every user-facing request context (mirrors index.ts).
176
+ export type { RequestScope, ExecutionContext } from "./types/request-scope.js";
177
+
175
178
  export const getRequestContext: <
176
179
  TEnv = DefaultEnv,
177
180
  >() => PublicRequestContext<TEnv> = _getRequestContextInternal;
package/src/index.ts CHANGED
@@ -147,24 +147,52 @@ export { createVar, type ContextVar } from "./context-var.js";
147
147
  export { nonce } from "./rsc/nonce.js";
148
148
 
149
149
  /**
150
- * 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.
151
156
  */
152
- export function Prerender(): never {
153
- throw serverOnlyStubError("Prerender");
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 };
154
165
  }
155
166
 
156
167
  /**
157
- * Error-throwing stub for server-only `Passthrough` function.
168
+ * SSR/client stub for server-only `Passthrough` function.
158
169
  */
159
- export function Passthrough(): never {
160
- throw serverOnlyStubError("Passthrough");
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 };
161
178
  }
162
179
 
163
180
  /**
164
- * 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.
165
187
  */
166
- export function Static(): never {
167
- 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 };
168
196
  }
169
197
 
170
198
  /**
@@ -236,6 +264,9 @@ export function transition(): never {
236
264
  // Request context type (safe for client)
237
265
  export type { PublicRequestContext as RequestContext } from "./server/request-context.js";
238
266
 
267
+ // Shared base for every user-facing request context.
268
+ export type { RequestScope, ExecutionContext } from "./types/request-scope.js";
269
+
239
270
  // Cookie store types (safe for client)
240
271
  export type {
241
272
  CookieStore,
@@ -243,6 +274,10 @@ export type {
243
274
  ReadonlyHeaders,
244
275
  } from "./server/cookie-store.js";
245
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
+
246
281
  // Meta types
247
282
  export type { MetaDescriptor, MetaDescriptorBase } from "./router/types.js";
248
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 {
@@ -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
@@ -311,11 +312,14 @@ export function createReverse<TRoutes extends Record<string, string>>(
311
312
  /:([a-zA-Z_][a-zA-Z0-9_]*)(\([^)]*\))?(\?)/g,
312
313
  (_, key, _constraint, optional) => {
313
314
  const value = params[key];
314
- if (value === undefined) {
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 === "") {
315
319
  hadOmittedOptional = true;
316
320
  return "";
317
321
  }
318
- return encodeURIComponent(value);
322
+ return encodePathSegment(value);
319
323
  },
320
324
  );
321
325
  // Second pass: required params (no trailing ?)
@@ -326,7 +330,7 @@ export function createReverse<TRoutes extends Record<string, string>>(
326
330
  if (value === undefined) {
327
331
  throw new Error(`Missing param "${key}" for route "${name}"`);
328
332
  }
329
- return encodeURIComponent(value);
333
+ return encodePathSegment(value);
330
334
  },
331
335
  );
332
336
  // Clean up slashes only when an optional param was actually omitted,