@rangojs/router 0.0.0-experimental.78a48627 → 0.0.0-experimental.79

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 (147) hide show
  1. package/README.md +76 -18
  2. package/dist/bin/rango.js +138 -50
  3. package/dist/vite/index.js +853 -435
  4. package/dist/vite/index.js.bak +5448 -0
  5. package/package.json +16 -17
  6. package/skills/cache-guide/SKILL.md +32 -0
  7. package/skills/caching/SKILL.md +45 -4
  8. package/skills/handler-use/SKILL.md +362 -0
  9. package/skills/intercept/SKILL.md +20 -0
  10. package/skills/layout/SKILL.md +22 -0
  11. package/skills/links/SKILL.md +3 -1
  12. package/skills/loader/SKILL.md +53 -43
  13. package/skills/middleware/SKILL.md +34 -3
  14. package/skills/migrate-nextjs/SKILL.md +560 -0
  15. package/skills/migrate-react-router/SKILL.md +764 -0
  16. package/skills/parallel/SKILL.md +185 -0
  17. package/skills/prerender/SKILL.md +110 -68
  18. package/skills/rango/SKILL.md +24 -22
  19. package/skills/route/SKILL.md +55 -0
  20. package/skills/router-setup/SKILL.md +87 -2
  21. package/skills/typesafety/SKILL.md +10 -0
  22. package/src/__internal.ts +1 -1
  23. package/src/browser/app-version.ts +14 -0
  24. package/src/browser/event-controller.ts +5 -0
  25. package/src/browser/navigation-bridge.ts +37 -5
  26. package/src/browser/navigation-client.ts +142 -57
  27. package/src/browser/navigation-store.ts +43 -8
  28. package/src/browser/partial-update.ts +63 -22
  29. package/src/browser/prefetch/cache.ts +73 -11
  30. package/src/browser/prefetch/fetch.ts +98 -27
  31. package/src/browser/prefetch/queue.ts +92 -20
  32. package/src/browser/prefetch/resource-ready.ts +77 -0
  33. package/src/browser/react/Link.tsx +76 -9
  34. package/src/browser/react/NavigationProvider.tsx +16 -7
  35. package/src/browser/react/context.ts +7 -2
  36. package/src/browser/react/use-handle.ts +9 -58
  37. package/src/browser/react/use-router.ts +21 -8
  38. package/src/browser/rsc-router.tsx +134 -59
  39. package/src/browser/scroll-restoration.ts +21 -18
  40. package/src/browser/segment-reconciler.ts +36 -9
  41. package/src/browser/server-action-bridge.ts +8 -6
  42. package/src/browser/types.ts +27 -5
  43. package/src/build/generate-manifest.ts +6 -6
  44. package/src/build/generate-route-types.ts +3 -0
  45. package/src/build/route-trie.ts +50 -24
  46. package/src/build/route-types/include-resolution.ts +8 -1
  47. package/src/build/route-types/router-processing.ts +223 -74
  48. package/src/build/route-types/scan-filter.ts +8 -1
  49. package/src/cache/cache-runtime.ts +15 -11
  50. package/src/cache/cache-scope.ts +48 -7
  51. package/src/cache/cf/cf-cache-store.ts +453 -11
  52. package/src/cache/cf/index.ts +5 -1
  53. package/src/cache/document-cache.ts +17 -7
  54. package/src/cache/index.ts +1 -0
  55. package/src/cache/taint.ts +55 -0
  56. package/src/client.tsx +84 -230
  57. package/src/context-var.ts +72 -2
  58. package/src/debug.ts +2 -2
  59. package/src/handle.ts +40 -0
  60. package/src/index.rsc.ts +3 -1
  61. package/src/index.ts +46 -6
  62. package/src/prerender/store.ts +5 -4
  63. package/src/prerender.ts +138 -77
  64. package/src/reverse.ts +25 -1
  65. package/src/route-definition/dsl-helpers.ts +224 -37
  66. package/src/route-definition/helpers-types.ts +67 -19
  67. package/src/route-definition/index.ts +3 -0
  68. package/src/route-definition/redirect.ts +11 -3
  69. package/src/route-definition/resolve-handler-use.ts +149 -0
  70. package/src/route-types.ts +18 -0
  71. package/src/router/content-negotiation.ts +100 -1
  72. package/src/router/handler-context.ts +82 -23
  73. package/src/router/intercept-resolution.ts +9 -4
  74. package/src/router/lazy-includes.ts +7 -6
  75. package/src/router/loader-resolution.ts +156 -21
  76. package/src/router/logging.ts +1 -1
  77. package/src/router/manifest.ts +28 -15
  78. package/src/router/match-api.ts +124 -189
  79. package/src/router/match-middleware/background-revalidation.ts +30 -2
  80. package/src/router/match-middleware/cache-lookup.ts +94 -17
  81. package/src/router/match-middleware/cache-store.ts +53 -10
  82. package/src/router/match-middleware/intercept-resolution.ts +9 -7
  83. package/src/router/match-middleware/segment-resolution.ts +60 -5
  84. package/src/router/match-result.ts +104 -10
  85. package/src/router/metrics.ts +6 -1
  86. package/src/router/middleware-types.ts +6 -8
  87. package/src/router/middleware.ts +4 -6
  88. package/src/router/navigation-snapshot.ts +182 -0
  89. package/src/router/prerender-match.ts +110 -10
  90. package/src/router/preview-match.ts +30 -102
  91. package/src/router/request-classification.ts +310 -0
  92. package/src/router/route-snapshot.ts +245 -0
  93. package/src/router/router-context.ts +1 -0
  94. package/src/router/router-interfaces.ts +36 -4
  95. package/src/router/router-options.ts +37 -11
  96. package/src/router/segment-resolution/fresh.ts +198 -20
  97. package/src/router/segment-resolution/helpers.ts +29 -24
  98. package/src/router/segment-resolution/loader-cache.ts +1 -0
  99. package/src/router/segment-resolution/revalidation.ts +433 -296
  100. package/src/router/types.ts +1 -0
  101. package/src/router.ts +55 -6
  102. package/src/rsc/handler.ts +472 -372
  103. package/src/rsc/loader-fetch.ts +23 -3
  104. package/src/rsc/manifest-init.ts +5 -1
  105. package/src/rsc/progressive-enhancement.ts +14 -2
  106. package/src/rsc/rsc-rendering.ts +10 -1
  107. package/src/rsc/server-action.ts +8 -0
  108. package/src/rsc/ssr-setup.ts +2 -2
  109. package/src/rsc/types.ts +9 -1
  110. package/src/segment-content-promise.ts +67 -0
  111. package/src/segment-loader-promise.ts +122 -0
  112. package/src/segment-system.tsx +109 -23
  113. package/src/server/context.ts +166 -17
  114. package/src/server/handle-store.ts +19 -0
  115. package/src/server/loader-registry.ts +9 -8
  116. package/src/server/request-context.ts +185 -19
  117. package/src/ssr/index.tsx +4 -0
  118. package/src/static-handler.ts +18 -6
  119. package/src/types/cache-types.ts +4 -4
  120. package/src/types/handler-context.ts +137 -33
  121. package/src/types/loader-types.ts +36 -9
  122. package/src/types/route-entry.ts +12 -1
  123. package/src/types/segments.ts +2 -0
  124. package/src/urls/include-helper.ts +24 -14
  125. package/src/urls/path-helper-types.ts +39 -6
  126. package/src/urls/path-helper.ts +48 -13
  127. package/src/urls/pattern-types.ts +12 -0
  128. package/src/urls/response-types.ts +16 -6
  129. package/src/use-loader.tsx +77 -5
  130. package/src/vite/discovery/bundle-postprocess.ts +30 -33
  131. package/src/vite/discovery/discover-routers.ts +5 -1
  132. package/src/vite/discovery/prerender-collection.ts +128 -74
  133. package/src/vite/discovery/state.ts +13 -6
  134. package/src/vite/index.ts +4 -0
  135. package/src/vite/plugin-types.ts +51 -79
  136. package/src/vite/plugins/expose-action-id.ts +1 -3
  137. package/src/vite/plugins/expose-id-utils.ts +12 -0
  138. package/src/vite/plugins/expose-ids/handler-transform.ts +30 -0
  139. package/src/vite/plugins/expose-internal-ids.ts +257 -40
  140. package/src/vite/plugins/performance-tracks.ts +88 -0
  141. package/src/vite/plugins/refresh-cmd.ts +88 -26
  142. package/src/vite/plugins/version-plugin.ts +13 -1
  143. package/src/vite/rango.ts +163 -211
  144. package/src/vite/router-discovery.ts +178 -45
  145. package/src/vite/utils/banner.ts +3 -3
  146. package/src/vite/utils/prerender-utils.ts +37 -5
  147. package/src/vite/utils/shared-utils.ts +3 -2
