@rangojs/router 0.0.0-experimental.121 → 0.0.0-experimental.124

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 (120) hide show
  1. package/dist/bin/rango.js +7 -2
  2. package/dist/vite/index.js +47 -6
  3. package/package.json +61 -21
  4. package/skills/cache-guide/SKILL.md +8 -6
  5. package/skills/caching/SKILL.md +148 -1
  6. package/skills/hooks/SKILL.md +38 -27
  7. package/skills/host-router/SKILL.md +16 -2
  8. package/skills/intercept/SKILL.md +4 -2
  9. package/skills/layout/SKILL.md +11 -6
  10. package/skills/loader/SKILL.md +6 -2
  11. package/skills/middleware/SKILL.md +4 -2
  12. package/skills/migrate-nextjs/SKILL.md +38 -16
  13. package/skills/parallel/SKILL.md +9 -4
  14. package/skills/rango/SKILL.md +27 -15
  15. package/skills/route/SKILL.md +4 -2
  16. package/skills/testing/SKILL.md +129 -0
  17. package/skills/testing/bindings.md +89 -0
  18. package/skills/testing/cache-prerender.md +98 -0
  19. package/skills/testing/client-components.md +122 -0
  20. package/skills/testing/e2e-parity.md +125 -0
  21. package/skills/testing/flight.md +89 -0
  22. package/skills/testing/handles.md +129 -0
  23. package/skills/testing/loader.md +128 -0
  24. package/skills/testing/middleware.md +99 -0
  25. package/skills/testing/render-handler.md +118 -0
  26. package/skills/testing/response-routes.md +95 -0
  27. package/skills/testing/reverse-and-types.md +84 -0
  28. package/skills/testing/server-actions.md +107 -0
  29. package/skills/testing/server-tree.md +128 -0
  30. package/skills/testing/setup.md +120 -0
  31. package/skills/use-cache/SKILL.md +9 -7
  32. package/src/browser/action-fence.ts +37 -0
  33. package/src/browser/cookie-name.ts +140 -0
  34. package/src/browser/invalidate-client-cache.ts +52 -0
  35. package/src/browser/navigation-bridge.ts +14 -1
  36. package/src/browser/navigation-client.ts +14 -1
  37. package/src/browser/navigation-store-handle.ts +39 -0
  38. package/src/browser/navigation-store.ts +26 -12
  39. package/src/browser/prefetch/fetch.ts +7 -0
  40. package/src/browser/rango-state.ts +176 -97
  41. package/src/browser/react/index.ts +0 -6
  42. package/src/browser/rsc-router.tsx +12 -4
  43. package/src/browser/server-action-bridge.ts +77 -15
  44. package/src/browser/types.ts +7 -1
  45. package/src/cache/cache-error.ts +104 -0
  46. package/src/cache/cache-policy.ts +95 -1
  47. package/src/cache/cache-runtime.ts +79 -13
  48. package/src/cache/cache-scope.ts +55 -4
  49. package/src/cache/cache-tag.ts +135 -0
  50. package/src/cache/cf/cf-cache-store.ts +2080 -224
  51. package/src/cache/cf/index.ts +15 -1
  52. package/src/cache/document-cache.ts +74 -7
  53. package/src/cache/index.ts +17 -0
  54. package/src/cache/memory-segment-store.ts +164 -14
  55. package/src/cache/tag-invalidation.ts +230 -0
  56. package/src/cache/types.ts +27 -0
  57. package/src/client.rsc.tsx +1 -1
  58. package/src/client.tsx +0 -6
  59. package/src/component-utils.ts +19 -0
  60. package/src/handle.ts +29 -9
  61. package/src/host/testing.ts +43 -14
  62. package/src/index.rsc.ts +29 -1
  63. package/src/index.ts +43 -1
  64. package/src/loader.rsc.ts +24 -3
  65. package/src/loader.ts +16 -2
  66. package/src/prerender.ts +24 -3
  67. package/src/router/basename.ts +14 -0
  68. package/src/router/match-handlers.ts +62 -20
  69. package/src/router/prerender-match.ts +6 -0
  70. package/src/router/router-interfaces.ts +7 -0
  71. package/src/router/router-options.ts +30 -0
  72. package/src/router/segment-resolution/loader-cache.ts +8 -17
  73. package/src/router/state-cookie-name.ts +33 -0
  74. package/src/router/telemetry.ts +99 -0
  75. package/src/router.ts +36 -7
  76. package/src/rsc/handler.ts +13 -1
  77. package/src/rsc/helpers.ts +19 -0
  78. package/src/rsc/progressive-enhancement.ts +2 -0
  79. package/src/rsc/response-route-handler.ts +8 -1
  80. package/src/rsc/rsc-rendering.ts +2 -0
  81. package/src/rsc/types.ts +2 -0
  82. package/src/runtime-env.ts +18 -0
  83. package/src/server/cookie-store.ts +52 -1
  84. package/src/server/request-context.ts +105 -2
  85. package/src/static-handler.ts +25 -3
  86. package/src/testing/cache-status.ts +166 -0
  87. package/src/testing/collect-handle.ts +63 -0
  88. package/src/testing/dispatch.ts +581 -0
  89. package/src/testing/dom.entry.ts +22 -0
  90. package/src/testing/e2e/fixture.ts +188 -0
  91. package/src/testing/e2e/index.ts +149 -0
  92. package/src/testing/e2e/matchers.ts +51 -0
  93. package/src/testing/e2e/page-helpers.ts +272 -0
  94. package/src/testing/e2e/parity.ts +387 -0
  95. package/src/testing/e2e/server.ts +195 -0
  96. package/src/testing/flight-matchers.ts +110 -0
  97. package/src/testing/flight-normalize.ts +38 -0
  98. package/src/testing/flight-runtime.d.ts +57 -0
  99. package/src/testing/flight-tree.ts +682 -0
  100. package/src/testing/flight.entry.ts +52 -0
  101. package/src/testing/flight.ts +234 -0
  102. package/src/testing/generated-routes.ts +223 -0
  103. package/src/testing/index.ts +119 -0
  104. package/src/testing/internal/context.ts +390 -0
  105. package/src/testing/internal/flight-client-globals.ts +30 -0
  106. package/src/testing/internal/seed-vars.ts +80 -0
  107. package/src/testing/render-handler.ts +360 -0
  108. package/src/testing/render-route.tsx +594 -0
  109. package/src/testing/run-loader.ts +474 -0
  110. package/src/testing/run-middleware.ts +231 -0
  111. package/src/testing/vitest-stubs/cloudflare-email.ts +9 -0
  112. package/src/testing/vitest-stubs/cloudflare-workers.ts +21 -0
  113. package/src/testing/vitest-stubs/plugin-rsc.ts +16 -0
  114. package/src/testing/vitest-stubs/version.ts +5 -0
  115. package/src/testing/vitest.ts +305 -0
  116. package/src/types/cache-types.ts +13 -4
  117. package/src/types/error-types.ts +5 -1
  118. package/src/types/global-namespace.ts +11 -1
  119. package/src/types/handler-context.ts +16 -5
  120. package/src/browser/react/use-client-cache.ts +0 -58
