@rangojs/router 0.0.0-experimental.20 → 0.0.0-experimental.20dbba0c

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 (189) hide show
  1. package/AGENTS.md +4 -0
  2. package/README.md +172 -50
  3. package/dist/bin/rango.js +138 -50
  4. package/dist/vite/index.js +1160 -508
  5. package/dist/vite/index.js.bak +5448 -0
  6. package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  7. package/package.json +17 -16
  8. package/skills/breadcrumbs/SKILL.md +252 -0
  9. package/skills/cache-guide/SKILL.md +32 -0
  10. package/skills/caching/SKILL.md +49 -8
  11. package/skills/document-cache/SKILL.md +2 -2
  12. package/skills/handler-use/SKILL.md +362 -0
  13. package/skills/hooks/SKILL.md +61 -51
  14. package/skills/host-router/SKILL.md +218 -0
  15. package/skills/intercept/SKILL.md +20 -0
  16. package/skills/layout/SKILL.md +22 -0
  17. package/skills/links/SKILL.md +91 -17
  18. package/skills/loader/SKILL.md +107 -24
  19. package/skills/middleware/SKILL.md +34 -3
  20. package/skills/migrate-nextjs/SKILL.md +560 -0
  21. package/skills/migrate-react-router/SKILL.md +765 -0
  22. package/skills/parallel/SKILL.md +185 -0
  23. package/skills/prerender/SKILL.md +112 -70
  24. package/skills/rango/SKILL.md +24 -23
  25. package/skills/response-routes/SKILL.md +8 -0
  26. package/skills/route/SKILL.md +58 -4
  27. package/skills/router-setup/SKILL.md +95 -5
  28. package/skills/streams-and-websockets/SKILL.md +283 -0
  29. package/skills/typesafety/SKILL.md +38 -24
  30. package/src/__internal.ts +92 -0
  31. package/src/browser/app-shell.ts +52 -0
  32. package/src/browser/app-version.ts +14 -0
  33. package/src/browser/event-controller.ts +5 -0
  34. package/src/browser/link-interceptor.ts +4 -0
  35. package/src/browser/navigation-bridge.ts +175 -17
  36. package/src/browser/navigation-client.ts +177 -44
  37. package/src/browser/navigation-store.ts +68 -9
  38. package/src/browser/navigation-transaction.ts +11 -9
  39. package/src/browser/partial-update.ts +113 -17
  40. package/src/browser/prefetch/cache.ts +275 -28
  41. package/src/browser/prefetch/fetch.ts +191 -46
  42. package/src/browser/prefetch/policy.ts +6 -0
  43. package/src/browser/prefetch/queue.ts +123 -20
  44. package/src/browser/prefetch/resource-ready.ts +77 -0
  45. package/src/browser/rango-state.ts +53 -13
  46. package/src/browser/react/Link.tsx +98 -14
  47. package/src/browser/react/NavigationProvider.tsx +89 -14
  48. package/src/browser/react/context.ts +7 -2
  49. package/src/browser/react/use-handle.ts +9 -58
  50. package/src/browser/react/use-navigation.ts +22 -2
  51. package/src/browser/react/use-params.ts +11 -1
  52. package/src/browser/react/use-router.ts +29 -9
  53. package/src/browser/rsc-router.tsx +177 -66
  54. package/src/browser/scroll-restoration.ts +41 -42
  55. package/src/browser/segment-reconciler.ts +36 -9
  56. package/src/browser/server-action-bridge.ts +8 -6
  57. package/src/browser/types.ts +73 -5
  58. package/src/build/generate-manifest.ts +6 -6
  59. package/src/build/generate-route-types.ts +3 -0
  60. package/src/build/route-trie.ts +67 -25
  61. package/src/build/route-types/include-resolution.ts +8 -1
  62. package/src/build/route-types/router-processing.ts +223 -74
  63. package/src/build/route-types/scan-filter.ts +8 -1
  64. package/src/cache/cache-runtime.ts +15 -11
  65. package/src/cache/cache-scope.ts +48 -7
  66. package/src/cache/cf/cf-cache-store.ts +455 -15
  67. package/src/cache/cf/index.ts +5 -1
  68. package/src/cache/document-cache.ts +17 -7
  69. package/src/cache/index.ts +1 -0
  70. package/src/cache/taint.ts +55 -0
  71. package/src/client.rsc.tsx +2 -1
  72. package/src/client.tsx +85 -276
  73. package/src/context-var.ts +72 -2
  74. package/src/debug.ts +2 -2
  75. package/src/handle.ts +40 -0
  76. package/src/handles/breadcrumbs.ts +66 -0
  77. package/src/handles/index.ts +1 -0
  78. package/src/host/index.ts +0 -3
  79. package/src/index.rsc.ts +9 -36
  80. package/src/index.ts +79 -70
  81. package/src/outlet-context.ts +1 -1
  82. package/src/prerender/store.ts +57 -15
  83. package/src/prerender.ts +138 -77
  84. package/src/response-utils.ts +28 -0
  85. package/src/reverse.ts +27 -2
  86. package/src/route-definition/dsl-helpers.ts +240 -40
  87. package/src/route-definition/helpers-types.ts +67 -19
  88. package/src/route-definition/index.ts +3 -3
  89. package/src/route-definition/redirect.ts +11 -3
  90. package/src/route-definition/resolve-handler-use.ts +155 -0
  91. package/src/route-map-builder.ts +7 -1
  92. package/src/route-types.ts +18 -0
  93. package/src/router/content-negotiation.ts +100 -1
  94. package/src/router/find-match.ts +4 -2
  95. package/src/router/handler-context.ts +129 -26
  96. package/src/router/intercept-resolution.ts +11 -4
  97. package/src/router/lazy-includes.ts +10 -7
  98. package/src/router/loader-resolution.ts +160 -22
  99. package/src/router/logging.ts +5 -2
  100. package/src/router/manifest.ts +31 -16
  101. package/src/router/match-api.ts +128 -193
  102. package/src/router/match-middleware/background-revalidation.ts +30 -2
  103. package/src/router/match-middleware/cache-lookup.ts +94 -17
  104. package/src/router/match-middleware/cache-store.ts +53 -10
  105. package/src/router/match-middleware/intercept-resolution.ts +9 -7
  106. package/src/router/match-middleware/segment-resolution.ts +61 -5
  107. package/src/router/match-result.ts +103 -18
  108. package/src/router/metrics.ts +238 -13
  109. package/src/router/middleware-types.ts +48 -27
  110. package/src/router/middleware.ts +201 -86
  111. package/src/router/navigation-snapshot.ts +182 -0
  112. package/src/router/pattern-matching.ts +77 -11
  113. package/src/router/prerender-match.ts +114 -10
  114. package/src/router/preview-match.ts +30 -102
  115. package/src/router/request-classification.ts +310 -0
  116. package/src/router/revalidation.ts +27 -7
  117. package/src/router/route-snapshot.ts +245 -0
  118. package/src/router/router-context.ts +6 -1
  119. package/src/router/router-interfaces.ts +50 -5
  120. package/src/router/router-options.ts +50 -19
  121. package/src/router/segment-resolution/fresh.ts +215 -19
  122. package/src/router/segment-resolution/helpers.ts +30 -25
  123. package/src/router/segment-resolution/loader-cache.ts +1 -0
  124. package/src/router/segment-resolution/revalidation.ts +454 -301
  125. package/src/router/segment-wrappers.ts +2 -0
  126. package/src/router/trie-matching.ts +30 -6
  127. package/src/router/types.ts +1 -0
  128. package/src/router/url-params.ts +49 -0
  129. package/src/router.ts +89 -17
  130. package/src/rsc/handler.ts +563 -364
  131. package/src/rsc/helpers.ts +69 -41
  132. package/src/rsc/index.ts +0 -20
  133. package/src/rsc/loader-fetch.ts +23 -3
  134. package/src/rsc/manifest-init.ts +5 -1
  135. package/src/rsc/progressive-enhancement.ts +37 -10
  136. package/src/rsc/response-route-handler.ts +14 -1
  137. package/src/rsc/rsc-rendering.ts +47 -44
  138. package/src/rsc/server-action.ts +24 -10
  139. package/src/rsc/ssr-setup.ts +128 -0
  140. package/src/rsc/types.ts +11 -1
  141. package/src/search-params.ts +16 -13
  142. package/src/segment-content-promise.ts +67 -0
  143. package/src/segment-loader-promise.ts +122 -0
  144. package/src/segment-system.tsx +109 -23
  145. package/src/server/context.ts +174 -19
  146. package/src/server/handle-store.ts +19 -0
  147. package/src/server/loader-registry.ts +9 -8
  148. package/src/server/request-context.ts +218 -65
  149. package/src/server.ts +6 -0
  150. package/src/ssr/index.tsx +4 -0
  151. package/src/static-handler.ts +18 -6
  152. package/src/theme/index.ts +4 -13
  153. package/src/types/cache-types.ts +4 -4
  154. package/src/types/handler-context.ts +140 -72
  155. package/src/types/loader-types.ts +41 -15
  156. package/src/types/request-scope.ts +126 -0
  157. package/src/types/route-config.ts +17 -8
  158. package/src/types/route-entry.ts +19 -1
  159. package/src/types/segments.ts +2 -5
  160. package/src/urls/include-helper.ts +24 -14
  161. package/src/urls/path-helper-types.ts +39 -6
  162. package/src/urls/path-helper.ts +48 -13
  163. package/src/urls/pattern-types.ts +12 -0
  164. package/src/urls/response-types.ts +18 -16
  165. package/src/use-loader.tsx +77 -5
  166. package/src/vite/discovery/bundle-postprocess.ts +61 -89
  167. package/src/vite/discovery/discover-routers.ts +7 -4
  168. package/src/vite/discovery/prerender-collection.ts +162 -88
  169. package/src/vite/discovery/state.ts +17 -13
  170. package/src/vite/index.ts +8 -3
  171. package/src/vite/plugin-types.ts +51 -79
  172. package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
  173. package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  174. package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
  175. package/src/vite/plugins/expose-action-id.ts +1 -3
  176. package/src/vite/plugins/expose-id-utils.ts +12 -0
  177. package/src/vite/plugins/expose-ids/handler-transform.ts +30 -0
  178. package/src/vite/plugins/expose-internal-ids.ts +257 -40
  179. package/src/vite/plugins/performance-tracks.ts +88 -0
  180. package/src/vite/plugins/refresh-cmd.ts +127 -0
  181. package/src/vite/plugins/version-plugin.ts +13 -1
  182. package/src/vite/rango.ts +190 -217
  183. package/src/vite/router-discovery.ts +241 -45
  184. package/src/vite/utils/banner.ts +4 -4
  185. package/src/vite/utils/package-resolution.ts +34 -1
  186. package/src/vite/utils/prerender-utils.ts +97 -5
  187. package/src/vite/utils/shared-utils.ts +3 -2
  188. package/skills/testing/SKILL.md +0 -226
  189. package/src/route-definition/route-function.ts +0 -119