@@ -65,24 +65,10 @@ export const urlpatterns = urls(({ path, loader }) => [
65
65
 
66
66
  ## Consuming Loader Data
67
67
 
68
- Loaders are the **live data layer** they resolve fresh on every request.
69
- The way you consume them depends on whether you're in a server component
70
- (route handler) or a client component.
71
-
72
- > **IMPORTANT: Prefer consuming loaders in client components.** Keeping data
73
- > fetching in loaders and consumption in client components creates a clean
74
- > separation: the server-side handler renders static markup that can be
75
- > freely cached with `cache()`, while loader data stays fresh on every
76
- > request. When you consume loaders in server handlers via `ctx.use()`, the
77
- > handler output depends on the loader data, which means caching the handler
78
- > also caches the data — defeating the purpose of the live data layer.
79
-
80
- ### In Client Components (Preferred)
81
-
82
- Client components use `useLoader()` from `@rangojs/router/client`.
83
- The loader **must** be registered with `loader()` in the route's DSL
84
- segments so the framework knows to resolve it during SSR and stream
85
- the data to the client:
68
+ Register loaders with `loader()` in the DSL and consume them in client
69
+ components with `useLoader()`. This is the recommended pattern it keeps
70
+ data fetching on the server and consumption on the client, with a clean
71
+ separation that works correctly with `cache()`.
86
72
 
87
73
  ```typescript
88
74
  "use client";
@@ -96,40 +82,60 @@ function ProductDetails() {
96
82
  ```
97
83
 
98
84
  ```typescript
99
- // Route definition — loader() registration required for client consumption
85
+ // Route definition — loader() registration required
100
86
  path("/product/:slug", ProductPage, { name: "product" }, () => [
101
- loader(ProductLoader), // Required for useLoader() in client components
87
+ loader(ProductLoader),
102
88
  ]);
103
89
  ```
104
90
 
105
- ### In Route Handlers (Server Components)
91
+ DSL loaders are the **live data layer** — they resolve fresh on every
92
+ request, even when the route is inside a `cache()` boundary. The router
93
+ excludes them from the segment cache at storage time and re-resolves them
94
+ on retrieval. This means `cache()` gives you cached UI + fresh data by
95
+ default.
106
96
 
107
- In server components, use `ctx.use(Loader)` directly in the route handler.
108
- This doesn't require `loader()` registration in the DSL — it works
109
- standalone. **However**, prefer client-side consumption when possible (see
110
- note above).
97
+ ### Cache safety
111
98
 
112
- ```typescript
113
- import { ProductLoader } from "./loaders/product";
99
+ DSL loaders can safely read `createVar({ cache: false })` variables
100
+ because they are always resolved fresh. The read guard is bypassed for
101
+ loader functions — they never produce stale data.
102
+
103
+ ### ctx.use(Loader) — escape hatch
104
+
105
+ For cases where you need loader data in the server handler itself (e.g.,
106
+ to set ctx variables or make routing decisions), use `ctx.use(Loader)`:
114
107
 
115
- // Route handler — server component
108
+ ```typescript
116
109
  path("/product/:slug", async (ctx) => {
117
110
  const { product } = await ctx.use(ProductLoader);
118
- return <h1>{product.name}</h1>;
119
- }, { name: "product" })
111
+ ctx.set(Product, product); // make available to children
112
+ return <ProductPage />;
113
+ }, { name: "product" }, () => [
114
+ loader(ProductLoader), // still register for client consumption
115
+ ])
120
116
  ```
121
117
 
122
- When you do register with `loader()` in the DSL, `ctx.use()` returns the
118
+ When you register with `loader()` in the DSL, `ctx.use()` returns the
123
119
  same memoized result — loaders never run twice per request.
124
120
 
121
+ **Limitations of ctx.use(Loader):**
122
+
123
+ - The handler output depends on the loader data. If the route is inside
124
+ `cache()`, the handler is cached with the loader result baked in —
125
+ defeating the live data guarantee.
126
+ - Non-cacheable variable reads (`createVar({ cache: false })`) inside the
127
+ handler still throw, even if the data came from a loader.
128
+ - Prefer DSL `loader()` + client `useLoader()` for data that depends on
129
+ non-cacheable context variables.
130
+
125
131
  **Never use `useLoader()` in server components** — it is a client-only API.
126
132
 
127
133
  ### Summary
128
134
 
129
- | Context | API | `loader()` DSL required? |
130
- | ---------------------------- | ------------------- | ------------------------ |
131
- | Client component (preferred) | `useLoader(Loader)` | **Yes** |
132
- | Route handler (server) | `ctx.use(Loader)` | No |
135
+ | Pattern | API | Cache-safe | Recommended |
136
+ | ---------------------- | ------------------- | ---------- | ----------- |
137
+ | DSL + client component | `useLoader(Loader)` | Yes | Yes |
138
+ | Handler escape hatch | `ctx.use(Loader)` | No | When needed |
133
139
 
134
140
  ## Loader Context
135
141
 
@@ -548,7 +554,7 @@ export const ProductLoader = createLoader(async (ctx) => {
548
554
  .first();
549
555
 
550
556
  if (!product) {
551
- throw new Response("Product not found", { status: 404 });
557
+ notFound("Product not found");
552
558
  }
553
559
 
554
560
  return { product };
@@ -564,10 +570,9 @@ export const CartLoader = createLoader(async (ctx) => {
564
570
  return { cart };
565
571
  });
566
572
 
567
- // urls.tsx
573
+ // urls.tsx — register loaders in the DSL
568
574
  export const urlpatterns = urls(({ path, layout, loader, loading, cache, revalidate }) => [
569
575
  layout(<ShopLayout />, () => [
570
- // Shared cart loader for all shop routes
571
576
  loader(CartLoader, () => [
572
577
  revalidate(({ actionId }) => actionId?.includes("Cart") ?? false),
573
578
  ]),
@@ -579,17 +584,22 @@ export const urlpatterns = urls(({ path, layout, loader, loading, cache, revalid
579
584
  ]),
580
585
  ]);
581
586
 
582
- // pages/product.tsx — server component (route handler)
587
+ // components/ProductDetails.tsx — consume in client component
588
+ "use client";
589
+ import { useLoader } from "@rangojs/router/client";
583
590
  import { ProductLoader, CartLoader } from "./loaders/shop";
584
591
 
585
- async function ProductPage(ctx) {
586
- const { product } = await ctx.use(ProductLoader);
587
- const { cart } = await ctx.use(CartLoader);
592
+ function ProductDetails() {
593
+ const { data: { product } } = useLoader(ProductLoader);
594
+ const { data: { cart } } = useLoader(CartLoader);
588
595
 
589
596
  return (
590
597
  <div>
591
598
  <h1>{product.name}</h1>
592
- <AddToCartButton productId={product.id} inCart={cart?.items.includes(product.id)} />
599
+ <AddToCartButton
600
+ productId={product.id}
601
+ inCart={cart?.items.includes(product.id)}
602
+ />
593
603
  </div>
594
604
  );
595
605
  }
@@ -26,6 +26,8 @@ const router = createRouter<AppEnv>({})
26
26
  .routes(urlpatterns);
27
27
  ```
