@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
@@ -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.