@rangojs/router 0.0.0-experimental.139 → 0.0.0-experimental.140
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.
- package/dist/bin/rango.js +27 -2
- package/dist/vite/index.js +147 -30
- package/package.json +1 -1
- package/skills/breadcrumbs/SKILL.md +1 -1
- package/skills/cache-guide/SKILL.md +1 -0
- package/skills/caching/SKILL.md +1 -1
- package/skills/migrate-nextjs/SKILL.md +15 -0
- package/skills/migrate-react-router/SKILL.md +15 -2
- package/skills/ppr/SKILL.md +426 -0
- package/skills/rango/SKILL.md +28 -25
- package/skills/route/SKILL.md +43 -0
- package/src/build/route-trie.ts +35 -7
- package/src/cache/cf/cf-cache-store.ts +155 -0
- package/src/cache/index.ts +6 -0
- package/src/cache/memory-segment-store.ts +57 -1
- package/src/cache/shell-cache.ts +386 -0
- package/src/cache/types.ts +58 -0
- package/src/cache/vercel/vercel-cache-store.ts +159 -5
- package/src/index.rsc.ts +5 -0
- package/src/index.ts +17 -0
- package/src/router/middleware.ts +14 -5
- package/src/router/parse-pattern.ts +115 -0
- package/src/router/pattern-matching.ts +53 -64
- package/src/router/segment-resolution/fresh.ts +12 -1
- package/src/router/segment-resolution/loader-cache.ts +14 -0
- package/src/router/segment-resolution/loader-mask.ts +44 -0
- package/src/router/substitute-pattern-params.ts +54 -35
- package/src/router/trie-matching.ts +19 -11
- package/src/router/url-params.ts +13 -0
- package/src/rsc/full-payload.ts +70 -0
- package/src/rsc/rsc-rendering.ts +105 -51
- package/src/rsc/shell-capture.ts +439 -0
- package/src/rsc/types.ts +26 -0
- package/src/server/cookie-store.ts +45 -0
- package/src/server/live.ts +130 -0
- package/src/server/request-context.ts +49 -0
- package/src/ssr/index.tsx +377 -180
- package/src/ssr/ssr-root.tsx +228 -0
- package/src/testing/render-route.tsx +7 -9
- package/src/types/route-config.ts +19 -7
- package/src/urls/type-extraction.ts +43 -18
- package/src/vite/discovery/discovery-errors.ts +61 -0
- package/src/vite/plugins/virtual-entries.ts +27 -2
- package/src/vite/router-discovery.ts +69 -15
- package/src/vite/utils/prerender-utils.ts +17 -4
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: ppr
|
|
3
|
+
description: PPR shell caching — serve a cached HTML shell instantly and resume live loader holes (createShellCacheMiddleware)
|
|
4
|
+
argument-hint: "[setup]"
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# PPR Shell Caching
|
|
8
|
+
|
|
9
|
+
Caches the rendered HTML **shell** of a route (React `prerender` prelude bytes
|
|
10
|
+
plus `postponed` state) and, on a later request, flushes those bytes before any
|
|
11
|
+
render work happens, then resumes fizz for just the live loader holes. The
|
|
12
|
+
browser sees one ordinary streamed document; loaders stay fresh on every
|
|
13
|
+
request. This is the second render axis — the default axis-1 path is untouched,
|
|
14
|
+
and every ineligible request falls open to it.
|
|
15
|
+
|
|
16
|
+
Compare `/document-cache`, which freezes the WHOLE response including loader
|
|
17
|
+
output. Shell caching is for pages that mix a stable shell with live data: the
|
|
18
|
+
shell is shared per host+URL, the holes are per request.
|
|
19
|
+
|
|
20
|
+
## Setup
|
|
21
|
+
|
|
22
|
+
The middleware needs a store that implements the shell family
|
|
23
|
+
(`getShell`/`putShell`): `MemorySegmentCacheStore` (dev/tests), `CFCacheStore`
|
|
24
|
+
(Cloudflare KV), or `VercelCacheStore` (runtime cache). It defaults to the
|
|
25
|
+
app-level store from `createRouter({ cache })`; a store without the family
|
|
26
|
+
disables the middleware (fail-open to axis 1).
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { createRouter } from "@rangojs/router";
|
|
30
|
+
import {
|
|
31
|
+
createShellCacheMiddleware,
|
|
32
|
+
CFCacheStore,
|
|
33
|
+
} from "@rangojs/router/cache";
|
|
34
|
+
import { urlpatterns } from "./urls";
|
|
35
|
+
|
|
36
|
+
const router = createRouter<AppBindings>({
|
|
37
|
+
document: Document,
|
|
38
|
+
urls: urlpatterns,
|
|
39
|
+
cache: (env, ctx) => ({
|
|
40
|
+
store: new CFCacheStore({ kv: env.CACHE_KV, ctx: ctx! }),
|
|
41
|
+
}),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Path-scoped: only the routes that fit the shell/hole shape below.
|
|
45
|
+
router.use(
|
|
46
|
+
"/products",
|
|
47
|
+
createShellCacheMiddleware({ ttlSeconds: 600, swrSeconds: 120 }),
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
export default router;
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Where PPR sits: the cache onion
|
|
54
|
+
|
|
55
|
+
Rango's caches layer like an onion — each ring stores a progressively more
|
|
56
|
+
"cooked" representation of the same page, and PPR is a new ring, not a
|
|
57
|
+
replacement for any existing one. From innermost (raw values) to outermost
|
|
58
|
+
(final bytes):
|
|
59
|
+
|
|
60
|
+
| Ring | Primitive | What is stored | What stays live on a hit |
|
|
61
|
+
| ----------------------- | ---------------------------------------- | -------------------------------------------------- | -------------------------------------- |
|
|
62
|
+
| 1. Function values | `"use cache"` | a function's return value | everything around the call |
|
|
63
|
+
| 2. Loader values | `loader(Fn, () => [cache({...})])` | one loader's result (opt-in; loaders default live) | all other loaders, handlers, rendering |
|
|
64
|
+
| 3. Segments (Flight) | `cache()` route / build-time `prerender` | serialized rendered segments + replayed handles | loaders, HTML render |
|
|
65
|
+
| 4. **HTML shell (PPR)** | `createShellCacheMiddleware` | rendered prelude bytes + React postponed state | loaders (the holes), hydration payload |
|
|
66
|
+
| 5. Whole response | `/document-cache` | final response bytes, headers included | nothing — all-or-nothing |
|
|
67
|
+
|
|
68
|
+
Each ring is derived from the ones inside it, and PPR makes that literal:
|
|
69
|
+
the captured shell is the fizz render of ring 3's replayed segments, which is
|
|
70
|
+
why shell/payload consistency holds by construction on `cache()` routes. The
|
|
71
|
+
rings compose in one request: a HIT serves ring 4's bytes instantly, the
|
|
72
|
+
resume pass replays ring 3's segments for the hydration payload, a hole's
|
|
73
|
+
loader may consult ring 2, and a component inside it may consult ring 1.
|
|
74
|
+
|
|
75
|
+
The onion also explains the boundary with ring 5: the document cache freezes
|
|
76
|
+
loaders too (no holes, coarser but simpler), which is why stacking both on
|
|
77
|
+
one route is pointless — pick the outermost ring whose "stays live" column
|
|
78
|
+
matches the route (see Pitfalls).
|
|
79
|
+
|
|
80
|
+
Invalidation crosses rings: `invalidateTags` reaches segment, shell, loader,
|
|
81
|
+
and item entries in the same store, and shell entries additionally
|
|
82
|
+
self-invalidate on `React.version` change. TTL/SWR are per-ring — an inner
|
|
83
|
+
ring's shorter TTL shows through a hole immediately (loaders are live), but
|
|
84
|
+
shell-embedded content refreshes only on the shell's own recapture.
|
|
85
|
+
|
|
86
|
+
## Creating holes: I want X → do Y
|
|
87
|
+
|
|
88
|
+
A shell-cached route is a stable shell with live **holes** punched through it.
|
|
89
|
+
Everything hinges on where the hole is, and the hole is always a route-level
|
|
90
|
+
`loading()` boundary. Start here:
|
|
91
|
+
|
|
92
|
+
| I want… | Do this |
|
|
93
|
+
| ------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
94
|
+
| Live per-request data in the page | a route `loader()` **+ route-level `loading()`** — the loader is the hole, `loading()` is the boundary the capture postpones at and the resume stitches into |
|
|
95
|
+
| A slow nested value streamed **inside** the hole | return `{ outer, pendingData: Promise }` from the loader; `use(pendingData)` under the consumer's OWN inner `<Suspense>` (see "Loader-carried promises") |
|
|
96
|
+
| A hole for data that is **already resolved** (in-memory, `Promise.resolve`, a cached read) | wrap it in **`live(() => …)`** under your own `<Suspense>` — masked at capture exactly like a loader, so it postpones instead of settling into the shared shell (see "live()") |
|
|
97
|
+
| Shell-safe, deterministic data | `await` it in a **handler** — it is shell material, frozen into the prelude |
|
|
98
|
+
| **Per-user** data | a `loader` (masked at capture, always fresh), or **`live()`** for a boundary that is not a route loader. NEVER a handler or middleware-derived `ctx` state — those run during capture and bake into the **shared** shell (see Security) |
|
|
99
|
+
| A slow nested value on a route with **no** `loading()` | still fine on axis 1: the tree-build await is SHALLOW, so `{ outer, pendingData }` streams the inner under the consumer's `<Suspense>` — but the route is not shell-cacheable |
|
|
100
|
+
|
|
101
|
+
The last row is the common trap: "no `loading()` blocks" does NOT mean "nested
|
|
102
|
+
promises can't stream". They stream today, unchanged by PPR — the route just
|
|
103
|
+
has no hole, so shell caching stays off for it (eternal MISS, below).
|
|
104
|
+
|
|
105
|
+
## The hole contract (read this before wiring a route)
|
|
106
|
+
|
|
107
|
+
A hole exists ONLY where the route-level `loading()` boundary separates loader
|
|
108
|
+
consumption from the shell. `loading()` becomes `LoaderBoundary`
|
|
109
|
+
(`src/route-content-wrapper.tsx`) — a `<Suspense>` whose resolver `use()`es the
|
|
110
|
+
loader promise INSIDE it — so the masked loader postpones exactly there and the
|
|
111
|
+
prelude freezes the layouts plus the fallback. Two consequences:
|
|
112
|
+
|
|
113
|
+
1. Shell material (static content, handle reads, interactive client islands)
|
|
114
|
+
lives in a **layout** above the loader route.
|
|
115
|
+
2. The loader-consuming route below carries **`loading()`**.
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
export const urlpatterns = urls(({ path, layout, loader, loading }) => [
|
|
119
|
+
// Shell: header, nav, islands, handle pushes. Frozen into the prelude.
|
|
120
|
+
layout(ProductShellLayout, () => [
|
|
121
|
+
// Hole: the live price. Masked at capture, fresh on every serve.
|
|
122
|
+
path("/products/:id", PricePage, { name: "product" }, () => [
|
|
123
|
+
loader(LivePriceLoader),
|
|
124
|
+
loading(<PriceSkeleton />), // the boundary capture postpones at
|
|
125
|
+
]),
|
|
126
|
+
]),
|
|
127
|
+
]);
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
**Why a route without `loading()` can never be a hole — even with a fast
|
|
131
|
+
loader.** The loading-less branch in `renderSegments` (`src/segment-system.tsx`)
|
|
132
|
+
awaits loader data at TREE-BUILD (`await buildLoaderPromise(...)`), above every
|
|
133
|
+
Suspense boundary. That await is SHALLOW — it settles only the loader's OUTER
|
|
134
|
+
value — but during capture the loader is masked WHOLE: `createMaskedLoaderPromise`
|
|
135
|
+
(`src/router/segment-resolution/loader-mask.ts`) hands back a never-resolving
|
|
136
|
+
promise for the entire value, so even the outer never settles. The tree-build
|
|
137
|
+
await pins the whole tree above `<body>`, the prelude comes back trivial, and
|
|
138
|
+
the sanity gate refuses to store. Observable symptom: `x-rango-shell: MISS` on
|
|
139
|
+
every request forever, plus a **once-per-key** worker warning you can grep for —
|
|
140
|
+
`produced no usable shell … without a route-level loading() boundary`
|
|
141
|
+
(`src/rsc/shell-capture.ts`).
|
|
142
|
+
|
|
143
|
+
Whole-loader masking is deliberate scar tissue, not a limitation to route
|
|
144
|
+
around. Loaders are the ONE lane exempt from the `cookies()`/`headers()` capture
|
|
145
|
+
guard — they always run fresh on serve — so running a loader even _partially_
|
|
146
|
+
during capture could bake a per-user outer field into the shared shell, breaking
|
|
147
|
+
freshness and the security model at once. Finer-grained masking is intentionally
|
|
148
|
+
not offered. A hand-rolled `<Suspense>` around a `useLoader()` reader does not
|
|
149
|
+
help either: the pin is at the tree-build await, which is _above_ it.
|
|
150
|
+
|
|
151
|
+
## Loader-carried promises: streaming inside a hole
|
|
152
|
+
|
|
153
|
+
A hole is not limited to one value. A loader can return its outer value fast and
|
|
154
|
+
carry a **nested promise** that settles later; `FlightSerialize` preserves the
|
|
155
|
+
`Promise` across the RSC boundary (`src/serialize.ts`), so the client `use()`es
|
|
156
|
+
it under its OWN inner `<Suspense>` — a second streaming layer _inside_ the hole.
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
// loader — outer resolves fast; the nested promise settles later
|
|
160
|
+
export const StreamLoader = createLoader(async () => {
|
|
161
|
+
const pendingData = new Promise<string>((r) =>
|
|
162
|
+
setTimeout(() => r("slow inner value"), 300),
|
|
163
|
+
);
|
|
164
|
+
return { label: "fast outer value", pendingData };
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
```tsx
|
|
169
|
+
// consumer (client): use() the nested promise under an INNER Suspense
|
|
170
|
+
"use client";
|
|
171
|
+
import { Suspense, use } from "react";
|
|
172
|
+
import { useLoader } from "@rangojs/router/client";
|
|
173
|
+
|
|
174
|
+
function Inner({ promise }: { promise: Promise<string> }) {
|
|
175
|
+
return <span>{use(promise)}</span>;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function StreamView({ loader }: { loader: LoaderDefinition<Data> }) {
|
|
179
|
+
const { data } = useLoader(loader); // resolves the OUTER value
|
|
180
|
+
return (
|
|
181
|
+
<>
|
|
182
|
+
<div>{data.label}</div>
|
|
183
|
+
<Suspense fallback={<div>loading inner…</div>}>
|
|
184
|
+
<Inner promise={data.pendingData} /> {/* streams the nested value */}
|
|
185
|
+
</Suspense>
|
|
186
|
+
</>
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Route shape is unchanged: `loader(StreamLoader)` + `loading(<Skeleton />)`. On a
|
|
192
|
+
**HIT** the resume streams three progressive layers in one response body:
|
|
193
|
+
|
|
194
|
+
1. the cached shell prelude (layout + the `loading()` fallback) — flushed
|
|
195
|
+
instantly, before any render work;
|
|
196
|
+
2. the outer loader value fills the hole, carrying the inner `<Suspense>`
|
|
197
|
+
fallback;
|
|
198
|
+
3. the nested-promise inner value + React's `$RC` boundary stitch.
|
|
199
|
+
|
|
200
|
+
Capture never sees any of this: the loader is masked, so the whole subtree
|
|
201
|
+
postpones at `loading()` and the nested promise costs nothing at capture time.
|
|
202
|
+
That is what makes loader-carried promises DETERMINISTIC — contrast the
|
|
203
|
+
handler-passed promise below, which races the capture's quiet window. The
|
|
204
|
+
three-layer timeline is pinned in dev + production e2e
|
|
205
|
+
(`tests/cloudflare-basic/e2e/ppr-shell.test.ts`, `e2e/shell-cache.test.ts`).
|
|
206
|
+
|
|
207
|
+
One nuance: a loader with a `cache(...)` config deep-settles on write, so a
|
|
208
|
+
loader-cache HIT delivers the inner promise already resolved.
|
|
209
|
+
|
|
210
|
+
## live(): a deterministic hole for any boundary
|
|
211
|
+
|
|
212
|
+
`loading()` makes a route LOADER a hole. `live()` makes ANY boundary a hole —
|
|
213
|
+
including one whose data is already resolved. During the background capture
|
|
214
|
+
`live()` behaves exactly like the loader mask: it returns a never-settling
|
|
215
|
+
promise, so the consuming `<Suspense>` postpones and the prelude freezes only the
|
|
216
|
+
fallback. On the serve pass (and on the client) it is a passthrough — the thunk
|
|
217
|
+
runs, or the promise passes through unchanged.
|
|
218
|
+
|
|
219
|
+
```tsx
|
|
220
|
+
import { Suspense } from "react";
|
|
221
|
+
import { live } from "@rangojs/router";
|
|
222
|
+
|
|
223
|
+
async function Greeting() {
|
|
224
|
+
// Promise.resolve(...) would normally SETTLE during capture and bake into the
|
|
225
|
+
// shared shell. live() holds it out, so this boundary postpones instead.
|
|
226
|
+
const name = await live(() => Promise.resolve(currentUserName()));
|
|
227
|
+
return <span>Hi {name}</span>;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// under the frozen shell:
|
|
231
|
+
// <Suspense fallback={<span>…</span>}>
|
|
232
|
+
// <Greeting />
|
|
233
|
+
// </Suspense>
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
Two forms:
|
|
237
|
+
|
|
238
|
+
- **Thunk (preferred): `live(() => value)`** — during capture the thunk NEVER
|
|
239
|
+
runs (no fetch, no cost); the boundary is a pure hole.
|
|
240
|
+
- **Value: `live(promise)`** — the work already fired before `live()` saw it, so
|
|
241
|
+
during capture the real promise is DISCARDED and a hole returned in its place.
|
|
242
|
+
Use it only when you already hold the promise; prefer the thunk otherwise.
|
|
243
|
+
|
|
244
|
+
`live()` is what makes a resolved value a hole at all: a bare `Promise.resolve(x)`
|
|
245
|
+
under `<Suspense>` settles inside the capture's quiet window and freezes into the
|
|
246
|
+
shell. It is also the escape hatch for the passed-promise trap below. The
|
|
247
|
+
capture/serve split is pinned in dev + production e2e (the "live() makes a
|
|
248
|
+
resolved promise a HOLE" case in `tests/cloudflare-basic/e2e/ppr-shell.test.ts`
|
|
249
|
+
and `e2e/shell-cache.test.ts`).
|
|
250
|
+
|
|
251
|
+
## Passed promises are not holes
|
|
252
|
+
|
|
253
|
+
The pattern that looks like a hole but is not: a **handler** creates a promise
|
|
254
|
+
and passes it as a prop to a client component that `use()`s it inside its own
|
|
255
|
+
`<Suspense>`. Only route **loaders** (and `live()`) are masked at capture — a
|
|
256
|
+
handler and any promise it creates EXECUTE during the background capture render.
|
|
257
|
+
What happens next is decided by the promise's LATENCY CLASS against the
|
|
258
|
+
capture's quiet window (task-quantized: it closes a couple of macrotask hops
|
|
259
|
+
after the last Flight byte, not on a wall clock). Both sides are reliable —
|
|
260
|
+
just in opposite directions:
|
|
261
|
+
|
|
262
|
+
- Resolved or microtask-resolvable (`Promise.resolve`, a warm in-memory read):
|
|
263
|
+
reliably SHELL, every capture — it settles in the same window as plain JSX.
|
|
264
|
+
If the value is per-request, that is a deterministic bug: frozen into the
|
|
265
|
+
shared shell until TTL (hydration repairs it from the fresh payload —
|
|
266
|
+
degraded, not corrupt, but a drift you shipped).
|
|
267
|
+
- Genuinely pending real I/O: reliably a HOLE — it cannot win a task-quantized
|
|
268
|
+
window. Resume fills it at serve. The capture still paid the promise's
|
|
269
|
+
execution cost and side effects, though. The only nondeterministic sliver
|
|
270
|
+
left is I/O completing within ~2 event-loop turns of the shell going quiet
|
|
271
|
+
— freakishly fast, self-healing via TTL/recapture, and only reachable by
|
|
272
|
+
code that declared no intent.
|
|
273
|
+
|
|
274
|
+
An async HANDLER (a streamed `loading()` handler returning a promise) is the
|
|
275
|
+
deliberate opposite: it is tracked in the handle store, and capture WAITS for
|
|
276
|
+
handlers to settle before aborting — handler output is shell material by
|
|
277
|
+
design, never a hole.
|
|
278
|
+
|
|
279
|
+
Verdict: a promise's latency class picks its side — you can safely assume a
|
|
280
|
+
genuinely pending, unresolved promise becomes a hole. But that decision was
|
|
281
|
+
made by latency, not by you. Wherever intent and latency could disagree —
|
|
282
|
+
per-request data that might get cache-fast, a value that must never appear in
|
|
283
|
+
the shared shell — say it in code: **`live()`** for a guaranteed hole (masked
|
|
284
|
+
at capture like a loader; prefer the thunk form so nothing runs during
|
|
285
|
+
capture), a loader behind `loading()` for route-level live data (zero capture
|
|
286
|
+
cost, and its nested promises stream too, per above), a plain `await` for
|
|
287
|
+
shell-safe deterministic data. Unwrapped promises are for the cases where
|
|
288
|
+
either outcome is acceptable.
|
|
289
|
+
|
|
290
|
+
Note on `useLoader()`: it never observes pending data. Inside a `loading()`
|
|
291
|
+
route, `LoaderBoundary` resolves the loader promise INSIDE its own Suspense
|
|
292
|
+
before children render, so `useLoader().data` (the OUTER value) is always
|
|
293
|
+
resolved; `isLoading` is client-side refetch state, not a server pending signal.
|
|
294
|
+
A nested promise on that data is separate — it streams under the consumer's own
|
|
295
|
+
inner `<Suspense>` (above). Multiple holes per page (several `loading()`
|
|
296
|
+
routes/parallels) are fine: resume fills every postponed boundary.
|
|
297
|
+
|
|
298
|
+
## Execution matrix
|
|
299
|
+
|
|
300
|
+
Three passes, three different cost profiles. The foreground request is never
|
|
301
|
+
blocked on the background capture.
|
|
302
|
+
|
|
303
|
+
| Phase | MISS (foreground) | Background capture | HIT (foreground) |
|
|
304
|
+
| ---------------- | ---------------------- | --------------------------------------------------------------- | ----------------------------------------------------------- |
|
|
305
|
+
| Middleware chain | runs (full) | **NOT re-run** — inherits the request's post-middleware context | runs (full) |
|
|
306
|
+
| `router.match` | runs | re-runs under a derived context | runs |
|
|
307
|
+
| Handlers | run | run | run |
|
|
308
|
+
| Loaders | run **fresh** | **MASKED** (never execute) | run **fresh** |
|
|
309
|
+
| Flight render | full | full | full (hydration needs the whole payload — no Flight resume) |
|
|
310
|
+
| HTML production | full fizz | `prerender` + abort → prelude + postponed | `resume` only the holes — O(paths to holes) |
|
|
311
|
+
| Shell store | schedules a bg capture | `putShell(key, …)` | `getShell(key)`; a stale/SWR hit also schedules a recapture |
|
|
312
|
+
| Prelude bytes | — | — | prepended by the middleware before the resumed body |
|
|
313
|
+
|
|
314
|
+
Loader freshness under PPR is **identical to axis 1**: loaders — the outer value
|
|
315
|
+
AND any nested promise — run fresh on every request, including HITs. Only the
|
|
316
|
+
HTML _around_ the hole came from cache. Background capture is scheduled via
|
|
317
|
+
`runBackground` (`waitUntil` on workerd, fire-and-forget in Node dev), so it
|
|
318
|
+
never delays the served response. Re-deriving through `router.match()` rather
|
|
319
|
+
than a second `next()` is what keeps middleware from running twice
|
|
320
|
+
(`src/rsc/shell-capture.ts`).
|
|
321
|
+
|
|
322
|
+
## Security
|
|
323
|
+
|
|
324
|
+
Shell caching shares one shell per host+URL across all users, so its safety
|
|
325
|
+
rests on three things — the first two are enforced, the third is on you.
|
|
326
|
+
|
|
327
|
+
**(a) Access control is sound.** The middleware runs on every request, including
|
|
328
|
+
HITs, and composition is **marker-gated**: the middleware prepends the cached
|
|
329
|
+
prelude ONLY when the live response carries the internal `x-rango-shell-resumed`
|
|
330
|
+
marker (`src/cache/shell-cache.ts`). Any middleware short-circuit — a 401, a
|
|
331
|
+
redirect, a 404 — never resumes, so it never carries the marker and passes
|
|
332
|
+
through **untouched**, never composed with a cached shell. Put auth middleware
|
|
333
|
+
upstream of the shell middleware and unauthorized users get their 401/redirect,
|
|
334
|
+
not someone else's cached page.
|
|
335
|
+
|
|
336
|
+
**(b) Identity can't leak into a shared shell.** `cookies()` and `headers()`
|
|
337
|
+
THROW during the background capture render (`assertNotInsideShellCapture`,
|
|
338
|
+
`src/server/cookie-store.ts`), the same guard family as `"use cache"` and
|
|
339
|
+
`cache()`. A shell that reads cookies is PPR-ineligible by construction.
|
|
340
|
+
|
|
341
|
+
**(c) Residual hazard — state it plainly.** Middleware-derived per-user state is
|
|
342
|
+
NOT guarded: a `ctx` variable set by an upstream auth middleware and read by a
|
|
343
|
+
handler WITHOUT `cookies()`/`headers()` is invisible to guard (b). The background
|
|
344
|
+
capture inherits the triggering request's post-middleware context and bakes that
|
|
345
|
+
state into the shared shell. Mitigations, in order of preference:
|
|
346
|
+
|
|
347
|
+
- shell-cache only **public/shared** pages;
|
|
348
|
+
- put all per-user content in **loaders** (the enforced, masked lane);
|
|
349
|
+
- `isEnabled` to disable the middleware for authenticated sessions;
|
|
350
|
+
- `keyGenerator` to add a per-variant dimension (it owns the FULL key identity,
|
|
351
|
+
including host — see Options).
|
|
352
|
+
|
|
353
|
+
Shell content that still varies per request degrades to a hydration repair
|
|
354
|
+
(bounded by TTL/SWR), not corruption — but it is a smell. `cache()` the route so
|
|
355
|
+
the same replayed segments feed the captured shell and every resumed render.
|
|
356
|
+
|
|
357
|
+
## What always stays on axis 1
|
|
358
|
+
|
|
359
|
+
Non-GET, RSC/partial/action/loader fetches, per-request CSP nonce,
|
|
360
|
+
`streamMode: "allReady"`, redirects, 404s, error renders, and any store without
|
|
361
|
+
the shell family. A stored shell is also invalidated when `React.version`
|
|
362
|
+
changes (postponed state is build-coupled), so deploys self-heal via recapture.
|
|
363
|
+
|
|
364
|
+
First byte on a HIT does not wait on the shell render or the loader. Hydration
|
|
365
|
+
uses the fresh per-request Flight payload, so interactivity is unaffected.
|
|
366
|
+
|
|
367
|
+
## Partial navigations
|
|
368
|
+
|
|
369
|
+
Soft navigations (`_rsc_partial`) bypass this middleware, and that is by
|
|
370
|
+
design, not a gap: a partial response has no HTML tier — no fizz render to
|
|
371
|
+
skip, which is the entire cost document-PPR eliminates. On a `cache()` route a
|
|
372
|
+
partial navigation already delivers the PPR contract at the data tier:
|
|
373
|
+
replayed cached segments flush immediately (in-memory after the store read),
|
|
374
|
+
loaders run fresh and stream their rows into the same response, and the
|
|
375
|
+
client shows `loading()` fallbacks until they arrive. Shell instantly, live
|
|
376
|
+
holes revived — same semantics, different wire format.
|
|
377
|
+
|
|
378
|
+
For warm-navigation latency, combine `cache()` with prefetching: a prefetched
|
|
379
|
+
partial payload is the client-side analogue of the shell cache, and loaders
|
|
380
|
+
still stream fresh on arrival. Document-PPR covers the cold full-document
|
|
381
|
+
load; prefetch + `cache()` covers navigation.
|
|
382
|
+
|
|
383
|
+
What partial navigations do NOT get is the byte-level shortcut: the worker
|
|
384
|
+
still deserializes stored segments and re-encodes the Flight payload per
|
|
385
|
+
request. Serving a stored Flight byte-prefix and appending fresh loader rows
|
|
386
|
+
would require hand-managed row-ID alignment — React has no Flight-side
|
|
387
|
+
resume (no postponed-state equivalent exists for Flight) — and is a deferred
|
|
388
|
+
optimization, tracked in the design doc's out-of-scope list.
|
|
389
|
+
|
|
390
|
+
## Options
|
|
391
|
+
|
|
392
|
+
| Option | Default | Notes |
|
|
393
|
+
| -------------- | ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
|
|
394
|
+
| `store` | app-level `_cacheStore` | must implement `getShell`/`putShell`; the capture writes to the SAME store the middleware reads |
|
|
395
|
+
| `ttlSeconds` | `300` | shell freshness window |
|
|
396
|
+
| `swrSeconds` | — | stale window: serve stale + background recapture |
|
|
397
|
+
| `keyGenerator` | `${host}${pathname}${sortedSearch}` | custom keys own the FULL identity — include the host unless the store is provably single-host (multi-tenant shells must never collide) |
|
|
398
|
+
| `isEnabled` | — | per-request opt-out predicate (e.g. disable for authed sessions) |
|
|
399
|
+
| `skipPaths` | `[]` | path-prefix opt-out |
|
|
400
|
+
| `debug` | `false` | HIT/MISS/CAPTURED logging |
|
|
401
|
+
|
|
402
|
+
## Pitfalls
|
|
403
|
+
|
|
404
|
+
- **Loader route without `loading()`**: eternal MISS plus a once-per-key
|
|
405
|
+
console warning. Move shell material to a layout and add `loading()` (see "The
|
|
406
|
+
hole contract").
|
|
407
|
+
- **Handler-passed promise for live data**: nondeterministic race, drift into
|
|
408
|
+
the shared shell. Use a loader behind `loading()`, or wrap it in `live()`.
|
|
409
|
+
- **`live()` value form (`live(promise)`)**: the work already fired before
|
|
410
|
+
`live()` saw it, so during capture the promise still runs and its side effects
|
|
411
|
+
still happen — only its result is held out of the shell. Prefer the thunk form
|
|
412
|
+
`live(() => …)` so nothing executes during capture.
|
|
413
|
+
- **Per-user state via `ctx` variables**: not guarded — see Security (c).
|
|
414
|
+
- **Stacking with `/document-cache`**: pick one per route. The document cache
|
|
415
|
+
would cache the composite — correct output, but it makes shell caching
|
|
416
|
+
redundant there.
|
|
417
|
+
- **Dev + HMR**: works, but edits produce stale shells until TTL/recapture.
|
|
418
|
+
- A cold-worker capture can occasionally abort mid-render; it is logged as
|
|
419
|
+
retryable and the next request recaptures — self-healing, not an error.
|
|
420
|
+
|
|
421
|
+
## Related
|
|
422
|
+
|
|
423
|
+
- `/document-cache` — whole-response edge caching (no live holes)
|
|
424
|
+
- `/caching` and `/cache-guide` — segment/function caching (axis 1 data)
|
|
425
|
+
- `/shell-manifest` — replayed handles as cache metadata read by live loaders
|
|
426
|
+
- Design doc: `docs/design/ppr-shell-resume.md` in the package
|
package/skills/rango/SKILL.md
CHANGED
|
@@ -95,21 +95,22 @@ stated, greppable contract.
|
|
|
95
95
|
|
|
96
96
|
## Pick a primitive
|
|
97
97
|
|
|
98
|
-
| I need to…
|
|
99
|
-
|
|
|
100
|
-
| render data fresh every request
|
|
101
|
-
| cache a rendered subtree
|
|
102
|
-
| cache one function/component's result
|
|
103
|
-
| cache a loader's data
|
|
104
|
-
| re-render a segment after an action
|
|
105
|
-
| mutate
|
|
106
|
-
| debug a slow request
|
|
107
|
-
| share config across routes
|
|
108
|
-
| compose a sub-app / module
|
|
109
|
-
| modal / soft navigation
|
|
110
|
-
| pre-render a route at build time
|
|
111
|
-
| feed live loaders from a cached shell
|
|
112
|
-
|
|
|
98
|
+
| I need to… | Use | Skill |
|
|
99
|
+
| --------------------------------------- | ---------------------------------- | ----------------------- |
|
|
100
|
+
| render data fresh every request | `loader()` + `useLoader()` | /loader |
|
|
101
|
+
| cache a rendered subtree | `cache()` on a segment | /caching |
|
|
102
|
+
| cache one function/component's result | `"use cache"` | /use-cache |
|
|
103
|
+
| cache a loader's data | `loader(L, () => [cache()])` | /loader, /caching |
|
|
104
|
+
| re-render a segment after an action | `revalidate()` | /loader |
|
|
105
|
+
| mutate | `"use server"` action | /server-actions |
|
|
106
|
+
| debug a slow request | `debugPerformance` / telemetry | /observability |
|
|
107
|
+
| share config across routes | factory returning a helper array | /composability |
|
|
108
|
+
| compose a sub-app / module | `include()` | /route |
|
|
109
|
+
| modal / soft navigation | `intercept()` | /intercept |
|
|
110
|
+
| pre-render a route at build time | `Prerender(...)` wrapper | /prerender |
|
|
111
|
+
| feed live loaders from a cached shell | replayed handle + `ctx.rendered()` | /shell-manifest |
|
|
112
|
+
| cache the HTML shell, keep loaders live | `createShellCacheMiddleware()` | /ppr |
|
|
113
|
+
| stream SSE / upgrade a WebSocket | `path.stream()` / `path.any()` | /streams-and-websockets |
|
|
113
114
|
|
|
114
115
|
## Invariants
|
|
115
116
|
|
|
@@ -152,6 +153,7 @@ Same words, different jobs — this is the most common source of the
|
|
|
152
153
|
| 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. |
|
|
153
154
|
| React Router / Remix `shouldRevalidate` | **Axis 2** | This is the correct mental model for Rango's `revalidate()`. |
|
|
154
155
|
| HTTP `Cache-Control` / ISR | **Axis 1** | Edge/document layer — see `/document-cache`. Separate from both `cache()` and `revalidate()`. |
|
|
156
|
+
| Next.js PPR (partial prerendering) | HTML shell layer | Same idea, different wiring: opt-in `createShellCacheMiddleware()` captures at runtime (no build-time default), and holes are route-level `loading()` boundaries — a hand-rolled `<Suspense>` is not a hole. See `/ppr`. |
|
|
155
157
|
| 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. |
|
|
156
158
|
|
|
157
159
|
See `/cache-guide` for the axis-1 decision guide, `/loader` and `/route` for
|
|
@@ -247,16 +249,17 @@ Grouped by concern — read when you need to…
|
|
|
247
249
|
|
|
248
250
|
**Data & caching** — fetch, mutate, and cache:
|
|
249
251
|
|
|
250
|
-
| Skill | Description
|
|
251
|
-
| ----------------- |
|
|
252
|
-
| `/loader` | Data loaders with `createLoader()` and `revalidate()`
|
|
253
|
-
| `/server-actions` | Mutations with `"use server"`, useActionState, validation, revalidation
|
|
254
|
-
| `/caching` | Segment caching with memory or KV stores
|
|
255
|
-
| `/use-cache` | Function-level caching with `"use cache"` directive
|
|
256
|
-
| `/cache-guide` | When to use `cache()` vs `"use cache"` — differences and decision guide
|
|
257
|
-
| `/document-cache` | Edge caching with Cache-Control headers
|
|
258
|
-
| `/
|
|
259
|
-
| `/
|
|
252
|
+
| Skill | Description |
|
|
253
|
+
| ----------------- | -------------------------------------------------------------------------------------------- |
|
|
254
|
+
| `/loader` | Data loaders with `createLoader()` and `revalidate()` |
|
|
255
|
+
| `/server-actions` | Mutations with `"use server"`, useActionState, validation, revalidation |
|
|
256
|
+
| `/caching` | Segment caching with memory or KV stores |
|
|
257
|
+
| `/use-cache` | Function-level caching with `"use cache"` directive |
|
|
258
|
+
| `/cache-guide` | When to use `cache()` vs `"use cache"` — differences and decision guide |
|
|
259
|
+
| `/document-cache` | Edge caching with Cache-Control headers |
|
|
260
|
+
| `/ppr` | PPR shell caching: cached HTML shell served instantly, live loader holes resumed per request |
|
|
261
|
+
| `/prerender` | Pre-render route segments at build time (Passthrough live fallback) |
|
|
262
|
+
| `/shell-manifest` | Replayed handles as cache metadata read by live loaders (frozen shell, batched live holes) |
|
|
260
263
|
|
|
261
264
|
**Client & presentation** — build the client-side UX:
|
|
262
265
|
|
package/skills/route/SKILL.md
CHANGED
|
@@ -53,6 +53,49 @@ For the common pattern of an optional locale prefix
|
|
|
53
53
|
locale detection, fallback chains, URL generation with absent locale —
|
|
54
54
|
see `/i18n`.
|
|
55
55
|
|
|
56
|
+
### Named catch-all params (`:name+` / `:name*`)
|
|
57
|
+
|
|
58
|
+
A catch-all consumes the **rest of the path** and exposes it as a single
|
|
59
|
+
decoded string at `ctx.params.<name>`, with the internal `/` separators kept.
|
|
60
|
+
It must be the **last** segment of the pattern.
|
|
61
|
+
|
|
62
|
+
- `:name+` — **one-or-more** segments (Next `[...name]`, React-Router splat).
|
|
63
|
+
`/docs/:slug+` matches `/docs/a` and `/docs/a/b/c`, but **not** the bare
|
|
64
|
+
`/docs`.
|
|
65
|
+
- `:name*` — **zero-or-more** segments (Next `[[...name]]`). `/docs/:slug*`
|
|
66
|
+
additionally matches the bare `/docs`, binding `ctx.params.slug` to `""`.
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
urls(({ path }) => [
|
|
70
|
+
// /shop/electronics/phones -> ctx.params.path === "electronics/phones"
|
|
71
|
+
path("/shop/:path+", ShopCatchAll, { name: "shopCatchAll" }),
|
|
72
|
+
|
|
73
|
+
// /docs -> ctx.params.slug === ""
|
|
74
|
+
// /docs/intro -> ctx.params.slug === "intro"
|
|
75
|
+
// /docs/a/b -> ctx.params.slug === "a/b"
|
|
76
|
+
path("/docs/:slug*", (ctx) => {
|
|
77
|
+
const parts = ctx.params.slug === "" ? [] : ctx.params.slug.split("/");
|
|
78
|
+
return <Docs segments={parts} />;
|
|
79
|
+
}, { name: "docs" }),
|
|
80
|
+
]);
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
`ctx.params.<name>` is always a `string` for a catch-all (never `undefined`) —
|
|
84
|
+
`:name*` binds `""` for the empty case, so read it directly. `reverse()` /
|
|
85
|
+
`ctx.reverse()` rebuild the URL with separators preserved:
|
|
86
|
+
`reverse("docs", { slug: "a/b" })` -> `/docs/a/b`.
|
|
87
|
+
|
|
88
|
+
The value is the URL-decoded remainder. `split("/")` recovers the segments in the
|
|
89
|
+
common case, but note that a segment containing an encoded slash (`%2F`) decodes
|
|
90
|
+
to a literal `/` and is therefore indistinguishable from a separator — the same
|
|
91
|
+
trade-off the bare `*` splat has. If you need to distinguish those, match on the
|
|
92
|
+
raw pathname instead.
|
|
93
|
+
|
|
94
|
+
The bare unnamed wildcard `path("/files/*", …)` still works and is read at
|
|
95
|
+
`ctx.params["*"]`; prefer a named catch-all when you want a typed param key.
|
|
96
|
+
Combining a modifier with `?`, a literal suffix, or a constraint
|
|
97
|
+
(`:slug*?`, `:slug*.html`, `:slug(a|b)+`) is rejected at build time.
|
|
98
|
+
|
|
56
99
|
## Route Handler Patterns
|
|
57
100
|
|
|
58
101
|
### Component Function
|
package/src/build/route-trie.ts
CHANGED
|
@@ -62,8 +62,13 @@ export interface TrieNode {
|
|
|
62
62
|
p?: { n: string; c: TrieNode };
|
|
63
63
|
/** Suffix-param children keyed by suffix (e.g., ".html" → { n: "productId", c: ... }) */
|
|
64
64
|
xp?: Record<string, { n: string; c: TrieNode }>;
|
|
65
|
-
/**
|
|
66
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Wildcard terminal: leaf + paramName (`pn`). `pn` is "*" for the bare `/*`
|
|
67
|
+
* form and the param name for a named catch-all (`:name+`/`:name*`). `w1`
|
|
68
|
+
* marks a one-or-more catch-all (`:name+`): the runtime walker then rejects
|
|
69
|
+
* the zero-segment/empty-remainder case. Absent `w1` is zero-or-more.
|
|
70
|
+
*/
|
|
71
|
+
w?: TrieLeaf & { pn: string; w1?: true };
|
|
67
72
|
}
|
|
68
73
|
|
|
69
74
|
/**
|
|
@@ -383,12 +388,35 @@ function insertSegments(
|
|
|
383
388
|
} else if (segment.type === "wildcard") {
|
|
384
389
|
// Wildcard consumes all remaining segments. Carry any params bound before
|
|
385
390
|
// the wildcard in pa so they zip correctly against paramValues at match.
|
|
386
|
-
|
|
391
|
+
// `pn` is "*" for the bare `/*` and the param name for a named catch-all;
|
|
392
|
+
// `w1` marks the one-or-more variant (`:name+`) so the walker rejects the
|
|
393
|
+
// empty-remainder case.
|
|
394
|
+
const wildLeaf: TrieLeaf & { pn: string; w1?: true } = {
|
|
387
395
|
...buildLeaf(leafBase, paramNames),
|
|
388
|
-
pn:
|
|
396
|
+
pn: segment.value,
|
|
397
|
+
...(segment.oneOrMore ? { w1: true as const } : {}),
|
|
389
398
|
};
|
|
390
|
-
const existing = node.w
|
|
391
|
-
|
|
392
|
-
|
|
399
|
+
const existing = node.w;
|
|
400
|
+
// Merge when there's no existing wildcard, when this is a response-type
|
|
401
|
+
// content-negotiation variant of the same catch-all (one side carries `rt`),
|
|
402
|
+
// or when it's the SAME catch-all identity (same param name + arity).
|
|
403
|
+
// Otherwise two DISTINCT catch-all forms (`/x/*` vs `/x/:p+`) would collide on
|
|
404
|
+
// the single wildcard slot with no non-lossy merge — so keep the first-declared
|
|
405
|
+
// (matching the regex matcher's declaration-order tiebreak) rather than let
|
|
406
|
+
// mergeLeaves' last-wins overwrite silently drop its `pn`/`w1` identity (which
|
|
407
|
+
// stranded the first route and fell through to a corrupt regex-fallback redirect).
|
|
408
|
+
const canMerge =
|
|
409
|
+
existing === undefined ||
|
|
410
|
+
Boolean(existing.rt) ||
|
|
411
|
+
Boolean(wildLeaf.rt) ||
|
|
412
|
+
(existing.pn === wildLeaf.pn &&
|
|
413
|
+
Boolean(existing.w1) === Boolean(wildLeaf.w1));
|
|
414
|
+
if (canMerge) {
|
|
415
|
+
const merged = mergeLeaves(
|
|
416
|
+
existing ? ({ ...existing } as TrieLeaf) : undefined,
|
|
417
|
+
wildLeaf,
|
|
418
|
+
);
|
|
419
|
+
node.w = merged as TrieLeaf & { pn: string; w1?: true };
|
|
420
|
+
}
|
|
393
421
|
}
|
|
394
422
|
}
|