@rangojs/router 0.0.0-experimental.19 → 0.0.0-experimental.1fa245e2

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 (160) hide show
  1. package/{CLAUDE.md → AGENTS.md} +4 -0
  2. package/README.md +122 -30
  3. package/dist/bin/rango.js +245 -63
  4. package/dist/vite/index.js +859 -418
  5. package/package.json +3 -3
  6. package/skills/breadcrumbs/SKILL.md +250 -0
  7. package/skills/cache-guide/SKILL.md +32 -0
  8. package/skills/caching/SKILL.md +49 -8
  9. package/skills/document-cache/SKILL.md +2 -2
  10. package/skills/hooks/SKILL.md +33 -31
  11. package/skills/host-router/SKILL.md +218 -0
  12. package/skills/links/SKILL.md +3 -1
  13. package/skills/loader/SKILL.md +72 -22
  14. package/skills/middleware/SKILL.md +2 -0
  15. package/skills/parallel/SKILL.md +126 -0
  16. package/skills/prerender/SKILL.md +112 -70
  17. package/skills/rango/SKILL.md +0 -1
  18. package/skills/route/SKILL.md +34 -4
  19. package/skills/router-setup/SKILL.md +95 -5
  20. package/skills/typesafety/SKILL.md +35 -23
  21. package/src/__internal.ts +92 -0
  22. package/src/bin/rango.ts +18 -0
  23. package/src/browser/app-version.ts +14 -0
  24. package/src/browser/event-controller.ts +5 -0
  25. package/src/browser/link-interceptor.ts +4 -0
  26. package/src/browser/navigation-bridge.ts +114 -18
  27. package/src/browser/navigation-client.ts +126 -44
  28. package/src/browser/navigation-store.ts +43 -8
  29. package/src/browser/navigation-transaction.ts +11 -9
  30. package/src/browser/partial-update.ts +80 -15
  31. package/src/browser/prefetch/cache.ts +166 -27
  32. package/src/browser/prefetch/fetch.ts +52 -39
  33. package/src/browser/prefetch/policy.ts +6 -0
  34. package/src/browser/prefetch/queue.ts +92 -20
  35. package/src/browser/prefetch/resource-ready.ts +77 -0
  36. package/src/browser/react/Link.tsx +70 -14
  37. package/src/browser/react/NavigationProvider.tsx +40 -4
  38. package/src/browser/react/context.ts +7 -2
  39. package/src/browser/react/use-handle.ts +9 -58
  40. package/src/browser/react/use-router.ts +21 -8
  41. package/src/browser/rsc-router.tsx +143 -59
  42. package/src/browser/scroll-restoration.ts +41 -42
  43. package/src/browser/segment-reconciler.ts +6 -1
  44. package/src/browser/server-action-bridge.ts +454 -436
  45. package/src/browser/types.ts +60 -5
  46. package/src/build/generate-manifest.ts +6 -6
  47. package/src/build/generate-route-types.ts +5 -0
  48. package/src/build/route-trie.ts +19 -3
  49. package/src/build/route-types/include-resolution.ts +8 -1
  50. package/src/build/route-types/router-processing.ts +346 -87
  51. package/src/build/route-types/scan-filter.ts +8 -1
  52. package/src/cache/cache-runtime.ts +15 -11
  53. package/src/cache/cache-scope.ts +48 -7
  54. package/src/cache/cf/cf-cache-store.ts +453 -11
  55. package/src/cache/cf/index.ts +5 -1
  56. package/src/cache/document-cache.ts +17 -7
  57. package/src/cache/index.ts +1 -0
  58. package/src/cache/taint.ts +55 -0
  59. package/src/client.rsc.tsx +2 -1
  60. package/src/client.tsx +3 -102
  61. package/src/context-var.ts +72 -2
  62. package/src/debug.ts +2 -2
  63. package/src/handle.ts +40 -0
  64. package/src/handles/breadcrumbs.ts +66 -0
  65. package/src/handles/index.ts +1 -0
  66. package/src/host/index.ts +0 -3
  67. package/src/index.rsc.ts +8 -37
  68. package/src/index.ts +40 -66
  69. package/src/prerender/store.ts +57 -15
  70. package/src/prerender.ts +138 -77
  71. package/src/reverse.ts +22 -1
  72. package/src/route-definition/dsl-helpers.ts +73 -25
  73. package/src/route-definition/helpers-types.ts +10 -6
  74. package/src/route-definition/index.ts +3 -3
  75. package/src/route-definition/redirect.ts +11 -3
  76. package/src/route-definition/resolve-handler-use.ts +149 -0
  77. package/src/route-map-builder.ts +7 -1
  78. package/src/route-types.ts +11 -0
  79. package/src/router/content-negotiation.ts +100 -1
  80. package/src/router/find-match.ts +4 -2
  81. package/src/router/handler-context.ts +108 -25
  82. package/src/router/intercept-resolution.ts +11 -4
  83. package/src/router/lazy-includes.ts +4 -1
  84. package/src/router/loader-resolution.ts +123 -11
  85. package/src/router/logging.ts +5 -2
  86. package/src/router/manifest.ts +9 -3
  87. package/src/router/match-api.ts +125 -190
  88. package/src/router/match-middleware/background-revalidation.ts +30 -2
  89. package/src/router/match-middleware/cache-lookup.ts +88 -16
  90. package/src/router/match-middleware/cache-store.ts +53 -10
  91. package/src/router/match-middleware/intercept-resolution.ts +9 -7
  92. package/src/router/match-middleware/segment-resolution.ts +61 -5
  93. package/src/router/match-result.ts +22 -15
  94. package/src/router/metrics.ts +238 -13
  95. package/src/router/middleware-types.ts +53 -12
  96. package/src/router/middleware.ts +172 -85
  97. package/src/router/navigation-snapshot.ts +182 -0
  98. package/src/router/pattern-matching.ts +20 -5
  99. package/src/router/prerender-match.ts +114 -10
  100. package/src/router/preview-match.ts +30 -102
  101. package/src/router/request-classification.ts +310 -0
  102. package/src/router/revalidation.ts +27 -7
  103. package/src/router/route-snapshot.ts +245 -0
  104. package/src/router/router-context.ts +6 -1
  105. package/src/router/router-interfaces.ts +50 -5
  106. package/src/router/router-options.ts +50 -19
  107. package/src/router/segment-resolution/fresh.ts +200 -19
  108. package/src/router/segment-resolution/helpers.ts +30 -25
  109. package/src/router/segment-resolution/loader-cache.ts +1 -0
  110. package/src/router/segment-resolution/revalidation.ts +429 -301
  111. package/src/router/segment-wrappers.ts +2 -0
  112. package/src/router/trie-matching.ts +20 -2
  113. package/src/router/types.ts +1 -0
  114. package/src/router.ts +88 -15
  115. package/src/rsc/handler.ts +546 -359
  116. package/src/rsc/index.ts +0 -20
  117. package/src/rsc/manifest-init.ts +5 -1
  118. package/src/rsc/progressive-enhancement.ts +25 -8
  119. package/src/rsc/rsc-rendering.ts +35 -43
  120. package/src/rsc/server-action.ts +16 -10
  121. package/src/rsc/ssr-setup.ts +128 -0
  122. package/src/rsc/types.ts +10 -1
  123. package/src/search-params.ts +16 -13
  124. package/src/segment-system.tsx +140 -4
  125. package/src/server/context.ts +148 -16
  126. package/src/server/loader-registry.ts +9 -8
  127. package/src/server/request-context.ts +182 -34
  128. package/src/server.ts +6 -0
  129. package/src/ssr/index.tsx +4 -0
  130. package/src/static-handler.ts +18 -6
  131. package/src/theme/index.ts +4 -13
  132. package/src/types/cache-types.ts +4 -4
  133. package/src/types/handler-context.ts +149 -49
  134. package/src/types/loader-types.ts +36 -9
  135. package/src/types/route-config.ts +17 -8
  136. package/src/types/route-entry.ts +8 -1
  137. package/src/types/segments.ts +2 -5
  138. package/src/urls/path-helper-types.ts +9 -2
  139. package/src/urls/path-helper.ts +48 -13
  140. package/src/urls/pattern-types.ts +12 -0
  141. package/src/urls/response-types.ts +16 -6
  142. package/src/use-loader.tsx +73 -4
  143. package/src/vite/discovery/bundle-postprocess.ts +61 -89
  144. package/src/vite/discovery/discover-routers.ts +23 -5
  145. package/src/vite/discovery/prerender-collection.ts +48 -15
  146. package/src/vite/discovery/state.ts +17 -13
  147. package/src/vite/index.ts +8 -3
  148. package/src/vite/plugin-types.ts +51 -79
  149. package/src/vite/plugins/client-ref-dedup.ts +115 -0
  150. package/src/vite/plugins/expose-action-id.ts +1 -3
  151. package/src/vite/plugins/performance-tracks.ts +88 -0
  152. package/src/vite/plugins/refresh-cmd.ts +127 -0
  153. package/src/vite/plugins/version-plugin.ts +13 -1
  154. package/src/vite/rango.ts +174 -211
  155. package/src/vite/router-discovery.ts +169 -42
  156. package/src/vite/utils/banner.ts +3 -3
  157. package/src/vite/utils/prerender-utils.ts +78 -0
  158. package/src/vite/utils/shared-utils.ts +3 -2
  159. package/skills/testing/SKILL.md +0 -226
  160. package/src/route-definition/route-function.ts +0 -119