@@ -202,8 +202,10 @@ layout(<ShopLayout />, () => [
202
202
  ])
203
203
 
204
204
  // Or revalidate based on conditions
205
+ import * as CartActions from "./actions/cart";
206
+
205
207
  layout(<CartLayout />, () => [
206
- revalidate(({ actionId }) => actionId?.includes("Cart") || undefined),
208
+ revalidate((ctx) => ctx.isAction(CartActions) || undefined),
207
209
 
208
210
  path("/cart", CartPage, { name: "cart" }),
209
211
  ])
@@ -221,8 +223,9 @@ them on both producer and consumer segments:
221
223
 
222
224
  ```typescript
223
225
  // revalidation-contracts.ts
224
- export const revalidateCartData = ({ actionId }) =>
225
- actionId?.includes("src/actions/cart.ts#addToCart") || undefined;
226
+ import { addToCart } from "./actions/cart";
227
+
228
+ export const revalidateCartData = (ctx) => ctx.isAction(addToCart) || undefined;
226
229
  ```
227
230
 
228
231
  ```typescript
@@ -242,9 +245,10 @@ You can also package them as importable handoff helpers:
242
245
  ```typescript
243
246
  // revalidation-contracts.ts
244
247
  import { revalidate } from "@rangojs/router";
248
+ import * as AuthActions from "./actions/auth";
245
249
 
246
- export const revalidateAuthData = ({ actionId }) =>
247
- actionId?.includes("src/actions/auth.ts#") || undefined;
250
+ export const revalidateAuthData = (ctx) =>
251
+ ctx.isAction(AuthActions) || undefined;
248
252
  export const revalidateAuth = () => [revalidate(revalidateAuthData)];
249
253
  ```
250
254
 
@@ -262,6 +266,7 @@ layout(<ShellLayout />, () => [
262
266
  ```typescript
263
267
  import { urls } from "@rangojs/router";
264
268
  import { Outlet, ParallelOutlet } from "@rangojs/router/client";
269
+ import * as CartActions from "./actions/cart";
265
270
 
