@rangojs/router 0.0.0-experimental.140 → 0.0.0-experimental.141
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/vite/index.js +1 -1
- package/package.json +1 -1
- package/skills/ppr/SKILL.md +229 -362
- package/skills/rango/SKILL.md +2 -2
- package/src/cache/cf/cf-cache-store.ts +8 -0
- package/src/cache/index.ts +0 -5
- package/src/cache/shell-snapshot.ts +368 -0
- package/src/cache/types.ts +66 -0
- package/src/cache/vercel/vercel-cache-store.ts +12 -1
- package/src/index.rsc.ts +1 -5
- package/src/index.ts +1 -17
- package/src/rsc/rsc-rendering.ts +279 -89
- package/src/rsc/shell-capture.ts +523 -65
- package/src/rsc/shell-serve.ts +124 -0
- package/src/server/context.ts +7 -0
- package/src/server/request-context.ts +52 -46
- package/src/ssr/index.tsx +38 -6
- package/src/theme/ThemeProvider.tsx +36 -26
- package/src/urls/index.ts +1 -0
- package/src/urls/path-helper.ts +5 -0
- package/src/urls/pattern-types.ts +36 -0
- package/src/cache/shell-cache.ts +0 -386
- package/src/server/live.ts +0 -130
package/skills/ppr/SKILL.md
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: ppr
|
|
3
|
-
description: PPR shell caching —
|
|
3
|
+
description: PPR shell caching — opt a page route in with the `ppr` path option; the router serves the cached HTML shell instantly and resumes the live holes
|
|
4
4
|
argument-hint: "[setup]"
|
|
5
5
|
---
|
|
6
6
|
|
|
7
7
|
# PPR Shell Caching
|
|
8
8
|
|
|
9
|
-
Caches the rendered HTML **shell** of a route (React `prerender` prelude
|
|
10
|
-
plus `postponed` state) and, on a later request, flushes those bytes
|
|
11
|
-
render work happens, then resumes fizz for just the live
|
|
9
|
+
Caches the rendered HTML **shell** of a page route (React `prerender` prelude
|
|
10
|
+
bytes plus `postponed` state) and, on a later request, flushes those bytes
|
|
11
|
+
before any render work happens, then resumes fizz for just the live holes. The
|
|
12
12
|
browser sees one ordinary streamed document; loaders stay fresh on every
|
|
13
13
|
request. This is the second render axis — the default axis-1 path is untouched,
|
|
14
14
|
and every ineligible request falls open to it.
|
|
@@ -17,21 +17,35 @@ Compare `/document-cache`, which freezes the WHOLE response including loader
|
|
|
17
17
|
output. Shell caching is for pages that mix a stable shell with live data: the
|
|
18
18
|
shell is shared per host+URL, the holes are per request.
|
|
19
19
|
|
|
20
|
-
## Setup
|
|
20
|
+
## Setup: one path option, no middleware
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
PPR is a DOCUMENT-level property declared on the page route via the `ppr` path
|
|
23
|
+
option. Serving is **integral to the router** — there is nothing to mount. The
|
|
24
|
+
only prerequisite is an app-level `createRouter({ cache })` store that
|
|
25
|
+
implements the shell family (`getShell`/`putShell`): `MemorySegmentCacheStore`
|
|
26
|
+
(dev/tests), `CFCacheStore` (Cloudflare KV), or `VercelCacheStore` (runtime
|
|
27
|
+
cache). A ppr route on a store without the family stays on axis 1 with a
|
|
28
|
+
once-per-key warning.
|
|
27
29
|
|
|
28
30
|
```typescript
|
|
29
|
-
import { createRouter } from "@rangojs/router";
|
|
30
|
-
import {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
import { createRouter, urls } from "@rangojs/router";
|
|
32
|
+
import { CFCacheStore } from "@rangojs/router/cache";
|
|
33
|
+
|
|
34
|
+
export const urlpatterns = urls(({ path, layout, loader, loading }) => [
|
|
35
|
+
layout(ProductShell, () => [
|
|
36
|
+
path(
|
|
37
|
+
"/products/:id",
|
|
38
|
+
PricePage,
|
|
39
|
+
// `ppr` is the whole opt-in AND the policy. `ppr: true` uses the default
|
|
40
|
+
// ttl (300s); an object sets ttl/swr/tags (PartialPrerenderProps).
|
|
41
|
+
{ name: "product", ppr: { ttl: 600, swr: 120 } },
|
|
42
|
+
() => [
|
|
43
|
+
loader(LivePriceLoader),
|
|
44
|
+
loading(<PriceSkeleton />), // the structural hole boundary
|
|
45
|
+
],
|
|
46
|
+
),
|
|
47
|
+
]),
|
|
48
|
+
]);
|
|
35
49
|
|
|
36
50
|
const router = createRouter<AppBindings>({
|
|
37
51
|
document: Document,
|
|
@@ -40,383 +54,236 @@ const router = createRouter<AppBindings>({
|
|
|
40
54
|
store: new CFCacheStore({ kv: env.CACHE_KV, ctx: ctx! }),
|
|
41
55
|
}),
|
|
42
56
|
});
|
|
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
57
|
export default router;
|
|
51
58
|
```
|
|
52
59
|
|
|
60
|
+
A route WITHOUT the `ppr` option is pure axis 1: no store read, no capture, no
|
|
61
|
+
logs, zero cost. `ppr` is per page route — declaring it on a layout is not
|
|
62
|
+
supported (subtree inheritance is a possible follow-up).
|
|
63
|
+
|
|
53
64
|
## Where PPR sits: the cache onion
|
|
54
65
|
|
|
55
66
|
Rango's caches layer like an onion — each ring stores a progressively more
|
|
56
|
-
"cooked" representation of the same page
|
|
57
|
-
|
|
58
|
-
(final bytes):
|
|
67
|
+
"cooked" representation of the same page. From innermost (raw values) to
|
|
68
|
+
outermost (final bytes):
|
|
59
69
|
|
|
60
70
|
| Ring | Primitive | What is stored | What stays live on a hit |
|
|
61
71
|
| ----------------------- | ---------------------------------------- | -------------------------------------------------- | -------------------------------------- |
|
|
62
72
|
| 1. Function values | `"use cache"` | a function's return value | everything around the call |
|
|
63
73
|
| 2. Loader values | `loader(Fn, () => [cache({...})])` | one loader's result (opt-in; loaders default live) | all other loaders, handlers, rendering |
|
|
64
74
|
| 3. Segments (Flight) | `cache()` route / build-time `prerender` | serialized rendered segments + replayed handles | loaders, HTML render |
|
|
65
|
-
| 4. **HTML shell (PPR)** | `
|
|
75
|
+
| 4. **HTML shell (PPR)** | `ppr` path option | rendered prelude bytes + React postponed state | the holes, hydration payload |
|
|
66
76
|
| 5. Whole response | `/document-cache` | final response bytes, headers included | nothing — all-or-nothing |
|
|
67
77
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
78
|
+
PPR is ORTHOGONAL to `cache()` (ring 3): a ppr route may be uncached (its
|
|
79
|
+
handlers run fresh on every serve and during capture), fully `cache()`d (its
|
|
80
|
+
segments replay), or mixed. One useful cache() property to know: the segment
|
|
81
|
+
codec **deep-settles promises at the ring-3 write**, so nothing inside a
|
|
82
|
+
`cache()` boundary can stay live — that is a cache() fact, not a ppr one.
|
|
83
|
+
|
|
84
|
+
Invalidation crosses rings: `updateTag()`/`revalidateTag()` reach segment,
|
|
85
|
+
shell, loader, and item entries in the same store, and shell entries
|
|
86
|
+
additionally self-invalidate on `React.version` change.
|
|
87
|
+
|
|
88
|
+
## The serve pipeline: commit after ALL middleware
|
|
89
|
+
|
|
90
|
+
On a document GET to a ppr route the router runs:
|
|
91
|
+
|
|
92
|
+
1. **match** — route identified, `ppr` config read from the matched route;
|
|
93
|
+
2. **the WHOLE middleware chain** — the global `router.use()` chain AND route
|
|
94
|
+
DSL `middleware()`; both are guards, and the COMMIT POINT is after all of
|
|
95
|
+
them: any rejection/redirect/401 wins before a single shell byte, on MISS
|
|
96
|
+
and on a warmed HIT alike;
|
|
97
|
+
3. **shell lookup** — `getShell(key)` on the app store (key =
|
|
98
|
+
host+pathname+sorted search);
|
|
99
|
+
4. **HIT** — the composed response is committed immediately: the stored prelude
|
|
100
|
+
bytes flush first, while segment resolution, the fresh Flight render (the
|
|
101
|
+
full hydration payload — there is no Flight-side resume), and the fizz
|
|
102
|
+
`resume` of just the holes run BEHIND them inside the response stream;
|
|
103
|
+
5. **MISS** — plain axis-1 serve, tagged `x-rango-shell: MISS`, plus a
|
|
104
|
+
background capture (stampede-guarded, retry-in-place, exponential backoff).
|
|
105
|
+
|
|
106
|
+
`x-rango-shell: HIT | MISS` is the observability header. Because the commit
|
|
107
|
+
point is after the chain, an unauthorized request NEVER sees shell bytes — put
|
|
108
|
+
auth middleware anywhere (global or route DSL) and it guards PPR for free.
|
|
109
|
+
|
|
110
|
+
## The hole doctrine (encode this in your head)
|
|
111
|
+
|
|
112
|
+
Holes are **render-defined**, decided by the shape of the tree, on three rules:
|
|
113
|
+
|
|
114
|
+
| Class | What makes the hole | At capture | At serve |
|
|
115
|
+
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- | ---------------------------------- |
|
|
116
|
+
| **STRUCTURAL** | the ENTIRE segment subtree under a `loading()` registration | loaders masked; the LoaderBoundary postpones; the fallback bakes in as route structure | loaders run fresh; resume fills it |
|
|
117
|
+
| **PHYSICS** | any promise NESTED in handed-over data still pending at capture, under the consumer's own `<Suspense>` — handler props, handle containers (`push({ x: promise })`), loader-carried | real I/O cannot win the task-quantized quiet window; the boundary postpones | the promise settles and streams in |
|
|
118
|
+
| **SHELL** | awaited handler data, TOP-LEVEL `push(promise)` (awaited before SSR), resolved promises, replayed `cache()` segments | baked into the prelude | served from the frozen prelude |
|
|
119
|
+
|
|
120
|
+
The unified rule for promises: **a promise nested inside your data is never
|
|
121
|
+
baked; the container settles.** The one asymmetry to remember versus loaders: a
|
|
122
|
+
LOADER container is a hole via `loading()` (the whole loader value is live),
|
|
123
|
+
while a HANDLE container is shell via root consumption (the handles generator
|
|
124
|
+
is drained before SSR) — only the promises nested inside it stay live.
|
|
125
|
+
|
|
126
|
+
### Handles: "nesting = liveness"
|
|
127
|
+
|
|
128
|
+
- `ctx.use(H)(promise)` — a TOP-LEVEL pushed promise is awaited server-side
|
|
129
|
+
before SSR (`resolvedHandleStream`) and BAKED into the shell. The capture
|
|
130
|
+
gate is held open for the same await, so real latency here is safe (bounded
|
|
131
|
+
by the capture's 5s guard).
|
|
132
|
+
- `ctx.use(H)({ x: promise })` — the container passes through verbatim
|
|
133
|
+
(resolution is shallow); the nested promise streams to the consumer, who must
|
|
134
|
+
`<Suspense>` it. Under capture that boundary postpones — a hole.
|
|
135
|
+
|
|
136
|
+
### Want a hole for already-resolved data?
|
|
137
|
+
|
|
138
|
+
Put it in a loader: `loader(() => Promise.resolve(x))` + `loading()`. Loaders
|
|
139
|
+
are always the live lane — masked at capture, fresh on every serve — no matter
|
|
140
|
+
how fast the value settles.
|
|
141
|
+
|
|
142
|
+
### The structural negative: a loader route without loading()
|
|
143
|
+
|
|
144
|
+
The loading-less branch awaits loader data at TREE-BUILD, above every Suspense
|
|
145
|
+
boundary, so under capture's masked loaders the whole tree pins above `<body>`,
|
|
146
|
+
the prelude comes back trivial, and the sanity gate refuses to store. Observable
|
|
147
|
+
symptom: `x-rango-shell: MISS` forever plus a once-per-key worker warning. Add
|
|
148
|
+
`loading()` to the loader route and keep shell material in a layout.
|
|
116
149
|
|
|
117
|
-
|
|
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
|
-
```
|
|
150
|
+
## Execution matrix
|
|
129
151
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
(
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
152
|
+
| Phase | MISS (foreground) | Background capture | HIT (foreground) |
|
|
153
|
+
| ---------------- | ---------------------- | ------------------------------------------------------------------ | ----------------------------------------------------------- |
|
|
154
|
+
| Middleware chain | runs (full) | **NOT re-run** — inherits the request's post-middleware context | runs (full) — commit point is after it |
|
|
155
|
+
| `router.match` | runs | re-runs under a derived context | runs (behind the flushed prelude) |
|
|
156
|
+
| Handlers | run | run on UNCACHED segments; `cache()`d segments replay (mixed-chain) | run (same mixed-chain rules as any render) |
|
|
157
|
+
| Loaders | run **fresh** | **MASKED** (never execute) — the structural holes | run **fresh** |
|
|
158
|
+
| Flight render | full | full | full (hydration needs the whole payload — no Flight resume) |
|
|
159
|
+
| HTML production | full fizz | `prerender` + abort → prelude + postponed | `resume` only the holes — O(paths to holes) |
|
|
160
|
+
| Shell store | schedules a bg capture | `putShell(key, …)` | `getShell(key)`; a stale/SWR hit also schedules a recapture |
|
|
161
|
+
| Prelude bytes | — | — | flushed FIRST, before segment resolution starts |
|
|
162
|
+
|
|
163
|
+
Middleware is not re-run during capture because it already ran for the
|
|
164
|
+
triggering request — the capture's derived context inherits the
|
|
165
|
+
post-middleware state (`ctx` variables included, which is what makes
|
|
166
|
+
middleware-derived shell content photograph correctly). Guarding is
|
|
167
|
+
serve-time: the commit point runs the full chain on EVERY serve.
|
|
168
|
+
|
|
169
|
+
Because handlers on uncached segments EXECUTE during capture, the
|
|
170
|
+
`cookies()`/`headers()` capture guard is load-bearing: those reads THROW during
|
|
171
|
+
a capture render (`assertNotInsideShellCapture`), so identity can never leak
|
|
172
|
+
into a shared shell through them. Loaders are exempt (always fresh).
|
|
173
|
+
|
|
174
|
+
## allReady: the SEO/bot story
|
|
175
|
+
|
|
176
|
+
`ssr: { resolveStreaming: ... }` returning `"allReady"` (e.g. for bot user
|
|
177
|
+
agents) bypasses PPR entirely — the request gets one complete, fully-buffered
|
|
178
|
+
axis-1 document. Crawlers that dislike streamed shells get a finished page;
|
|
179
|
+
regular users get the streamed shell. No configuration interaction: allReady
|
|
180
|
+
wins.
|
|
157
181
|
|
|
158
|
-
|
|
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
|
-
```
|
|
182
|
+
## Security
|
|
167
183
|
|
|
168
|
-
|
|
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
|
-
```
|
|
184
|
+
Shell caching shares one shell per host+URL across all users:
|
|
190
185
|
|
|
191
|
-
|
|
192
|
-
|
|
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
|
-
```
|
|
186
|
+
**(a) Access control is sound by construction.** The commit point is after ALL
|
|
187
|
+
middleware on every serve. A 401/redirect short-circuit returns before any
|
|
188
|
+
shell byte.
|
|
235
189
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
-
|
|
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.
|
|
190
|
+
**(b) Identity can't leak via cookies/headers.** `cookies()` and `headers()`
|
|
191
|
+
THROW during the background capture render. A shell that reads them is
|
|
192
|
+
PPR-ineligible by construction.
|
|
297
193
|
|
|
298
|
-
|
|
194
|
+
**(c) Residual hazard — middleware-derived per-user state.** A `ctx` variable
|
|
195
|
+
set by an upstream auth middleware and rendered by shell material is
|
|
196
|
+
photographed into the SHARED shell (the capture inherits post-middleware
|
|
197
|
+
state). That is scope fidelity working as designed — for shared values. If the
|
|
198
|
+
value is per-user: shell-cache only public/shared pages, put per-user content
|
|
199
|
+
in loaders, or key per variant at the CDN tier.
|
|
299
200
|
|
|
300
|
-
|
|
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`).
|
|
201
|
+
## What always stays on axis 1
|
|
321
202
|
|
|
322
|
-
|
|
203
|
+
Non-GET, RSC/partial/action/loader fetches, per-request CSP nonce,
|
|
204
|
+
`streamMode: "allReady"`, redirects, 404s, error renders, routes without `ppr`,
|
|
205
|
+
and any store without the shell family. A stored shell is invalidated when
|
|
206
|
+
`React.version` changes (postponed state is build-coupled), so deploys
|
|
207
|
+
self-heal via recapture.
|
|
323
208
|
|
|
324
|
-
|
|
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.
|
|
209
|
+
## Options: PartialPrerenderProps
|
|
356
210
|
|
|
357
|
-
|
|
211
|
+
```typescript
|
|
212
|
+
path("/products/:id", Page, { name: "product", ppr: true }, use);
|
|
213
|
+
path(
|
|
214
|
+
"/products/:id",
|
|
215
|
+
Page,
|
|
216
|
+
{ name: "product", ppr: { ttl: 600, swr: 120, tags: ["catalog"] } },
|
|
217
|
+
use,
|
|
218
|
+
);
|
|
219
|
+
```
|
|
358
220
|
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
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 |
|
|
221
|
+
| Field | Default | Notes |
|
|
222
|
+
| ------ | ------- | -------------------------------------------------------------------------------------------------- |
|
|
223
|
+
| `ttl` | `300` | shell freshness window in seconds (`ppr: true` uses the default) |
|
|
224
|
+
| `swr` | — | stale window: serve the stale shell + background recapture |
|
|
225
|
+
| `tags` | — | operational tags UNIONED with the tags the capture render auto-collects — see "Invalidation" below |
|
|
226
|
+
|
|
227
|
+
The shell store is always the app-level `createRouter({ cache })` store; the
|
|
228
|
+
default key is `${host}${pathname}${sortedSearch}:shell` (host-scoped so
|
|
229
|
+
multi-tenant shells never collide).
|
|
230
|
+
|
|
231
|
+
## Invalidation: tags vs revalidate()
|
|
232
|
+
|
|
233
|
+
`updateTag()`/`revalidateTag()` is the ONLY lever that changes the frozen shell
|
|
234
|
+
HTML; `revalidate()` is a DATA lever that never touches it.
|
|
235
|
+
|
|
236
|
+
A captured shell auto-carries the UNION of the non-loader tags recorded during
|
|
237
|
+
the capture render — every `cacheTag(...)` from a `"use cache"` function or
|
|
238
|
+
`cache()` segment that ran as shell material. Loader tags never attach (the
|
|
239
|
+
holes are already live). `ppr.tags` adds operational tags the render cannot
|
|
240
|
+
know (a tenant id, a deploy marker).
|
|
241
|
+
|
|
242
|
+
| Lever | Reaches the frozen shell? | Reaches the holes? |
|
|
243
|
+
| --------------------------------------------- | ------------------------------------------------------------- | --------------------------------------------------- |
|
|
244
|
+
| `updateTag` / `revalidateTag` on a SHELL tag | YES — drops the shell → MISS → recapture | n/a (holes are already live) |
|
|
245
|
+
| `updateTag` / `revalidateTag` on a LOADER tag | no — loader tags never attach to a shell | drops that loader's cached value (if it `cache()`s) |
|
|
246
|
+
| `revalidate()` (named revalidation contract) | **no** — re-runs segments/loaders for the PAYLOAD, never HTML | yes — the hole re-renders with fresh data |
|
|
401
247
|
|
|
402
248
|
## Pitfalls
|
|
403
249
|
|
|
404
250
|
- **Loader route without `loading()`**: eternal MISS plus a once-per-key
|
|
405
|
-
console warning
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
251
|
+
console warning (see "The structural negative").
|
|
252
|
+
- **Per-user value in shell material**: baked into the shared shell —
|
|
253
|
+
deterministically, not by race (handler promises deep-settle at the ring-3
|
|
254
|
+
write on cached chains; awaited/resolved values bake everywhere). Put
|
|
255
|
+
per-user data in a loader.
|
|
256
|
+
- **Theme on a HIT is capture-then-corrected**: the resume tree replays the
|
|
257
|
+
CAPTURE's `initialTheme` (resume requires it to match the frozen prelude);
|
|
258
|
+
the visitor's cookie theme is applied pre-paint by the FOUC script and
|
|
259
|
+
re-synced post-mount by ThemeProvider. Nothing to configure — but a themed
|
|
260
|
+
component in the shell may briefly render the captured theme's markup before
|
|
261
|
+
the post-mount re-sync.
|
|
262
|
+
- **Shell shows CAPTURE-time data for the shell's lifetime**: a `cache()`/`"use
|
|
263
|
+
cache"` value baked into the shell is PINNED at capture (the capture data
|
|
264
|
+
snapshot) and replayed on every HIT, so the shell stays byte-identical to the
|
|
265
|
+
frozen prelude even after that cache entry expires, gets recomputed, or is
|
|
266
|
+
tag-invalidated. This is deliberate — parity beats freshness inside the shell.
|
|
267
|
+
If a shell region needs to be fresh, put it under a `loading()` hole (holes are
|
|
268
|
+
never pinned) or make the SHELL itself invalidatable by adding the tag to
|
|
269
|
+
`ppr.tags`. Ring-1/ring-3 tag invalidation does NOT drop the shell.
|
|
270
|
+
- **Uncached nondeterminism in the shell is a hydration hazard**: a raw
|
|
271
|
+
`Date.now()` / `Math.random()` / uncached `fetch` rendered directly in shell
|
|
272
|
+
material (outside any cache ring) drifts between capture and hit and the
|
|
273
|
+
snapshot CANNOT pin it — it was never a cache read. It will mismatch the frozen
|
|
274
|
+
prelude and detonate hydration. Wrap it in `cache()`/`"use cache"` (then it is
|
|
275
|
+
pinned) or move it under a `loading()` hole.
|
|
276
|
+
- **Stacking with `/document-cache`**: pick one per route — the document cache
|
|
277
|
+
would cache the composite.
|
|
417
278
|
- **Dev + HMR**: works, but edits produce stale shells until TTL/recapture.
|
|
418
|
-
-
|
|
419
|
-
|
|
279
|
+
- **Dev cold-start cadence**: expect `MISS -> (in-place retry) -> HIT`. A
|
|
280
|
+
refused capture is negatively cached with an exponential window (1s doubling
|
|
281
|
+
to a 60s cap), so declaring `ppr` on an ineligible route never re-renders it
|
|
282
|
+
on every request.
|
|
283
|
+
- **HIT status is committed at the flush**: a failing hole cannot become a
|
|
284
|
+
500/redirect after the first shell byte — error UI renders inline via
|
|
285
|
+
Suspense/error boundaries (the same property any streamed SSR page has after
|
|
286
|
+
its shell flushes).
|
|
420
287
|
|
|
421
288
|
## Related
|
|
422
289
|
|