@@ -84,6 +84,7 @@ export async function evaluateRevalidation<TEnv>(
84
84
  } = options;
85
85
  const nextParams = segment.params || {};
86
86
  const paramsChanged = !paramsEqual(nextParams, prevParams);
87
+ const searchChanged = prevUrl.search !== nextUrl.search;
87
88
 
88
89
  // Trace helper: push a structured entry to the request-scoped trace buffer.
89
90
  // Guarded by isTraceActive() so object construction is skipped in production.
@@ -134,19 +135,38 @@ export async function evaluateRevalidation<TEnv>(
134
135
  // Only the route segment revalidates by default - all others require explicit opt-in
135
136
 
136
137
  if (segment.type === "route") {
137
- // Route segments revalidate when params change
138
- // Routes are the primary param-dependent content and always need updates
139
- defaultShouldRevalidate = paramsChanged;
138
+ // Route segments revalidate when path params OR search params change.
139
+ // Search params (e.g., ?page=2&sort=price) are server-parsed via ctx.search,
140
+ // so the handler must re-execute to produce updated content.
141
+ const routeChanged = paramsChanged || searchChanged;
142
+ defaultShouldRevalidate = routeChanged;
140
143
  defaultReason = paramsChanged
141
144
  ? "nav:params-changed"
142
- : "nav:params-unchanged";
143
- if (paramsChanged) {
144
- debugLog("revalidation", "route params changed, revalidating", {
145
+ : searchChanged
146
+ ? "nav:search-changed"
147
+ : "nav:params-unchanged";
148
+ if (routeChanged) {
149
+ debugLog("revalidation", "route revalidating", {
145
150
  segmentId: segment.id,
151
+ paramsChanged,
152
+ searchChanged,
146
153
  });
147
154
  }
155
+ } else if (segment.belongsToRoute && (paramsChanged || searchChanged)) {
156
+ // Children of the route path (loaders, orphan layouts/parallels)
157
+ // revalidate when path params or search params change
158
+ defaultShouldRevalidate = true;
159
+ defaultReason = paramsChanged
160
+ ? "nav:route-child-params-changed"
161
+ : "nav:route-child-search-changed";
162
+ debugLog("revalidation", "route child revalidating", {
163
+ segmentId: segment.id,
164
+ segmentType: segment.type,
165
+ paramsChanged,
166
+ searchChanged,
167
+ });
148
168
  } else {
149
- // Layouts and parallels default to no revalidation
169
+ // Parent layouts and parallels default to no revalidation
150
170
  // Cannot assume these segments depend on params without explicit declaration
151
171
  // Use custom revalidation functions to opt-in when needed
152
172
  defaultShouldRevalidate = false;
@@ -0,0 +1,245 @@
1
+ /**
2
+ * Route Snapshot
3
+ *
4
+ * Pure data type representing the fully-resolved state of a single route match.
5
+ * Consolidates the duplicated findMatch + loadManifest + collectRouteMiddleware +
6
+ * cacheScope derivation that previously lived separately in preview-match.ts
7
+ * and match-api.ts.
8
+ *
9
+ * resolveRoute() is the factory: given a pathname and dependencies, it returns
10
+ * a RouteSnapshot (or redirect/null). Consumers (createMatchContextForFull,
11
+ * createMatchContextForPartial, previewMatch) read snapshot fields instead of
12
+ * re-deriving them.
13
+ */
14
+
15
+ import type { CacheScope } from "../cache/cache-scope.js";
16
+ import { createCacheScope } from "../cache/cache-scope.js";
17
+ import type { EntryData, MetricsStore } from "../server/context.js";
18
+ import { loadManifest } from "./manifest.js";
19
+ import { collectRouteMiddleware } from "./middleware.js";
20
+ import type { CollectedMiddleware } from "./middleware-types.js";
21
+ import { traverseBack } from "./pattern-matching.js";
22
+ import type { RouteMatchResult } from "./pattern-matching.js";
23
+
24
+ /**
25
+ * Immutable snapshot of a resolved route match.
26
+ *
27
+ * Contains everything derivable from (pathname, findMatch, loadManifest)
28
+ * without request context, navigation state, or intercept logic.
29
+ */
30
+ export interface RouteSnapshot<TEnv = any> {
31
+ /** Raw match result from the trie/pattern matcher */
32
+ matched: RouteMatchResult<TEnv>;
33
+ /** Resolved manifest entry (with loaded handler, loader, etc.) */
34
+ manifestEntry: EntryData;
35
+ /** All entries in the route chain (from traverseBack) */
36
+ entries: EntryData[];
37
+ /** Canonical route key (e.g. "blog.detail") */
38
+ routeKey: string;
39
+ /** Last segment of a dotted route key (e.g. "detail" from "blog.detail") */
40
+ localRouteName: string;
41
+ /** Extracted route params */
42
+ params: Record<string, string>;
43
+ /** Collected route-level middleware from the entry tree */
44
+ routeMiddleware: CollectedMiddleware[];
45
+ /** Merged cache scope from the entry chain */
46
+ cacheScope: CacheScope | null;
47
+ /** Whether the matched route is a passthrough route */
48
+ isPassthrough: boolean;
49
+ /** Response type for non-RSC routes (e.g. "application/json") */
50
+ responseType?: string;
51
+ }
52
+
53
+ export type ResolveRouteResult<TEnv = any> =
54
+ | { type: "match"; snapshot: RouteSnapshot<TEnv> }
55
+ | { type: "redirect"; redirectTo: string }
56
+ | null;
57
+
58
+ export interface ResolveRouteDeps<TEnv = any> {
59
+ findMatch: (pathname: string) => RouteMatchResult<TEnv> | null;
60
+ metricsStore?: MetricsStore;
61
+ isSSR?: boolean;
62
+ /**
63
+ * When true, skip entries array and cacheScope chain construction.
64
+ * Used by previewMatch which only needs matched, manifestEntry,
65
+ * routeMiddleware, and responseType — avoids an extra traverseBack
66
+ * allocation and cacheScope composition on the hot classification path.
67
+ */
68
+ lite?: boolean;
69
+ /**
70
+ * When true, skip pushing the "route-matching" metric internally.
71
+ * Used by createMatchContextForPartial on the fresh path (no snapshot
72
+ * reuse) so it can measure current + prev + intercept-source findMatch
73
+ * calls under one combined "route-matching" metric. On the reuse path,
74
+ * the partial path emits "route-matching:nav" for the prev +
75
+ * intercept-source lookups only (current-route resolution was done
76
+ * during classification without metrics).
77
+ */
78
+ skipRouteMatchMetric?: boolean;
79
+ }
80
+
81
+ /**
82
+ * Resolve a pathname into a RouteSnapshot.
83
+ *
84
+ * This is the single source of truth for route derivation. It performs:
85
+ * 1. findMatch(pathname)
86
+ * 2. Redirect check
87
+ * 3. loadManifest
88
+ * 4. Passthrough detection
89
+ * 5. collectRouteMiddleware
90
+ * 6. Cache scope chain
91
+ * 7. responseType + localRouteName extraction
92
+ *
93
+ * Metrics timing is preserved identically to the previous inline code.
94
+ */
95
+ export async function resolveRoute<TEnv = any>(
96
+ pathname: string,
97
+ deps: ResolveRouteDeps<TEnv>,
98
+ ): Promise<ResolveRouteResult<TEnv>> {
99
+ const {
100
+ metricsStore,
101
+ isSSR = false,
102
+ lite = false,
103
+ skipRouteMatchMetric = false,
104
+ } = deps;
105
+
106
+ const routeMatchStart =
107
+ metricsStore && !skipRouteMatchMetric ? performance.now() : 0;
108
+ const matched = deps.findMatch(pathname);
109
+ if (metricsStore && !skipRouteMatchMetric) {
110
+ metricsStore.metrics.push({
111
+ label: "route-matching",
112
+ duration: performance.now() - routeMatchStart,
113
+ startTime: routeMatchStart - metricsStore.requestStart,
114
+ });
115
+ }
116
+
117
+ if (!matched) {
118
+ return null;
119
+ }
120
+
121
+ if (matched.redirectTo) {
122
+ return { type: "redirect", redirectTo: matched.redirectTo };
123
+ }
124
+
125
+ const manifestStart = metricsStore ? performance.now() : 0;
126
+ const manifestEntry = await loadManifest(
127
+ matched.entry,
128
+ matched.routeKey,
129
+ pathname,
130
+ metricsStore,
131
+ isSSR,
132
+ );
133
+ if (metricsStore) {
134
+ metricsStore.metrics.push({
135
+ label: "manifest-loading",
136
+ duration: performance.now() - manifestStart,
137
+ startTime: manifestStart - metricsStore.requestStart,
138
+ });
139
+ }
140
+
141
+ const isPassthrough =
142
+ manifestEntry.type === "route" && manifestEntry.isPassthrough === true;
143
+
144
+ let entries: EntryData[];
145
+ let cacheScope: CacheScope | null = null;
146
+ if (lite) {
147
+ entries = [];
148
+ } else {
149
+ ({ entries, cacheScope } = buildEntriesAndCacheScope(manifestEntry));
150
+ }
151
+
152
+ const routeMiddleware = collectRouteMiddleware(
153
+ lite ? traverseBack(manifestEntry) : entries,
154
+ matched.params,
155
+ );
156
+
157
+ const responseType =
158
+ matched.responseType ||
159
+ (manifestEntry.type === "route" ? manifestEntry.responseType : undefined);
160
+
161
+ const localRouteName = matched.routeKey.includes(".")
162
+ ? matched.routeKey.split(".").pop()!
163
+ : matched.routeKey;
164
+
165
+ return {
166
+ type: "match",
167
+ snapshot: {
168
+ matched,
169
+ manifestEntry,
170
+ entries,
171
+ routeKey: matched.routeKey,
172
+ localRouteName,
173
+ params: matched.params,
174
+ routeMiddleware,
175
+ cacheScope,
176
+ isPassthrough,
177
+ responseType,
178
+ },
179
+ };
180
+ }
181
+
182
+ /**
183
+ * Fill in the entries and cacheScope fields on a lite snapshot.
184
+ *
185
+ * When classifyRequest produces a lite snapshot (entries=[], cacheScope=null),
186
+ * this function computes the missing fields from manifestEntry without
187
+ * re-running findMatch, loadManifest, or collectRouteMiddleware.
188
+ *
189
+ * If the snapshot already has entries, returns it as-is.
190
+ */
191
+ export function ensureFullRouteSnapshot<TEnv = any>(
192
+ snapshot: RouteSnapshot<TEnv>,
193
+ ): RouteSnapshot<TEnv> {
194
+ if (snapshot.entries.length > 0) {
195
+ return snapshot;
196
+ }
197
+
198
+ const { entries, cacheScope } = buildEntriesAndCacheScope(
199
+ snapshot.manifestEntry,
200
+ );
201
+ return { ...snapshot, entries, cacheScope };
202
+ }
203
+
204
+ /**
205
+ * Materialize the entry chain and derive the merged cache scope.
206
+ * Shared by resolveRoute (non-lite) and ensureFullRouteSnapshot.
207
+ */
208
+ function buildEntriesAndCacheScope(manifestEntry: EntryData): {
209
+ entries: EntryData[];
210
+ cacheScope: CacheScope | null;
211
+ } {
212
+ const entries = [...traverseBack(manifestEntry)];
213
+ let cacheScope: CacheScope | null = null;
214
+ for (const entry of entries) {
215
+ if (entry.cache) {
216
+ cacheScope = createCacheScope(entry.cache, cacheScope);
217
+ }
218
+ }
219
+ return { entries, cacheScope };
220
+ }
221
+
222
+ /**
223
+ * Test helper: create a RouteSnapshot with sensible defaults and overrides.
224
+ */
225
+ export function createRouteSnapshot<TEnv = any>(
226
+ overrides?: Partial<RouteSnapshot<TEnv>>,
227
+ ): RouteSnapshot<TEnv> {
228
+ return {
229
+ matched: {
230
+ entry: {} as any,
231
+ routeKey: "test",
232
+ params: {},
233
+ optionalParams: new Set(),
234
+ } as RouteMatchResult<TEnv>,
235
+ manifestEntry: { type: "route", shortCode: "R0", parent: null } as any,
236
+ entries: [],
237
+ routeKey: "test",
238
+ localRouteName: "test",
239
+ params: {},
240
+ routeMiddleware: [],
241
+ cacheScope: null,
242
+ isPassthrough: false,
243
+ ...overrides,
244
+ };
245
+ }
@@ -138,6 +138,7 @@ export interface RouterContext<TEnv = any> {
138
138
  interceptResult: InterceptResult | null,
139
139
  localRouteName: string,
140
140
  pathname: string,
141
+ stale?: boolean,
141
142
  ) => Promise<{ segments: ResolvedSegment[]; matchedIds: string[] }>;
142
143
 
143
144
  // Generator-based segment resolution (for pipeline)
@@ -188,7 +189,10 @@ export interface RouterContext<TEnv = any> {
188
189
  | "cache-hit"
189
190
  | "loader"
190
191
  | "parallel"
191
- | "orphan-layout";
192
+ | "orphan-layout"
193
+ | "route-handler"
194
+ | "layout-handler"
195
+ | "intercept-loader";
192
196
  }) => Promise<boolean>;
193
197
 
194
198
  // Request context
@@ -206,6 +210,7 @@ export interface RouterContext<TEnv = any> {
206
210
  params: Record<string, string>,
207
211
  handlerContext: HandlerContext<any, TEnv>,
208
212
  loaderPromises: Map<string, Promise<any>>,
213
+ options?: { skipLoaders?: boolean },
209
214
  ) => Promise<ResolvedSegment[]>;
210
215
 
211
216
  // Generator-based simple resolution
@@ -2,6 +2,7 @@ import type { ComponentType, ReactNode } from "react";
2
2
  import type { SerializedManifest } from "../debug.js";
3
3
  import type { ReverseFunction } from "../reverse.js";
4
4
  import type { UrlPatterns } from "../urls.js";
5
+ import type { UrlBuilder } from "../urls/pattern-types.js";
5
6
  import type { EntryData } from "../server/context";
6
7
  import type { ErrorInfo, MatchResult } from "../types";
7
8
  import type { NonceProvider } from "../rsc/types.js";
@@ -68,12 +69,24 @@ export interface RSCRouter<
68
69
  readonly id: string;
69
70
 
70
71
  /**
71
- * Register routes using URL patterns from urls()
72
+ * URL prefix applied to all routes. Undefined when no basename is configured.
73
+ */
74
+ readonly basename: string | undefined;
75
+
76
+ /**
77
+ * Register routes using URL patterns from urls() or a builder function
72
78
  *
73
79
  * @example
74
80
  * ```typescript
75
- * createRouter({})
76
- * .routes(urlpatterns)
81
+ * // With urls()
82
+ * createRouter({}).routes(urlpatterns)
83
+ *
84
+ * // With builder function (urls() is implicit)
85
+ * createRouter({}).routes(({ path, layout }) => [
86
+ * layout(RootLayout, () => [
87
+ * path("/", HomePage),
88
+ * ]),
89
+ * ])
77
90
  * ```
78
91
  */
79
92
  routes<T extends UrlPatterns<TEnv, any>>(
@@ -85,6 +98,7 @@ export interface RSCRouter<
85
98
  ? MergeRoutesWithResponses<NonNullable<T["_routes"]>, T["_responses"]>
86
99
  : Record<string, string>)
87
100
  >;
101
+ routes(builder: UrlBuilder<TEnv>): RSCRouter<TEnv, TRoutes>;
88
102
 
89
103
  /**
90
104
  * Add global middleware that runs on all routes
@@ -188,8 +202,11 @@ export interface RSCRouterInternal<
188
202
  */
189
203
  readonly id: string;
190
204
 
205
+ /** URL prefix applied to all routes. */
206
+ readonly basename: string | undefined;
207
+
191
208
  /**
192
- * Register routes using URL patterns from urls()
209
+ * Register routes using URL patterns from urls() or a builder function
193
210
  */
194
211
  routes<T extends UrlPatterns<TEnv, any>>(
195
212
  patterns: T,
@@ -200,6 +217,7 @@ export interface RSCRouterInternal<
200
217
  ? MergeRoutesWithResponses<NonNullable<T["_routes"]>, T["_responses"]>
201
218
  : Record<string, string>)
202
219
  >;
220
+ routes(builder: UrlBuilder<TEnv>): RSCRouter<TEnv, TRoutes>;
203
221
 
204
222
  /**
205
223
  * Add global middleware that runs on all routes
@@ -258,10 +276,17 @@ export interface RSCRouterInternal<
258
276
 
259
277
  /**
260
278
  * Cache-Control header value for prefetch responses.
261
- * False means no browser caching of prefetch responses.
279
+ * False means no caching of prefetch responses.
280
+ * Derived from prefetchCacheTTL.
262
281
  */
263
282
  readonly prefetchCacheControl: string | false;
264
283
 
284
+ /**
285
+ * TTL in milliseconds for the client-side in-memory prefetch cache.
286
+ * 0 means caching is disabled.
287
+ */
288
+ readonly prefetchCacheTTL: number;
289
+
265
290
  /**
266
291
  * Whether connection warmup is enabled.
267
292
  * When true, the client sends HEAD /?_rsc_warmup after idle periods
@@ -269,6 +294,12 @@ export interface RSCRouterInternal<
269
294
  */
270
295
  readonly warmupEnabled: boolean;
271
296
 
297
+ /**
298
+ * Whether router-wide performance debugging is enabled.
299
+ * Used by the request handler to create metrics before middleware runs.
300
+ */
301
+ readonly debugPerformance?: boolean;
302
+
272
303
  /**
273
304
  * Whether ?__debug_manifest is allowed in production.
274
305
  * Always enabled in development.
@@ -325,6 +356,9 @@ export interface RSCRouterInternal<
325
356
  */
326
357
  readonly __sourceFile?: string;
327
358
 
359
+ /** @internal basename for runtime manifest generation */
360
+ readonly __basename?: string;
361
+
328
362
  match(
329
363
  request: Request,
330
364
  input?: RouterRequestInput<TEnv>,
@@ -340,6 +374,8 @@ export interface RSCRouterInternal<
340
374
  params: Record<string, string>,
341
375
  buildVars?: Record<string, any>,
342
376
  isPassthroughRoute?: boolean,
377
+ buildEnv?: any,
378
+ devMode?: boolean,
343
379
  ): Promise<{
344
380
  segments: SerializedSegmentData[];
345
381
  handles: Record<string, SegmentHandleData>;
@@ -358,6 +394,8 @@ export interface RSCRouterInternal<
358
394
  handler: Function,
359
395
  handlerId: string,
360
396
  routeName?: string,
397
+ buildEnv?: any,
398
+ devMode?: boolean,
361
399
  ): Promise<{ encoded: string; handles: Record<string, unknown[]> } | null>;
362
400
 
363
401
  /**
@@ -411,6 +449,13 @@ export interface RSCRouterInternal<
411
449
  segmentType?: ErrorInfo["segmentType"],
412
450
  ): Promise<MatchResult | null>;
413
451
 
452
+ /**
453
+ * Low-level route matching function.
454
+ * Used by classifyRequest() for request classification without
455
+ * entering the full match pipeline.
456
+ */
457
+ findMatch(pathname: string, metricsStore?: any): any;
458
+
414
459
  /**
415
460
  * Debug utility to serialize the manifest for inspection
416
461
  * Returns a JSON-friendly representation of all routes and layouts
@@ -8,6 +8,7 @@ import type {
8
8
  import type { NonceProvider } from "../rsc/types.js";
9
9
  import type { ExecutionContext } from "../server/request-context.js";
10
10
  import type { UrlPatterns } from "../urls.js";
11
+ import type { UrlBuilder } from "../urls/pattern-types.js";
11
12
  import type { NamedRouteEntry } from "./content-negotiation.js";
12
13
  import type { TelemetrySink } from "./telemetry.js";
13
14
  import type { RouterTimeouts, OnTimeoutCallback } from "./timeout.js";
@@ -95,6 +96,28 @@ export interface RSCRouterOptions<TEnv = any> {
95
96
  */
96
97
  $$sourceFile?: string;
97
98
 
99
+ /**
100
+ * URL prefix applied to all routes registered with this router.
101
+ *
102
+ * Useful when the app is served under a sub-path (e.g. `/admin` or `/v2`).
103
+ * All `path()` patterns are automatically prefixed and `reverse()` returns
104
+ * full paths including the basename. Route names are NOT prefixed.
105
+ *
106
+ * @example
107
+ * ```typescript
108
+ * const router = createRouter({
109
+ * basename: "/admin",
110
+ * }).routes(({ path }) => [
111
+ * path("/", Dashboard, { name: "home" }), // matches /admin
112
+ * path("/users", Users, { name: "users" }), // matches /admin/users
113
+ * ]);
114
+ *
115
+ * router.reverse("home"); // "/admin"
116
+ * router.reverse("users"); // "/admin/users"
117
+ * ```
118
+ */
119
+ basename?: string;
120
+
98
121
  /**
99
122
  * Enable performance metrics collection
100
123
  * When enabled, metrics are output to console and available via Server-Timing header
@@ -239,7 +262,7 @@ export interface RSCRouterOptions<TEnv = any> {
239
262
  *
240
263
  * @example Static config
241
264
  * ```typescript
242
- * import { MemorySegmentCacheStore } from "rsc-router/rsc";
265
+ * import { MemorySegmentCacheStore } from "@rangojs/router/cache";
243
266
  *
244
267
  * const router = createRouter({
245
268
  * cache: {
@@ -337,25 +360,28 @@ export interface RSCRouterOptions<TEnv = any> {
337
360
  /**
338
361
  * URL patterns to register with the router.
339
362
  *
340
- * Alternative to calling `.routes()` method - allows passing patterns
341
- * directly in the config for a more concise setup.
363
+ * Accepts either a `UrlPatterns` object from `urls()` or a builder function
364
+ * directly (urls() is called implicitly).
342
365
  *
343
366
  * @example
344
367
  * ```typescript
345
- * import { urls } from "@rangojs/router/server";
346
- *
347
- * const urlpatterns = urls(({ path, layout }) => [
348
- * path("/", HomePage, { name: "home" }),
349
- * path("/about", AboutPage, { name: "about" }),
350
- * ]);
351
- *
352
- * const router = createRouter<AppEnv>({
368
+ * // With urls()
369
+ * createRouter<AppEnv>({
353
370
  * document: Document,
354
371
  * urls: urlpatterns,
355
372
  * });
373
+ *
374
+ * // With builder function
375
+ * createRouter<AppEnv>({
376
+ * document: Document,
377
+ * urls: ({ path }) => [
378
+ * path("/", HomePage, { name: "home" }),
379
+ * path("/about", AboutPage, { name: "about" }),
380
+ * ],
381
+ * });
356
382
  * ```
357
383
  */
358
- urls?: UrlPatterns<TEnv, any>;
384
+ urls?: UrlPatterns<TEnv, any> | UrlBuilder<TEnv>;
359
385
 
360
386
  /**
361
387
  * Injected by the Vite transform at compile time.
@@ -415,16 +441,21 @@ export interface RSCRouterOptions<TEnv = any> {
415
441
  version?: string;
416
442
 
417
443
  /**
418
- * Cache-Control header value for prefetch responses.
419
- * Only applied to non-intercept partial responses that include the
420
- * `X-Rango-Prefetch` header (sent by the Link component's prefetch fetch).
421
- * Navigation responses are never cached by the browser.
444
+ * TTL (in seconds) for the in-memory prefetch cache and the
445
+ * Cache-Control header on prefetch responses.
446
+ *
447
+ * Controls how long prefetch responses are kept in the client-side
448
+ * in-memory cache and sets `Cache-Control: private, max-age=<ttl>`
449
+ * on server responses for CDN/edge caching.
450
+ *
451
+ * The cache is automatically invalidated on server actions regardless
452
+ * of TTL, so this is primarily a staleness safety net.
422
453
  *
423
- * Set to `false` to disable browser caching of prefetch responses entirely.
454
+ * Set to `false` to disable prefetch caching entirely.
424
455
  *
425
- * @default "private, max-age=300"
456
+ * @default 300 (5 minutes)
426
457
  */
427
- prefetchCacheControl?: string | false;
458
+ prefetchCacheTTL?: number | false;
428
459
 
429
460
  /**
430
461
  * Enable connection warmup to keep TCP+TLS alive after idle periods.