266
271
  function ShopLayout() {
267
272
  return (
@@ -291,7 +296,7 @@ export const shopPatterns = urls(({ path, layout, parallel, loader, revalidate }
291
296
  }, () => [
292
297
  // Layout loaders
293
298
  loader(CartLoader, () => [
294
- revalidate(({ actionId }) => actionId?.includes("Cart") || undefined),
299
+ revalidate((ctx) => ctx.isAction(CartActions) || undefined),
295
300
  ]),
296
301
 
297
302
  // Parallel routes
@@ -249,6 +249,8 @@ export const OrderLoader = createLoader(async (ctx) => {
249
249
  Add caching or revalidation to specific loaders:
250
250
 
251
251
  ```typescript
252
+ import * as CartActions from "./actions/cart";
253
+
252
254
  path("/product/:slug", ProductPage, { name: "product" }, () => [
253
255
  // Cached loader
254
256
  loader(ProductLoader, () => [cache({ ttl: 300 })]),
@@ -261,7 +263,7 @@ path("/product/:slug", ProductPage, { name: "product" }, () => [
261
263
  // Loader that revalidates after cart actions (defer otherwise — keeps the
262
264
  // permissive loader defaults for navigation and other actions intact)
263
265
  loader(CartLoader, () => [
264
- revalidate(({ actionId }) => actionId?.includes("Cart") || undefined),
266
+ revalidate((ctx) => ctx.isAction(CartActions) || undefined),
265
267
  ]),
266
268
  ]);
267
269
  ```
@@ -781,10 +783,12 @@ export const CartLoader = createLoader(async (ctx) => {
781
783
  });
782
784
 
783
785
  // urls.tsx — register loaders in the DSL
786
+ import * as CartActions from "./actions/cart";
787
+
784
788
  export const urlpatterns = urls(({ path, layout, loader, loading, cache, revalidate }) => [
785
789
  layout(<ShopLayout />, () => [
786
790
  loader(CartLoader, () => [
787
- revalidate(({ actionId }) => actionId?.includes("Cart") || undefined),
791
+ revalidate((ctx) => ctx.isAction(CartActions) || undefined),
788
792
  ]),
789
793
 
790
794
  path("/shop/product/:slug", ProductPage, { name: "product" }, () => [
@@ -67,8 +67,10 @@ For shared segment data, use named revalidation contracts on both the producer
67
67
  and consumer segments, even when middleware is present in the chain.
68
68
 
69
69
  ```typescript
70
- export const revalidateCartData = ({ actionId }) =>
71
- actionId?.includes("src/actions/cart.ts#") || undefined;
70
+ import * as CartActions from "./actions/cart";
71
+
72
+ export const revalidateCartData = (ctx) =>
73
+ ctx.isAction(CartActions) || undefined;
72
74
 
73
75
  layout(CartLayout, () => [
74
76
  middleware(cartRenderMiddleware),
@@ -288,21 +288,40 @@ export const Product = Passthrough(ProductDef, async (ctx) => {
288
288
  Use `Passthrough()` whenever the Next.js route has `dynamicParams: true` (the
289
289
  default) or serves an open-ended param space. See `/prerender` for full API.
290
290
 
291
- ### Revalidation: different model
291
+ ### Revalidation: two distinct axes
292
292
 
293
- Next.js uses path/tag-based cache invalidation (`revalidatePath`, `revalidateTag`)
294
- to bust cached responses. Rango does not currently have a direct equivalent.
293
+ Next.js conflates two things under "revalidation." Rango separates them — and
294
+ tag-based cache invalidation now maps directly.
295
295
 
296
- In Rango, separate these two concepts:
296
+ **1. Cache invalidation (bust cached values) — direct equivalent.** Tag entries
297
+ with `cache({ tags })` or, inside a `"use cache"` function, runtime
298
+ `cacheTag(...tags)`. Then invalidate by tag:
297
299
 
298
- **Partial rendering revalidation** — `revalidate()` controls which segments
299
- (layouts, paths, loaders, parallels) should re-run during partial action
300
- re-rendering. This is about the segment tree, not cache invalidation:
300
+ ```typescript
301
+ // Next.js Rango
302
+ // revalidateTag("products") → await updateTag("products") // in a server action: awaitable,
303
+ // // read-your-own-writes (next render is fresh)
304
+ // or revalidateTag("products") // in a route handler / webhook:
305
+ // // background, non-blocking (hard-purge)
306
+ ```
307
+
308
+ `updateTag` is awaitable and immediate; `revalidateTag` is fire-and-forget. Both
309
+ hard-purge (the next read re-renders fresh); the only difference is awaitability —
310
+ despite the Next.js name, `revalidateTag` here is NOT stale-while-revalidate.
311
+ Built-in stores (`MemorySegmentCacheStore`, `CFCacheStore`) index by tag. Next's
312
+ `revalidatePath` has no path-based equivalent — tag the relevant entries instead.
313
+
314
+ **2. Partial-render selection (which segments re-run after an action).** This is
315
+ NOT cache invalidation — it is `revalidate()`, controlling which segments
316
+ (layouts, paths, loaders, parallels) recompute during partial action
317
+ re-rendering:
301
318
 
302
319
  ```typescript
320
+ import { updateBlog } from "./actions/blog";
321
+
303
322
  // Re-run this layout when a blog action fires
304
323
  layout(BlogLayout, () => [
305
- revalidate(({ actionId }) => actionId?.includes("updateBlog") || undefined),
324
+ revalidate((ctx) => ctx.isAction(updateBlog) || undefined),
306
325
  path("/blog/:slug", BlogPost, { name: "blogPost" }),
307
326
  ]);
308
327
 
@@ -323,15 +342,18 @@ cache({ ttl: 60, swr: 300 }, () => [
323
342
  ]);
324
343
  ```
325
344
 
326
- The key shift is:
345
+ The two axes compose: `updateTag()` / `revalidateTag()` bust cached values;
346
+ `revalidate()` selects which segments re-render and stream to the client after an
347
+ action.
327
348
 
328
- - Next.js asks "which cached path or tag should I invalidate?"
329
- - Rango asks "which segments should re-run after this action?"
349
+ When migrating:
330
350
 
331
- When migrating `revalidatePath()` / `revalidateTag()` usage, the Rango version
332
- usually is not a 1:1 API replacement. Instead, decide which layouts, routes,
333
- loaders, or parallels should recompute after an action and declare
334
- `revalidate()` at those segment boundaries.
351
+ - `revalidateTag(tag)` `await updateTag(tag)` (in a server action) or
352
+ `revalidateTag(tag)` (in a route handler / webhook). Effectively 1:1.
353
+ - `revalidatePath(path)` no path-based equivalent; tag the entries on that
354
+ route (`cache({ tags })` / `cacheTag(...)`) and invalidate by tag.
355
+ - To also force specific segments to re-render after the action (independent of
356
+ cache busting), attach a `revalidate()` rule at those segment boundaries.
335
357
 
336
358
  ## 4. Middleware
337
359
 
@@ -463,7 +485,7 @@ Server actions work the same way — `"use server"` directive, `useActionState`,
463
485
 
464
486
  Key difference: in Rango, route middleware does NOT wrap action execution. Actions only see global middleware context. Use `getRequestContext()` in actions to access `ctx.set()`/`ctx.get()`.
465
487
 
466
- Next.js's `revalidatePath()` / `revalidateTag()` have no direct equivalent — Rango partially re-renders matched route segments (path/layout/parallel/intercept) and re-resolves their loaders, and you scope re-runs by attaching a `revalidate(({ actionId }) => ...)` rule to any segment or loader registration. See `/server-actions` for the full pattern (validation, error handling, file uploads) and `/loader` for revalidation rule semantics.
488
+ Next.js's `revalidateTag()` maps directly: tag entries via `cache({ tags })` / `cacheTag(...)`, then invalidate. **In a server action use `await updateTag(tag)`** — it is read-your-own-writes, so the action's own re-render sees fresh data; `revalidateTag(tag)` is a background (non-blocking) hard-purge and is NOT read-your-own-writes, so reserve it for route handlers / webhooks (calling it from an action can leave that action's re-render stale). `revalidatePath()` has no path-based equivalent — tag the route's entries instead. Separately, to force specific matched segments (path/layout/parallel/intercept) and their loaders to re-render after an action, attach a `revalidate(({ actionId }) => ...)` rule to that segment or loader registration. See `/server-actions` for the full pattern (validation, error handling, file uploads), `/caching` for tag invalidation, and `/loader` for revalidation rule semantics.
467
489
 
468
490
  ## 8. Metadata / Head
469
491
 
@@ -330,6 +330,8 @@ parallel({
330
330
  Control when parallel routes revalidate:
331
331
 
332
332
  ```typescript
333
+ import * as CartActions from "./actions/cart";
334
+
333
335
  parallel(
334
336
  {
335
337
  "@cart": () => <CartSummary />,
@@ -337,7 +339,7 @@ parallel(
337
339
  () => [
338
340
  loader(CartLoader),
339
341
  // Revalidate when cart actions occur
340
- revalidate(({ actionId }) => actionId?.includes("Cart") || undefined),
342
+ revalidate((ctx) => ctx.isAction(CartActions) || undefined),
341
343
  ]
342
344
  )
343
345
  ```
@@ -360,8 +362,10 @@ the parallel consumer:
360
362
 
361
363
  ```typescript
362
364
  // revalidation-contracts.ts
363
- export const revalidateCartData = ({ actionId }) =>
364
- actionId?.includes("src/actions/cart.ts#") || undefined;
365
+ import * as CartActions from "./actions/cart";
366
+
367
+ export const revalidateCartData = (ctx) =>
368
+ ctx.isAction(CartActions) || undefined;
365
369
 
366
370
  layout(CartLayout, () => [
367
371
  revalidate(revalidateCartData), // producer reruns
@@ -429,6 +433,7 @@ function MyLayout() {
429
433
  ```typescript
430
434
  import { urls } from "@rangojs/router";
431
435
  import { Outlet, ParallelOutlet } from "@rangojs/router/client";
436
+ import * as CartActions from "./actions/cart";
432
437
 
433
438
  function ShopLayout() {
434
439
  return (
@@ -479,7 +484,7 @@ export const shopPatterns = urls(({
479
484
  () => [
480
485
  loader(CartLoader),
481
486
  loading(<CartSkeleton />),
482
- revalidate(({ actionId }) => actionId?.includes("Cart") || undefined),
487
+ revalidate((ctx) => ctx.isAction(CartActions) || undefined),
483
488
  ]
484
489
  ),
485
490
 
@@ -28,10 +28,10 @@ with the shape, then pick a primitive.
28
28
  cache hit streams UI instantly while loaders resolve fresh alongside). Opt into
29
29
  caching explicitly. See `/loader` → "Parallel and streaming".
30
30
  - **One identity, one store** — loaders, handles, cached fns, and actions are all
31
- `path#export`; all caches share one store. Entries expire by TTL/SWR; cache
32
- entries accept an optional `tags` field, but built-in stores do not yet index
33
- or invalidate by tag, so tag-based invalidation (`revalidateTag`) is a
34
- forward-looking API requiring a custom store.
31
+ `path#export`; all caches share one store. Entries expire by TTL/SWR, and are
32
+ tagged via `cache({ tags })` or runtime `cacheTag(...tags)`; built-in stores
33
+ index by tag and invalidate via `updateTag(...tags)` (awaitable, read-your-own-writes)
34
+ or `revalidateTag(...tags)` (background, non-blocking).
35
35
  - **Type-safe end to end** — route names, params, search schemas, loader return
36
36
  types, context vars, and `href` / `reverse` are checked at compile time
37
37
  (`/typesafety`).
@@ -85,10 +85,10 @@ To decide where something can live: **does it define a URL? structure, stays in
85
85
  (`set`/`header`/`setTheme`/`onResponse`/`setLocationState`) throw; `ctx.use(Handle)`
86
86
  is captured on miss and replayed on hit. (The non-cacheable read guard is a
87
87
  separate `cache()`-boundary check — see the correctness bullet below.)
88
- - One identity `path#export` (`functionId`/`$$id`/`actionId`); one store. The
89
- cross-cutting freshness mechanism today is TTL/SWR expiry; cache entries accept
90
- an optional `tags` field, but built-in stores do not yet index or invalidate by
91
- tag, so `revalidateTag` is forward-looking (requires a custom store).
88
+ - One identity `path#export` (`functionId`/`$$id`/`actionId`); one store. Freshness
89
+ is TTL/SWR expiry plus tag-based invalidation: tag via `cache({ tags })` /
90
+ `cacheTag(...tags)`, then `updateTag(...tags)` (awaitable) or `revalidateTag(...tags)`
91
+ (background). Built-in stores index by tag.
92
92
  - `useLoader` / `useHandle` / `useFetchLoader` are client-only.
93
93
  - Caches are correctness-first: persistent store keys are version-segmented (no
94
94
  cross-deploy drift), the forward/back cache is mutation-aware, and
@@ -111,13 +111,13 @@ To decide where something can live: **does it define a URL? structure, stays in
111
111
  Same words, different jobs — this is the most common source of the
112
112
  `revalidate()`-is-caching misread.
113
113
 
114
- | You may know | Maps to Rango axis | Watch out |
115
- | ------------------------------------------ | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
116
- | Next.js `export const revalidate = N` | **Axis 1** (cache) | Same word, opposite meaning. Next's `revalidate` is time-based cache expiry; Rango's `revalidate()` is **axis 2**. Use `cache({ ttl })` for the Next behavior. |
117
- | Next.js `revalidatePath` / `revalidateTag` | **Axis 1** (cache) | Cache busting. No shipped equivalent: entries accept `tags`, but built-in stores don't yet index/invalidate by tag, so `revalidateTag` is forward-looking (custom store); today entries expire by TTL/SWR. No `revalidatePath`. |
118
- | React Router / Remix `shouldRevalidate` | **Axis 2** | This is the correct mental model for Rango's `revalidate()`. |
119
- | HTTP `Cache-Control` / ISR | **Axis 1** | Edge/document layer — see `/document-cache`. Separate from both `cache()` and `revalidate()`. |
120
- | Remix/RR `loader` | live data | Like Rango loaders, fresh per request — but Rango loaders run in parallel and stream (latency overlaps first paint), and can opt into caching on demand. |
114
+ | You may know | Maps to Rango axis | Watch out |
115
+ | --------------------------------------- | ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
116
+ | Next.js `export const revalidate = N` | **Axis 1** (cache) | Same word, opposite meaning. Next's `revalidate` is time-based cache expiry; Rango's `revalidate()` is **axis 2**. Use `cache({ ttl })` for the Next behavior. |
117
+ | Next.js `revalidateTag` / `updateTag` | **Axis 1** (cache) | Cache busting by tag. Tag via `cache({ tags })` / `cacheTag(...tags)`; invalidate with `updateTag(...tags)` (awaitable, read-your-own-writes) or `revalidateTag(...tags)` (background, non-blocking). Built-in stores index by tag. No `revalidatePath` (path-based busting); use tags. |
118
+ | React Router / Remix `shouldRevalidate` | **Axis 2** | This is the correct mental model for Rango's `revalidate()`. |
119
+ | HTTP `Cache-Control` / ISR | **Axis 1** | Edge/document layer — see `/document-cache`. Separate from both `cache()` and `revalidate()`. |
120
+ | Remix/RR `loader` | live data | Like Rango loaders, fresh per request — but Rango loaders run in parallel and stream (latency overlaps first paint), and can opt into caching on demand. |
121
121
 
122
122
  See `/cache-guide` for the axis-1 decision guide, `/loader` and `/route` for
123
123
  `revalidate()` (axis 2), and `/document-cache` for the edge layer.
@@ -154,6 +154,12 @@ returned, for outcome-conditional revalidation. The arg also exposes `actionId`
154
154
  (raw `path#export`), `actionUrl`, `formData`, `method`, and `stale` (cross-tab
155
155
  `_rsc_stale` signal). All are `undefined` on plain navigation (no action).
156
156
 
157
+ Two idioms, picked by what an _unrelated_ action should do. `ctx.isAction()`
158
+ returns a raw boolean, so combine it with `|| undefined` to **defer** ("mine,
159
+ else let the default decide": `ctx.isAction(CartActions) || undefined`) or leave
160
+ it bare to **suppress** ("mine only": `ctx.isAction(CartActions)`). Prefer the
161
+ defer form unless a sibling segment must own the unrelated-action decision.
162
+
157
163
  ```ts
158
164
  // re-render only when checkout actually succeeded; defer otherwise
159
165
  revalidate((ctx) => (ctx.isAction(checkout) && ctx.actionResult?.ok) || undefined),
@@ -232,6 +238,12 @@ Grouped by concern — read when you need to…
232
238
  | `/bundle-analysis` | Audit your app's production bundle for server leaks and oversized chunks |
233
239
  | `/debug-manifest` | Inspect route manifest structure |
234
240
 
241
+ **Testing**:
242
+
243
+ | Skill | Description |
244
+ | ---------- | ------------------------------------------------------------------------------------------------------------------------------- |
245
+ | `/testing` | Unit (loaders/middleware/reverse/components), integration (dispatch/Flight), and e2e (dev+prod parity, progressive enhancement) |
246
+
235
247
  **Setup, types & migration**:
236
248
 
237
249
  | Skill | Description |
@@ -246,10 +246,12 @@ the producer route and the consumer child segments.
246
246
 
247
247
  ```typescript
248
248
  // revalidation-contracts.ts
249
+ import * as CheckoutActions from "./actions/checkout";
250
+
249
251
  // Defer (|| undefined), not ?? false: a hard `false` short-circuits the chain,
250
252
  // so when the same segment composes multiple contracts the later ones never run.
251
- export const revalidateCheckoutData = ({ actionId }) =>
252
- actionId?.includes("src/actions/checkout.ts#") || undefined;
253
+ export const revalidateCheckoutData = (ctx) =>
254
+ ctx.isAction(CheckoutActions) || undefined;
253
255
 
254
256
  path("/checkout", CheckoutPage, { name: "checkout" }, () => [
255
257
  revalidate(revalidateCheckoutData), // producer (route handler) reruns
@@ -0,0 +1,129 @@
1
+ ---
2
+ name: testing
3
+ description: Test @rangojs/router apps — unit (loaders/middleware/reverse/components), integration (dispatch/Flight), and e2e (dev+prod parity, progressive enhancement)
4
+ argument-hint: [layer]
5
+ ---
6
+
7
+ # Testing @rangojs/router apps
8
+
9
+ Rango ships six consumer-facing testing entries, one per test runtime/dependency:
10
+ `@rangojs/router/testing` (unit + integration, under a Vite-driven Vitest
11
+ project), `@rangojs/router/testing/vitest` (the `rangoTestConfig`/`rangoTestAliases`
12
+ setup preset), `@rangojs/router/testing/dom` (`renderRoute`, needs RTL + a DOM
13
+ env), `@rangojs/router/testing/e2e` (the Playwright harness),
14
+ `@rangojs/router/testing/flight` (real Flight, react-server condition only), and
15
+ `@rangojs/router/testing/flight-matchers` (the Flight matchers).
16
+
17
+ The hard problem in an RSC app is that the layer you reach for is dictated by
18
+ **what the behavior touches** — a pure predicate is a one-line vitest test; a real
19
+ async Server Component cannot be a plain node test at all. Pick the layer
20
+ **first**, then the primitive. Reaching one layer too high (e2e for a reverse
21
+ function) is slow; one too low (a node test for Flight) fails to compile or
22
+ silently asserts nothing.
23
+
24
+ This page is the router. Each primitive's full API (options, the seeded context
25
+ your code receives, the return shape), a minimal recipe, and its caveats live in a
26
+ dedicated sub-file linked from the decision tree below. Read the one for your case.
27
+
28
+ > **Setup is the first wall.** The vitest projects, the `rangoTestConfig` vs
29
+ > `rangoTestAliases` choice (Node >= 23), and the react-server `@rangojs/router ->
30
+ index.rsc.ts` alias are all in [`./setup.md`](./setup.md). Read it before writing
31
+ > `vitest.config.ts`. Platform bindings (`env.DB`/DO/R2) are your own double —
32
+ > [`./bindings.md`](./bindings.md).
33
+
34
+ For the long-form prose guide (setup walkthrough + migration), see
35
+ [`docs/testing.md`](https://github.com/ivogt/vite-rsc/blob/main/packages/rangojs-router/docs/testing.md)
36
+ (the `docs/` directory is not shipped in the published package, so this is an
37
+ absolute link).
38
+
39
+ ## When to use
40
+
41
+ Use this skill when adding or changing tests for a Rango app: a loader,
42
+ middleware, a server action, a route map, a client component, a response route,
43
+ cache/SWR behavior, prerender, or a navigation/PE flow.
44
+
45
+ Two non-negotiable mandates (from the repo's `CLAUDE.md`, and they apply to
46
+ consumer apps too):
47
+
48
+ - **Every e2e covers BOTH dev and production.** A dev-only e2e is not acceptable.
49
+ Use `parityDescribe` — it generates the dev and production describes from one
50
+ body, so you cannot forget the prod half. See [`./e2e-parity.md`](./e2e-parity.md).
51
+ - **Progressive-enhancement parity** is a first-class assertion. A form-driven
52
+ flow must produce the same observable result with JS on and JS off. Use
53
+ `expectParity`.
54
+
55
+ ## The read-first shape
56
+
57
+ Four import roots, each matched to the dependency/runtime that can load it — this
58
+ split is forced by hard walls, not preference:
59
+
60
+ - `@rangojs/router/testing` — unit + integration primitives. Run these under a
61
+ **Vite-driven Vitest** project with the rango Vite plugin active (the router
62
+ internals import the `@rangojs/router:version` virtual; without the plugin, the
63
+ preset stubs it). References neither React, RTL, Playwright, nor the RSC runtime.
64
+ - `@rangojs/router/testing/dom` — `renderRoute` (the RTL component stub). Kept
65
+ separate so the unit barrel stays free of React/RTL; it lazy-loads
66
+ `@testing-library/react` and needs a DOM env (happy-dom/jsdom).
67
+ - `@rangojs/router/testing/e2e` — the Playwright harness. Kept separate so it
68
+ loads in a plain (non-Vite) Playwright runner; the helpers take your
69
+ `test`/`expect`, so this entry never imports `@playwright/test` at runtime.
70
+ - `@rangojs/router/testing/flight` — real Flight rendering. Its serializer loads
71
+ only under the `react-server` node condition; pulling it elsewhere throws.
72
+
73
+ The single rule that drives everything:
74
+
75
+ > **If the behavior needs a real Flight render, it cannot be a plain vitest node
76
+ > test.** It is either `renderToFlightString`/`renderServerTree`/`renderHandler`
77
+ > (under the react-server vitest project) or an e2e test. There is no middle
78
+ > ground in node.
79
+
80
+ ## Decision tree: behavior -> layer -> primitive
81
+
82
+ Each primitive links to its sub-file (API + recipe + caveats).
83
+
84
+ | The behavior is… | Layer | Primitive | Import root |
85
+ | --------------------------------------------------------------------------------------------------------- | ------------ | ------------------------------------------------------------------------------ | -------------------------------- |
86
+ | a pure function / `reverse` / `href` / a predicate (`revalidate`, `isAction`) | unit + types | [`reverse`/`@ts-expect-error`](./reverse-and-types.md) | `@rangojs/router/testing` |
87
+ | one loader's data logic | unit (node) | [`runLoader`](./loader.md) | `@rangojs/router/testing` |
88
+ | a loader's cookie / header / redirect output (auth-loader pattern) | unit (node) | [`runLoaderResult`](./loader.md) | `@rangojs/router/testing` |
89
+ | one middleware's ordering / short-circuit / cookie+header merge | unit (node) | [`runMiddleware`](./middleware.md) | `@rangojs/router/testing` |
90
+ | a `"use server"` action's cookie / header / flash output (even on `throw redirect()`) | unit (node) | [`runInRequestContext`](./server-actions.md) | `@rangojs/router/testing` |
91
+ | a handle's `collect`/accumulator, or a seeded handle read | unit | [`collectHandle` / seeded `handles`](./handles.md) | `@rangojs/router/testing[/dom]` |
92
+ | a CLIENT component reading router context (`useParams`/`useReverse`/`Outlet`/`useNavigation`/`useLoader`) | unit (DOM) | [`renderRoute`](./client-components.md) | `@rangojs/router/testing/dom` |
93
+ | a redirect / status / headers / cookies / **response route** (json/text/html/xml/md), no Flight | integration | [`dispatch`](./response-routes.md) | `@rangojs/router/testing` |
94
+ | a real async **Server Component** / Flight serialization shape | RSC unit | [`renderToFlightString` + `toMatchFlight`](./flight.md) | `@rangojs/router/testing/flight` |
95
+ | a client island's **typed props** / the **server-rendered** host content | RSC unit | [`renderServerTree` + `findClientBoundaries`/`findElements`](./server-tree.md) | `@rangojs/router/testing/flight` |
96
+ | a real route **handler** `(ctx) => rsc` (params/loaders/vars -> rendered RSC + effects) | RSC unit | [`renderHandler`](./render-handler.md) | `@rangojs/router/testing/flight` |
97
+ | navigation, hydration, PE parity, view transitions, real SSR | e2e | [`createRangoE2E` -> `parityDescribe`/`expectParity`](./e2e-parity.md) | `@rangojs/router/testing/e2e` |
98
+ | cache hit/miss/stale, prerender (= a cache hit by design) | e2e + signal | [`assertCacheStatus` / telemetry sink](./cache-prerender.md) | `@rangojs/router/testing[/e2e]` |
99
+ | generated route map drift vs runtime | unit (node) | [`assertGeneratedRoutesMatch`](./reverse-and-types.md) | `@rangojs/router/testing` |
100
+ | a platform binding (`env.DB` / Durable Object / `env.R2`) | unit/integr. | [your own double via `env`](./bindings.md) | (any primitive's `env` option) |
101
+
102
+ Cross-references to the DSL skills: `/loader`, `/middleware`, `/server-actions`,
103
+ `/handler-use`, `/hooks`, `/response-routes`, `/route`, `/caching`, `/prerender`,
104
+ `/typesafety`.
105
+
106
+ ## Sub-files
107
+
108
+ - Cross-cutting: [`setup.md`](./setup.md), [`bindings.md`](./bindings.md)
109
+ - Unit (node): [`loader.md`](./loader.md), [`middleware.md`](./middleware.md),
110
+ [`server-actions.md`](./server-actions.md), [`handles.md`](./handles.md),
111
+ [`reverse-and-types.md`](./reverse-and-types.md)
112
+ - Unit (DOM): [`client-components.md`](./client-components.md)
113
+ - RSC unit: [`flight.md`](./flight.md), [`server-tree.md`](./server-tree.md),
114
+ [`render-handler.md`](./render-handler.md)
115
+ - Integration: [`response-routes.md`](./response-routes.md)
116
+ - E2E: [`e2e-parity.md`](./e2e-parity.md), [`cache-prerender.md`](./cache-prerender.md)
117
+
118
+ ## Pre-push checklist (mirror CLAUDE.md)
119
+
120
+ Before pushing, run all of these and fix any failure:
121
+
122
+ 1. `pnpm run typecheck` (or `pnpm exec tsc --noEmit`)
123
+ 2. `pnpm run test:unit` (node + DOM vitest)
124
+ 3. `pnpm run test:unit:rsc` (the react-server Flight project)
125
+ 4. `pnpm run lint`
126
+ 5. `pnpm run format`
127
+
128
+ And: **every e2e has a production counterpart.** `parityDescribe` makes this
129
+ automatic — if you wrote a plain `test.describe` for a behavior, convert it.
@@ -0,0 +1,89 @@
1
+ # Testing platform bindings — your double is the seam
2
+
3
+ **Layer:** cross-cutting (unit/integration) · **Seam:** the `env` option every primitive takes
4
+
5
+ The node primitives test the router's seams; the moment your loader/middleware/action calls a **platform binding** (`env.DB`, a Durable Object stub, `env.R2`), you have crossed out of rango and into your app's I/O. The router machinery is real — what you seed is the binding double behind it, injected through `env`.
6
+
7
+ ## Where it plugs in
8
+
9
+ rango ships **no doubles** for platform bindings — they are app- and schema-specific. You build the double and inject it through the `env` option that every primitive already accepts:
10
+
11
+ - `runLoader(body, { env })`
12
+ - `runMiddleware(fn, { request, env })`
13
+ - `runInRequestContext(fn, { request, env })`
14
+ - `renderHandler(handler, { request, env })`
15
+ - `dispatch(router, { request, env })`
16
+ - `renderToFlightString(el, { env })`
17
+
18
+ Inside the run, `getRequestContext().env` (and anything that reads it — `cache()`, your loaders, your middleware) sees the object you passed.
19
+
20
+ ## Driver contract
21
+
22
+ The work here is matching the binding's **driver contract**, not its public API. A double that satisfies the public surface but not the driver's wire shape mounts green and proves nothing.
23
+
24
+ - **Per-method shapes.** `drizzle-orm/d1` serves SELECTs through `.raw()` and writes (INSERT/UPDATE/DELETE) through `.run()`. The two return different shapes and hit different code paths in the decoder. Model **both**.
25
+ - **`.raw()` (reads).** Must serve **positional row arrays in schema-column order**, with the driver-level encodings so the decoder round-trips `Date`/JSON. NOT `{ column: value }` objects.
26
+ - **`.run()` (writes).** Returns `{ success, meta }` — no rows — and bypasses the row responder entirely.
27
+
28
+ ## Recipe
29
+
30
+ ```ts
31
+ import { describe, it, expect } from "vitest";
32
+ import {
33
+ runLoader,
34
+ runMiddleware,
35
+ runInRequestContext,
36
+ } from "@rangojs/router/testing";
37
+ import { bundleLoaderBody } from "../app/loaders";
38
+ import { requireMembership } from "../app/middleware";
39
+ import { authorizeAction } from "../app/actions";
40
+
41
+ // A D1Database double satisfying drizzle-orm/d1's driver contract.
42
+ const fakeD1 = makeFakeD1({
43
+ // .raw() serves positional rows in schema-column order, driver-encoded.
44
+ raw: () => [[1, "acme", "2026-01-01T00:00:00.000Z"]],
45
+ // .run() returns { success, meta }, no rows.
46
+ run: () => ({ success: true, meta: { changes: 1 } }),
47
+ });
48
+
49
+ describe("bindings seam", () => {
50
+ it("loader reads through env.DB", async () => {
51
+ const result = await runLoader(bundleLoaderBody, { env: { DB: fakeD1 } });
52
+ expect(result).toMatchObject({ slug: "acme" });
53
+ });
54
+
55
+ it("middleware reads through env.DB", async () => {
56
+ const { nextCalled, response } = await runMiddleware(requireMembership, {
57
+ request: "/t/acme/edit",
58
+ env: { DB: fakeD1 },
59
+ });
60
+ expect(nextCalled).toBe(1); // membership passed, chain continued
61
+ expect(response.status).toBe(200);
62
+ });
63
+
64
+ it("action reads through env.DB", async () => {
65
+ const { result } = await runInRequestContext(
66
+ () => authorizeAction({ id: 1 }),
67
+ {
68
+ env: { DB: fakeD1 },
69
+ request: "/t/acme/edit",
70
+ },
71
+ );
72
+ expect(result).toBe(true);
73
+ });
74
+ });
75
+ ```
76
+
77
+ ## Caveats
78
+
79
+ - rango ships **no doubles** for platform bindings (`env.DB`, Durable Objects, `env.R2`) by design — they are app- and schema-specific. Inject your own double through the `env` option every primitive takes.
80
+ - This is usually the **single biggest effort** in a consumer unit suite, and the work is matching the **driver contract**, not the binding's public API.
81
+ - `drizzle-orm/d1`: a `D1Database` double must serve **positional row arrays in schema-column order** for drizzle's `.raw()` path (with driver-level encodings so the decoder round-trips `Date`/JSON), NOT `{ column: value }` objects — an object-shaped double returns silently-wrong or empty rows.
82
+ - The contract is **per-method**: SELECTs go through `.raw()` (positional rows); writes (INSERT/UPDATE/DELETE) go through `.run()`, which returns `{ success, meta }` (no rows) and bypasses the row responder entirely. Model **both** paths — a read-only `.raw()` double silently no-ops every write.
83
+ - Keep the double at the **binding boundary**; never mock a rango primitive to dodge building it.
84
+
85
+ ## See also
86
+
87
+ - (cross-cutting)
88
+ - Siblings: `./loader.md`, `./middleware.md`, `./server-actions.md`
89
+ - Long-form prose: [docs/testing.md](https://github.com/ivogt/vite-rsc/blob/main/packages/rangojs-router/docs/testing.md) — section "What these primitives deliberately don't cover (the platform-bindings paragraph)"