28
28
 
29
+ When the router has a `basename`, pattern-scoped `.use()` patterns are automatically prefixed. For example, with `basename: "/app"`, `.use("/admin/*", mw)` matches `/app/admin/*`.
30
+
29
31
  ### Route middleware (`middleware()` in `urls()`)
30
32
 
31
33
  Registered inside `urls()` callback. Wraps **rendering only** -- it does NOT wrap server action execution. Actions run before route middleware, so when route middleware executes during post-action revalidation, it can observe state that the action set (cookies, context variables, headers).
@@ -135,17 +137,46 @@ export const urlpatterns = urls(({ path, layout, middleware }) => [
135
137
  ## Middleware with Multiple Handlers
136
138
 
137
139
  ```typescript
138
- // Spread multiple middleware from a single export
140
+ // Group multiple middleware in an array
139
141
  export const shopMiddleware = [loggerMiddleware, mockAuthMiddleware];
140
142
 
141
- // In routes
143
+ // In routes — pass the array directly
142
144
  layout(<ShopLayout />, () => [
143
- middleware(...shopMiddleware),
145
+ middleware(shopMiddleware),
144
146
 
145
147
  path("/shop", ShopIndex, { name: "shop" }),
146
148
  ])
147
149
  ```
148
150
 
151
+ ## Wrapping Middleware (Scoped to Children)
152
+
153
+ Use the wrapping form to scope middleware to a subset of routes without
154
+ introducing a visible layout:
155
+
156
+ ```typescript
157
+ urls(({ path, middleware }) => [
158
+ // authMw only applies to /admin and /admin/settings
159
+ middleware(authMw, () => [
160
+ path("/admin", AdminPage, { name: "admin" }),
161
+ path("/admin/settings", SettingsPage, { name: "settings" }),
162
+ ]),
163
+
164
+ // Public route — no authMw
165
+ path("/", HomePage, { name: "home" }),
166
+ ]);
167
+ ```
168
+
169
+ Multiple middleware with wrapping:
170
+
171
+ ```typescript
172
+ middleware([authMw, loggingMw], () => [
173
+ path("/admin", AdminPage, { name: "admin" }),
174
+ ]);
175
+ ```
176
+
177
+ This creates a transparent layout (`<Outlet />`) that carries the middleware.
178
+ The middleware does not affect sibling routes outside the callback.
179
+
149
180
  ## Middleware Context
150
181
 
151
182
  ```typescript