@@ -29,6 +29,7 @@ import {
29
29
  } from "./response-adapter.js";
30
30
  import { mergeLocationState } from "./history-state.js";
31
31
  import { classifyActionOutcome } from "./action-coordinator.js";
32
+ import { getAppVersion } from "./app-version.js";
32
33
 
33
34
  // Polyfill Symbol.dispose/asyncDispose for Safari and older browsers
34
35
  if (typeof Symbol.dispose === "undefined") {
@@ -43,8 +44,6 @@ if (typeof Symbol.asyncDispose === "undefined") {
43
44
  */
44
45
  export interface ServerActionBridgeConfigWithController extends ServerActionBridgeConfig {
45
46
  eventController: EventController;
46
- /** RSC version from initial payload metadata */
47
- version?: string;
48
47
  /** Callback to trigger SPA navigation (for action redirects) */
49
48
  onNavigate?: (
50
49
  url: string,
@@ -75,7 +74,6 @@ export function createServerActionBridge(
75
74
  deps,
76
75
  onUpdate,
77
76
  renderSegments,
78
- version,
79
77
  onNavigate,
80
78
  } = config;
81
79
 
@@ -86,7 +84,7 @@ export function createServerActionBridge(
86
84
  client,
87
85
  onUpdate,
88
86
  renderSegments,
89
- version,
87
+ getVersion: getAppVersion,
90
88
  });
91
89
 
92
90
  /**
@@ -165,9 +163,15 @@ export function createServerActionBridge(
165
163
  segmentState.currentSegmentIds.join(","),
166
164
  );
167
165
  // Add version param for version mismatch detection
166
+ const version = getAppVersion();
168
167
  if (version) {
169
168
  url.searchParams.set("_rsc_v", version);
170
169
  }
170
+ // Add router ID for app switch detection
171
+ const rid = store.getRouterId?.();
172
+ if (rid) {
173
+ url.searchParams.set("_rsc_rid", rid);
174
+ }
171
175
 
172
176
  // Encode arguments
173
177
  const encodedBody = await deps.encodeReply(args, { temporaryReferences });
@@ -206,7 +210,6 @@ export function createServerActionBridge(
206
210
  "rsc-action": id,
207
211
  "X-RSC-Router-Client-Path": segmentState.currentUrl,
208
212
  ...(tx && { "X-RSC-Router-Request-Id": tx.requestId }),
209
- // Send intercept source URL so server can maintain intercept context
210
213
  ...(interceptSourceUrl && {
211
214
  "X-RSC-Router-Intercept-Source": interceptSourceUrl,
212
215
  }),
@@ -309,7 +312,6 @@ export function createServerActionBridge(
309
312
  matchedCount: payload.metadata?.matched?.length ?? 0,
310
313
  diffCount: payload.metadata?.diff?.length ?? 0,
311
314
  });
312
-
313
315
  // Guard: if the action was aborted while streaming (e.g., user navigated
314
316
  // away or abortAllActions fired), bail out before any reconcile/render/cache
315
317
  // writes to avoid overwriting the current UI with stale action results.
@@ -32,6 +32,9 @@ export type HandleData = Record<string, Record<string, unknown[]>>;
32
32
  export interface RscMetadata {
33
33
  pathname: string;
34
34
  segments: ResolvedSegment[];
35
+ /** Router instance ID. When this changes between navigations, the client
36
+ * forces a full tree replacement (app switch via host router). */
37
+ routerId?: string;
35
38
  isPartial?: boolean;
36
39
  isError?: boolean;
37
40
  matched?: string[];
@@ -55,6 +58,11 @@ export interface RscMetadata {
55
58
  * Used to detect version mismatches after HMR/deployment.
56
59
  */
57
60
  version?: string;
61
+ /**
62
+ * TTL in milliseconds for the client-side in-memory prefetch cache.
63
+ * Sent on initial render so the browser can configure its cache duration.
64
+ */
65
+ prefetchCacheTTL?: number;
58
66
  /**
59
67
  * Theme configuration from router.
60
68
  * Included when theme is enabled in router config.
@@ -65,6 +73,8 @@ export interface RscMetadata {
65
73
  * Included when theme is enabled in router config.
66
74
  */
67
75
  initialTheme?: Theme;
76
+ /** URL prefix for all routes (from createRouter({ basename })). */
77
+ basename?: string;
68
78
  /** Whether connection warmup is enabled */
69
79
  warmupEnabled?: boolean;
70
80
  /** Server-side redirect with optional state (for partial requests) */
@@ -210,6 +220,15 @@ export interface SegmentState {
210
220
  export interface NavigationUpdate {
211
221
  root: ReactNode | Promise<ReactNode>;
212
222
  metadata: RscMetadata;
223
+ /** Scroll behavior to apply after React commits this update */
224
+ scroll?: {
225
+ /** For back/forward: restore saved position */
226
+ restore?: boolean;
227
+ /** Set to false to disable scrolling entirely */
228
+ enabled?: boolean;
229
+ /** Function to check if streaming is in progress */
230
+ isStreaming?: () => boolean;
231
+ };
213
232
  }
214
233
 
215
234
  /**
@@ -227,6 +246,25 @@ export type HistoryState =
227
246
  export interface NavigateOptions {
228
247
  replace?: boolean;
229
248
  scroll?: boolean;
249
+ /**
250
+ * Whether to revalidate server data on navigation.
251
+ * Set to `false` to skip the RSC server fetch and only update the URL.
252
+ *
253
+ * Only takes effect when the pathname stays the same (search param / hash changes).
254
+ * If the pathname changes, this option is ignored and a full navigation occurs.
255
+ *
256
+ * All location-aware hooks (`useSearchParams`, `useNavigation`, etc.) still update.
257
+ * Server components do not re-render.
258
+ *
259
+ * @default true
260
+ *
261
+ * @example
262
+ * ```tsx
263
+ * router.push("/products?color=blue", { revalidate: false });
264
+ * router.replace("/products?page=3", { revalidate: false });
265
+ * ```
266
+ */
267
+ revalidate?: boolean;
230
268
  /**
231
269
  * State to pass to history.pushState/replaceState
232
270
  * Accessible via useLocationState() hook.
@@ -308,7 +346,13 @@ export type ReadonlyURLSearchParams = Omit<
308
346
  export interface RscBrowserDependencies {
309
347
  createFromFetch: <T>(
310
348
  response: Promise<Response>,
311
- options?: { temporaryReferences?: any },
349
+ options?: {
350
+ temporaryReferences?: any;
351
+ findSourceMapURL?: (
352
+ filename: string,
353
+ environmentName: string,
354
+ ) => string | null;
355
+ },
312
356
  ) => Promise<T>;
313
357
  createFromReadableStream: <T>(stream: ReadableStream) => Promise<T>;
314
358
  encodeReply: (
@@ -370,16 +414,25 @@ export interface NavigationStore {
370
414
  segments: ResolvedSegment[],
371
415
  handleData?: HandleData,
372
416
  ): void;
373
- getCachedSegments(
374
- historyKey: string,
375
- ):
376
- | { segments: ResolvedSegment[]; stale: boolean; handleData?: HandleData }
417
+ getCachedSegments(historyKey: string):
418
+ | {
419
+ segments: ResolvedSegment[];
420
+ stale: boolean;
421
+ handleData?: HandleData;
422
+ routerId?: string;
423
+ }
377
424
  | undefined;
378
425
  hasHistoryCache(historyKey: string): boolean;
379
426
  updateCacheHandleData(historyKey: string, handleData: HandleData): void;
380
427
  markCacheAsStale(): void;
381
428
  markCacheAsStaleAndBroadcast(): void;
382
429
  clearHistoryCache(): void;
430
+ /**
431
+ * Clear this tab's nav + prefetch caches without broadcasting or rotating
432
+ * shared state. Intended for app-switch transitions that affect only this
433
+ * tab's session.
434
+ */
435
+ clearHistoryCacheLocal(): void;
383
436
  broadcastCacheInvalidation(): void;
384
437
 
385
438
  // Cross-tab refresh callback (set by navigation bridge)
@@ -389,6 +442,10 @@ export interface NavigationStore {
389
442
  getInterceptSourceUrl(): string | null;
390
443
  setInterceptSourceUrl(url: string | null): void;
391
444
 
445
+ // Router identity tracking (for cross-app navigation detection)
446
+ getRouterId?(): string | undefined;
447
+ setRouterId?(id: string): void;
448
+
392
449
  // UI update notifications
393
450
  onUpdate(callback: UpdateSubscriber): () => void;
394
451
  emitUpdate(update: NavigationUpdate): void;
@@ -419,6 +476,8 @@ export interface FetchPartialOptions {
419
476
  interceptSourceUrl?: string;
420
477
  /** RSC version for cache invalidation detection */
421
478
  version?: string;
479
+ /** Current router ID — server detects app switch and returns full response */
480
+ routerId?: string;
422
481
  /** If true, this is an HMR refetch - server should invalidate manifest cache */
423
482
  hmr?: boolean;
424
483
  }
@@ -487,6 +546,15 @@ export interface NavigationBridge {
487
546
  refresh(): Promise<void>;
488
547
  handlePopstate(): Promise<void>;
489
548
  registerLinkInterception(): () => void;
549
+ /** Update the RSC version (e.g. after HMR). Clears prefetch cache. */
550
+ updateVersion(newVersion: string): void;
551
+ /**
552
+ * Replace the active app-shell snapshot (rootLayout, basename, version)
553
+ * atomically. Used on cross-app navigations when the response's routerId
554
+ * indicates the user entered a different app. Theme, warmup, and prefetch
555
+ * TTL are document-lifetime and not part of the shell.
556
+ */
557
+ updateAppShell(next: import("./app-shell.js").AppShell): void;
490
558
  }
491
559
 
492
560
  /**
@@ -45,7 +45,7 @@ export interface GeneratedManifest {
45
45
  routeTrailingSlash?: Record<string, string>;
46
46
  /** Route names using Prerender (for dev-mode Node.js delegation) */
47
47
  prerenderRoutes?: string[];
48
- /** Route names with passthrough: true (handler kept in bundle for live fallback) */
48
+ /** Route names wrapped with Passthrough() (live handler for runtime fallback) */
49
49
  passthroughRoutes?: string[];
50
50
  /** Route name → response type for non-RSC routes */
51
51
  responseTypeRoutes?: Record<string, string>;
@@ -150,10 +150,7 @@ function buildPrefixTreeNode(
150
150
  if (prerenderDefs && entry.prerenderDef) {
151
151
  prerenderDefs[name] = entry.prerenderDef;
152
152
  }
153
- if (
154
- passthroughRoutes &&
155
- entry.prerenderDef?.options?.passthrough === true
156
- ) {
153
+ if (passthroughRoutes && entry.isPassthrough === true) {
157
154
  passthroughRoutes.push(name);
158
155
  }
159
156
  }
@@ -285,6 +282,7 @@ export function generateManifest<TEnv>(
285
282
  export function generateManifestFull<TEnv>(
286
283
  urlpatterns: UrlPatterns<TEnv, any>,
287
284
  mountIndex: number = 0,
285
+ options?: { urlPrefix?: string },
288
286
  ): FullManifest {
289
287
  const routeManifest: Record<string, string> = {};
290
288
  const routeAncestry: Record<string, string[]> = {};
@@ -310,6 +308,8 @@ export function generateManifestFull<TEnv>(
310
308
  counters: {},
311
309
  mountIndex,
312
310
  trackedIncludes, // Enable include tracking
311
+ // basename sets the initial URL prefix for all path() registrations
312
+ ...(options?.urlPrefix ? { urlPrefix: options.urlPrefix } : {}),
313
313
  },
314
314
  () => {
315
315
  const helpers = createRouteHelpers();
@@ -347,7 +347,7 @@ export function generateManifestFull<TEnv>(
347
347
  if (entry.prerenderDef) {
348
348
  prerenderDefs[name] = entry.prerenderDef;
349
349
  }
350
- if (entry.prerenderDef?.options?.passthrough === true) {
350
+ if (entry.isPassthrough === true) {
351
351
  passthroughRoutes.push(name);
352
352
  }
353
353
  }
@@ -25,6 +25,9 @@ export {
25
25
  } from "./route-types/include-resolution.js";
26
26
  export {
27
27
  extractUrlsVariableFromRouter,
28
+ extractUrlsFromRouter,
29
+ extractBasenameFromRouter,
30
+ type UrlsExtractionResult,
28
31
  buildCombinedRouteMapForRouterFile,
29
32
  detectUnresolvableIncludes,
30
33
  detectUnresolvableIncludesForUrlsFile,
@@ -47,6 +47,8 @@ export interface TrieNode {
47
47
  s?: Record<string, TrieNode>;
48
48
  /** Param child: { n: paramName, c: child node } */
49
49
  p?: { n: string; c: TrieNode };
50
+ /** Suffix-param children keyed by suffix (e.g., ".html" → { n: "productId", c: ... }) */
51
+ xp?: Record<string, { n: string; c: TrieNode }>;
50
52
  /** Wildcard terminal: leaf + paramName */
51
53
  w?: TrieLeaf & { pn: string };
52
54
  }
@@ -96,8 +98,14 @@ export function buildRouteTrie(
96
98
  }
97
99
 
98
100
  /**
99
- * Insert a route into the trie, handling optional params by forking
100
- * the insertion path (one terminal without the param, one with).
101
+ * Insert a route into the trie. Optional params expand into two branches at
102
+ * registration time (skip-first, then present), so each terminal lives at the
103
+ * correct depth for its number of bound params and carries a branch-local
104
+ * `pa` listing only those names. The trie's single-slot `node.p` is reused
105
+ * across branches because matching ignores `node.p.n` — the leaf's `pa` is
106
+ * the source of truth for naming. Skip-first ordering lets `mergeLeaf`'s
107
+ * last-wins rule produce greedy-leftmost semantics for free at any shared
108
+ * terminal depth.
101
109
  */
102
110
  function insertRoute(
103
111
  node: TrieNode,
@@ -105,14 +113,13 @@ function insertRoute(
105
113
  index: number,
106
114
  leaf: Omit<TrieLeaf, "op" | "cv" | "pa">,
107
115
  ): void {
108
- // Collect param names, optional param names, and constraints across all segments
109
- const paramNames: string[] = [];
116
+ // op (full optional list) and cv (full constraint map) are route-level and
117
+ // identical on every terminal, so compute them once on the shared base.
110
118
  const optionalParams: string[] = [];
111
119
  const constraints: Record<string, string[]> = {};
112
120
 
113
121
  for (const seg of segments) {
114
122
  if (seg.type === "param") {
115
- paramNames.push(seg.value);
116
123
  if (seg.optional) {
117
124
  optionalParams.push(seg.value);
118
125
  }
@@ -122,21 +129,15 @@ function insertRoute(
122
129
  }
123
130
  }
124
131
 
125
- const fullLeaf: TrieLeaf = {
132
+ const leafBase: Omit<TrieLeaf, "pa"> = {
126
133
  ...leaf,
127
- ...(paramNames.length > 0 ? { pa: paramNames } : {}),
128
134
  ...(optionalParams.length > 0 ? { op: optionalParams } : {}),
129
135
  ...(Object.keys(constraints).length > 0 ? { cv: constraints } : {}),
130
136
  };
131
137
 
132
- insertSegments(node, segments, index, fullLeaf);
138
+ insertSegments(node, segments, index, leafBase, []);
133
139
  }
134
140
 
135
- /**
136
- * Recursively insert segments into the trie.
137
- * For optional params, we add a terminal at the current node (param absent)
138
- * AND continue inserting into the param child (param present).
139
- */
140
141
  /**
141
142
  * Extract ancestry map from a built trie by visiting all leaf nodes.
142
143
  * Returns { routeName: ancestryShortCodes[] } for every route in the trie.
@@ -158,6 +159,11 @@ export function extractAncestryFromTrie(
158
159
  visit(child);
159
160
  }
160
161
  }
162
+ if (node.xp) {
163
+ for (const child of Object.values(node.xp)) {
164
+ visit(child.c);
165
+ }
166
+ }
161
167
  if (node.p) {
162
168
  visit(node.p.c);
163
169
  }
@@ -211,15 +217,25 @@ function mergeLeaf(node: TrieNode, leaf: TrieLeaf): void {
211
217
  node.r = mergeLeaves(node.r, leaf);
212
218
  }
213
219
 
220
+ function buildLeaf(
221
+ leafBase: Omit<TrieLeaf, "pa">,
222
+ paramNames: string[],
223
+ ): TrieLeaf {
224
+ return paramNames.length > 0
225
+ ? { ...leafBase, pa: [...paramNames] }
226
+ : { ...leafBase };
227
+ }
228
+
214
229
  function insertSegments(
215
230
  node: TrieNode,
216
231
  segments: ParsedSegment[],
217
232
  index: number,
218
- leaf: TrieLeaf,
233
+ leafBase: Omit<TrieLeaf, "pa">,
234
+ paramNames: string[],
219
235
  ): void {
220
- // Base case: all segments consumed, add terminal
236
+ // Base case: all segments consumed, add terminal with branch-local pa
221
237
  if (index >= segments.length) {
222
- mergeLeaf(node, leaf);
238
+ mergeLeaf(node, buildLeaf(leafBase, paramNames));
223
239
  return;
224
240
  }
225
241
 
@@ -228,20 +244,46 @@ function insertSegments(
228
244
  if (segment.type === "static") {
229
245
  if (!node.s) node.s = {};
230
246
  if (!node.s[segment.value]) node.s[segment.value] = {};
231
- insertSegments(node.s[segment.value], segments, index + 1, leaf);
247
+ insertSegments(
248
+ node.s[segment.value],
249
+ segments,
250
+ index + 1,
251
+ leafBase,
252
+ paramNames,
253
+ );
232
254
  } else if (segment.type === "param") {
233
255
  if (segment.optional) {
234
- // Optional param: add terminal at current node (param absent)
235
- mergeLeaf(node, leaf);
236
- // AND continue with param child (param present)
256
+ // SKIP first: continue at the same node without binding this name.
257
+ // Skip-first ordering means the present-branch's TAKE overwrites any
258
+ // shared terminal later, giving greedy-leftmost semantics.
259
+ insertSegments(node, segments, index + 1, leafBase, paramNames);
237
260
  }
238
- if (!node.p) {
239
- node.p = { n: segment.value, c: {} };
261
+ if (segment.suffix) {
262
+ // Suffix param: keyed by suffix string (e.g., ".html")
263
+ if (!node.xp) node.xp = {};
264
+ if (!node.xp[segment.suffix]) {
265
+ node.xp[segment.suffix] = { n: segment.value, c: {} };
266
+ }
267
+ insertSegments(node.xp[segment.suffix].c, segments, index + 1, leafBase, [
268
+ ...paramNames,
269
+ segment.value,
270
+ ]);
271
+ } else {
272
+ if (!node.p) {
273
+ node.p = { n: segment.value, c: {} };
274
+ }
275
+ insertSegments(node.p.c, segments, index + 1, leafBase, [
276
+ ...paramNames,
277
+ segment.value,
278
+ ]);
240
279
  }
241
- insertSegments(node.p.c, segments, index + 1, leaf);
242
280
  } else if (segment.type === "wildcard") {
243
- // Wildcard consumes all remaining segments
244
- const wildLeaf = { ...leaf, pn: "*" };
281
+ // Wildcard consumes all remaining segments. Carry any params bound before
282
+ // the wildcard in pa so they zip correctly against paramValues at match.
283
+ const wildLeaf: TrieLeaf & { pn: string } = {
284
+ ...buildLeaf(leafBase, paramNames),
285
+ pn: "*",
286
+ };
245
287
  const existing = node.w ? ({ ...node.w } as TrieLeaf) : undefined;
246
288
  const merged = mergeLeaves(existing, wildLeaf);
247
289
  node.w = merged as TrieLeaf & { pn: string };
@@ -357,12 +357,17 @@ function buildRouteMapFromBlock(
357
357
  /**
358
358
  * Build route map and search schemas together.
359
359
  * Internal helper used by the include resolution path.
360
+ *
361
+ * @param inlineBlock - Optional pre-extracted code block (e.g. from an inline
362
+ * builder function). When provided, variableName is ignored and the block
363
+ * is parsed directly for path()/include() calls.
360
364
  */
361
365
  export function buildCombinedRouteMapWithSearch(
362
366
  filePath: string,
363
367
  variableName?: string,
364
368
  visited?: Set<string>,
365
369
  diagnosticsOut?: UnresolvableInclude[],
370
+ inlineBlock?: string,
366
371
  ): {
367
372
  routes: Record<string, string>;
368
373
  searchSchemas: Record<string, Record<string, string>>;
@@ -384,7 +389,9 @@ export function buildCombinedRouteMapWithSearch(
384
389
  }
385
390
 
386
391
  let block: string;
387
- if (variableName) {
392
+ if (inlineBlock) {
393
+ block = inlineBlock;
394
+ } else if (variableName) {
388
395
  const extracted = extractUrlsBlockForVariable(source, variableName);
389
396
  if (!extracted) return { routes: {}, searchSchemas: {} };
390
397
  block = extracted;