@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.
Files changed (45) hide show
  1. package/dist/bin/rango.js +27 -2
  2. package/dist/vite/index.js +147 -30
  3. package/package.json +1 -1
  4. package/skills/breadcrumbs/SKILL.md +1 -1
  5. package/skills/cache-guide/SKILL.md +1 -0
  6. package/skills/caching/SKILL.md +1 -1
  7. package/skills/migrate-nextjs/SKILL.md +15 -0
  8. package/skills/migrate-react-router/SKILL.md +15 -2
  9. package/skills/ppr/SKILL.md +426 -0
  10. package/skills/rango/SKILL.md +28 -25
  11. package/skills/route/SKILL.md +43 -0
  12. package/src/build/route-trie.ts +35 -7
  13. package/src/cache/cf/cf-cache-store.ts +155 -0
  14. package/src/cache/index.ts +6 -0
  15. package/src/cache/memory-segment-store.ts +57 -1
  16. package/src/cache/shell-cache.ts +386 -0
  17. package/src/cache/types.ts +58 -0
  18. package/src/cache/vercel/vercel-cache-store.ts +159 -5
  19. package/src/index.rsc.ts +5 -0
  20. package/src/index.ts +17 -0
  21. package/src/router/middleware.ts +14 -5
  22. package/src/router/parse-pattern.ts +115 -0
  23. package/src/router/pattern-matching.ts +53 -64
  24. package/src/router/segment-resolution/fresh.ts +12 -1
  25. package/src/router/segment-resolution/loader-cache.ts +14 -0
  26. package/src/router/segment-resolution/loader-mask.ts +44 -0
  27. package/src/router/substitute-pattern-params.ts +54 -35
  28. package/src/router/trie-matching.ts +19 -11
  29. package/src/router/url-params.ts +13 -0
  30. package/src/rsc/full-payload.ts +70 -0
  31. package/src/rsc/rsc-rendering.ts +105 -51
  32. package/src/rsc/shell-capture.ts +439 -0
  33. package/src/rsc/types.ts +26 -0
  34. package/src/server/cookie-store.ts +45 -0
  35. package/src/server/live.ts +130 -0
  36. package/src/server/request-context.ts +49 -0
  37. package/src/ssr/index.tsx +377 -180
  38. package/src/ssr/ssr-root.tsx +228 -0
  39. package/src/testing/render-route.tsx +7 -9
  40. package/src/types/route-config.ts +19 -7
  41. package/src/urls/type-extraction.ts +43 -18
  42. package/src/vite/discovery/discovery-errors.ts +61 -0
  43. package/src/vite/plugins/virtual-entries.ts +27 -2
  44. package/src/vite/router-discovery.ts +69 -15
  45. package/src/vite/utils/prerender-utils.ts +17 -4
@@ -0,0 +1,386 @@
1
+ /**
2
+ * PPR Shell Cache Middleware
3
+ *
4
+ * Axis 2 of the two-axis render model (see docs/design/ppr-shell-resume.md).
5
+ * Opt-in middleware that caches the rendered HTML *shell* (React's `prelude`
6
+ * plus the `postponed` state from a static `prerender` abort) and, on a later
7
+ * request, serves those bytes immediately and resumes fizz for just the live
8
+ * holes. The browser sees one ordinary streamed document.
9
+ *
10
+ * The middleware owns only the cheap URL/method gating and the stream
11
+ * composition. The RENDER layer (rsc-rendering, integrated by a later stage) is
12
+ * the final authority on whether a resume actually happens: it reads
13
+ * requestCtx._shellResume, calls the resume strategy, and marks the response
14
+ * with the internal `x-rango-shell-resumed` header ONLY when it truly resumed.
15
+ * This middleware composes the cached prelude in front of the live response ONLY
16
+ * when that marker is present; everything else (redirects, 404s, error renders,
17
+ * nonce/allReady bypasses) flows through untouched — every non-resumed path
18
+ * fails open to axis 1.
19
+ *
20
+ * Flow (the middleware calls next() EXACTLY ONCE on every path — the executor's
21
+ * per-entry next() is a single-use latch, so a second call throws):
22
+ * 1. Bypass matrix (non-GET, _rsc_* params, RSC request, skipPaths, isEnabled,
23
+ * store lacks the shell family) → plain next(), axis 1.
24
+ * 2. getShell(key) HIT + reactVersion matches → arm _shellResume, await next()
25
+ * once. On a stale (SWR) hit, ALSO set the _shellCapture descriptor before
26
+ * that same next() so the render layer schedules a background recapture.
27
+ * - marker present → strip it, prepend prelude bytes, x-rango-shell: HIT.
28
+ * - marker absent → render layer did not resume; return untouched.
29
+ * 3. MISS (or reactVersion mismatch) → set the _shellCapture descriptor before
30
+ * the single next(), stream the live response to the user with
31
+ * x-rango-shell: MISS. The render layer reads the descriptor after building the
32
+ * response and schedules a BACKGROUND capture (via router.match under a derived
33
+ * context — NOT a second next()); see rsc-rendering.ts + shell-capture.ts.
34
+ * The descriptor is cleared in a finally so it never leaks into a reused ctx.
35
+ */
36
+
37
+ import React from "react";
38
+ import type { MiddlewareFn, MiddlewareContext } from "../router/middleware.js";
39
+ import {
40
+ getRequestContext,
41
+ type RequestContext,
42
+ } from "../server/request-context.js";
43
+ import { mayNeedSSR } from "../rsc/ssr-setup.js";
44
+ import type { SegmentCacheStore } from "./types.js";
45
+ import { sortedSearchString } from "./cache-key-utils.js";
46
+ import { reportCacheError } from "./cache-error.js";
47
+
48
+ /** Debug/status header the browser (and e2e assertions) can read: HIT | MISS. */
49
+ const SHELL_STATUS_HEADER = "x-rango-shell";
50
+
51
+ /**
52
+ * Internal marker the render layer sets on the live response when — and only
53
+ * when — it actually resumed a cached shell. The middleware composes the prelude
54
+ * in front of the body iff this header is present, then strips it before the
55
+ * response leaves. It is the whole handshake between the middleware (which arms
56
+ * _shellResume optimistically) and the render layer (the final authority).
57
+ */
58
+ const SHELL_RESUMED_MARKER_HEADER = "x-rango-shell-resumed";
59
+
60
+ /**
61
+ * React version captured at prerender time is the invalidation gate: a stored
62
+ * shell whose reactVersion differs from the running React cannot be resumed (the
63
+ * postponed blob is build-coupled), so it is treated as a miss. Read once at
64
+ * module load — React.version is stable for the process lifetime.
65
+ */
66
+ const REACT_VERSION = React.version;
67
+
68
+ /** Decode a base64 prelude back into bytes for stream composition. */
69
+ function base64ToBytes(b64: string): Uint8Array {
70
+ const binary = atob(b64);
71
+ const bytes = new Uint8Array(binary.length);
72
+ for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
73
+ return bytes;
74
+ }
75
+
76
+ /**
77
+ * Compose the served response: prelude bytes first, then the live (resumed) body.
78
+ * React relies on HTML-parser foster-parenting for content streamed after the
79
+ * prelude's closing `</body></html>`, so plain byte concatenation is the correct
80
+ * composition (POC item 6) — do not try to reopen or splice the document.
81
+ *
82
+ * Status and headers come from the LIVE next() response: Set-Cookie and friends
83
+ * are per-request and belong to the live pass, not the frozen shell. The internal
84
+ * marker is stripped and x-rango-shell: HIT is added.
85
+ */
86
+ function composeShellResponse(
87
+ response: Response,
88
+ preludeBase64: string,
89
+ ): Response {
90
+ const preludeBytes = base64ToBytes(preludeBase64);
91
+ const body = response.body;
92
+ const composed = new ReadableStream<Uint8Array>({
93
+ async start(controller) {
94
+ controller.enqueue(preludeBytes);
95
+ if (body) {
96
+ const reader = body.getReader();
97
+ try {
98
+ for (;;) {
99
+ const { done, value } = await reader.read();
100
+ if (done) break;
101
+ controller.enqueue(value);
102
+ }
103
+ } finally {
104
+ reader.releaseLock();
105
+ }
106
+ }
107
+ controller.close();
108
+ },
109
+ cancel(reason) {
110
+ // Propagate downstream cancellation to the live body so it does not leak.
111
+ return body?.cancel(reason);
112
+ },
113
+ });
114
+
115
+ const headers = new Headers(response.headers);
116
+ headers.delete(SHELL_RESUMED_MARKER_HEADER);
117
+ headers.set(SHELL_STATUS_HEADER, "HIT");
118
+ return new Response(composed, {
119
+ status: response.status,
120
+ statusText: response.statusText,
121
+ headers,
122
+ });
123
+ }
124
+
125
+ /** Clone a response with the x-rango-shell status header added. */
126
+ function withShellStatus(response: Response, status: "HIT" | "MISS"): Response {
127
+ const headers = new Headers(response.headers);
128
+ headers.set(SHELL_STATUS_HEADER, status);
129
+ return new Response(response.body, {
130
+ status: response.status,
131
+ statusText: response.statusText,
132
+ headers,
133
+ });
134
+ }
135
+
136
+ /**
137
+ * Options for the PPR shell-cache middleware.
138
+ */
139
+ export interface ShellCacheOptions<TEnv = any> {
140
+ /**
141
+ * Cache store to use. Defaults to the request context's `_cacheStore`
142
+ * (the app-level store wired via the router's cache config).
143
+ */
144
+ store?: SegmentCacheStore<TEnv>;
145
+
146
+ /**
147
+ * Shell time-to-live in seconds. Defaults to 300.
148
+ */
149
+ ttlSeconds?: number;
150
+
151
+ /**
152
+ * Stale-while-revalidate window in seconds. On a stale hit the cached shell is
153
+ * still served and a background recapture is scheduled.
154
+ */
155
+ swrSeconds?: number;
156
+
157
+ /**
158
+ * Custom cache key generator. Receives the cleaned request URL. The middleware
159
+ * appends its own `:shell` namespace suffix, so the returned key never collides
160
+ * with a document-cache key.
161
+ *
162
+ * A custom generator owns the FULL key identity, including host scoping: the
163
+ * default key incorporates `url.host` so shells can never leak across tenants
164
+ * in multi-host deployments — include it in custom keys too unless the store
165
+ * is provably single-host.
166
+ */
167
+ keyGenerator?: (url: URL) => string;
168
+
169
+ /**
170
+ * Callback to decide whether shell caching is enabled for this request.
171
+ * Return false to fall through to a normal HTML render (axis 1).
172
+ */
173
+ isEnabled?: (ctx: MiddlewareContext<TEnv>) => boolean | Promise<boolean>;
174
+
175
+ /**
176
+ * Skip shell caching for specific path prefixes (e.g. admin, API routes).
177
+ */
178
+ skipPaths?: string[];
179
+
180
+ /**
181
+ * Enable debug logging for shell cache operations (HIT / MISS / CAPTURED).
182
+ * Defaults to false.
183
+ */
184
+ debug?: boolean;
185
+ }
186
+
187
+ /**
188
+ * Create the PPR shell-cache middleware.
189
+ *
190
+ * Add it to a router (or a route subtree) to cache the HTML shell and resume
191
+ * fizz for just the live holes on subsequent requests. Personalization must live
192
+ * in loaders/holes — the shell is shared per URL key (the shell-manifest
193
+ * pattern). Actions, progressive enhancement, formState, and per-request nonce
194
+ * always take axis 1.
195
+ *
196
+ * @example
197
+ * ```typescript
198
+ * const router = createRouter<AppEnv>()
199
+ * .use(createShellCacheMiddleware({ ttlSeconds: 600, swrSeconds: 60 }))
200
+ * .route("home", (ctx) => <HomePage />);
201
+ * ```
202
+ */
203
+ export function createShellCacheMiddleware<TEnv = any>(
204
+ options: ShellCacheOptions<TEnv> = {},
205
+ ): MiddlewareFn<TEnv> {
206
+ const {
207
+ ttlSeconds = 300,
208
+ swrSeconds,
209
+ keyGenerator,
210
+ isEnabled,
211
+ skipPaths = [],
212
+ debug = false,
213
+ } = options;
214
+
215
+ const log = debug ? (message: string) => console.log(message) : () => {};
216
+
217
+ return async function shellCacheMiddleware(
218
+ ctx: MiddlewareContext<TEnv>,
219
+ next: () => Promise<Response>,
220
+ ): Promise<Response> {
221
+ const url = ctx.url;
222
+ // ctx.url is stripped of _rsc_* params by the pipeline (stripInternalParams);
223
+ // read the raw request URL for internal-param detection, like document-cache.
224
+ const rawUrl = new URL(ctx.request.url);
225
+
226
+ // --- Bypass matrix (each bypass = plain next(), axis 1) ---
227
+
228
+ // Mutations are dynamic — never resume/capture a shell for them.
229
+ if (ctx.request.method !== "GET") return next();
230
+ // RSC action / loader / partial requests are not HTML document requests.
231
+ if (rawUrl.searchParams.has("_rsc_action")) return next();
232
+ if (rawUrl.searchParams.has("_rsc_loader")) return next();
233
+ if (rawUrl.searchParams.has("_rsc_partial")) return next();
234
+ // RSC (Flight) request — the Flight path is untouched by PPR.
235
+ if (!mayNeedSSR(ctx.request, rawUrl)) return next();
236
+ // Consumer opt-outs.
237
+ if (skipPaths.some((path) => url.pathname.startsWith(path))) return next();
238
+ if (isEnabled) {
239
+ const enabled = await isEnabled(ctx);
240
+ if (!enabled) return next();
241
+ }
242
+
243
+ const requestCtx = getRequestContext();
244
+ const store = options.store ?? requestCtx?._cacheStore;
245
+
246
+ // Store must implement the shell family — otherwise fail open to axis 1.
247
+ if (!store?.getShell || !store?.putShell) return next();
248
+
249
+ // Track whether next() has been called so the catch block knows whether it is
250
+ // safe to fall through to the handler (mirrors document-cache). cacheKey is
251
+ // assigned inside the try (a throwing keyGenerator must degrade, not 500).
252
+ let handlerCalled = false;
253
+ let cacheKey = "";
254
+
255
+ /**
256
+ * Build the "capture wanted" descriptor. Set on the request context BEFORE
257
+ * the single next() so the render layer can read it after building the
258
+ * response and schedule the background capture (via router.match under a
259
+ * derived context — NOT a second next()). `store` is the SAME store this
260
+ * middleware resolved for getShell, so a store-attached middleware writes
261
+ * captures where it reads them. `tags` is intentionally omitted: the capture
262
+ * collects the shell's own non-loader tags from its derived render.
263
+ */
264
+ const captureDescriptor = (): NonNullable<
265
+ RequestContext["_shellCapture"]
266
+ > => ({
267
+ key: cacheKey,
268
+ ttl: ttlSeconds,
269
+ swr: swrSeconds,
270
+ store,
271
+ });
272
+
273
+ try {
274
+ // Namespace the key with a `:shell` suffix (mirrors document-cache's
275
+ // `:html`/`:rsc` suffix) so it can never collide with a document-cache key;
276
+ // the store further isolates the shell family internally. Built inside the
277
+ // try so a throwing keyGenerator degrades to a full render, not a 500.
278
+ //
279
+ // The default key includes the request HOST: in a multi-tenant host-router
280
+ // deployment (one worker, one shared KV/runtime-cache store) a host-less
281
+ // key would serve tenant A's captured shell to tenant B's users. The CF
282
+ // document family fixed this exact class at the store tier (toDocKVHost);
283
+ // the shell family fixes it at the key tier so every store is safe.
284
+ let searchSuffix = "";
285
+ if (!keyGenerator) {
286
+ const sorted = sortedSearchString(url.searchParams);
287
+ if (sorted) searchSuffix = `?${sorted}`;
288
+ }
289
+ cacheKey = keyGenerator
290
+ ? `${keyGenerator(url)}:shell`
291
+ : `${url.host}${url.pathname}${searchSuffix}:shell`;
292
+
293
+ const cached = await store.getShell(cacheKey);
294
+ const validHit =
295
+ cached != null && cached.entry.reactVersion === REACT_VERSION;
296
+
297
+ if (cached && !validHit) {
298
+ // reactVersion mismatch: the postponed blob is build-coupled and cannot
299
+ // be resumed by the running React. There is no deleteShell primitive in
300
+ // v1, so we simply treat it as a MISS — the recapture below overwrites
301
+ // the same key, and the entry otherwise ages out via TTL.
302
+ log(
303
+ `[ShellCache] MISS ${url.pathname} (reactVersion ${cached.entry.reactVersion} != ${REACT_VERSION})`,
304
+ );
305
+ }
306
+
307
+ if (validHit) {
308
+ // Arm the resume optimistically. The render layer is the final authority
309
+ // (nonce/formState/allReady bypass); it engages resume and marks the
310
+ // response only when it actually resumed.
311
+ if (requestCtx) {
312
+ requestCtx._shellResume = { postponed: cached!.entry.postponed };
313
+ // SWR: on a stale hit, also request a background recapture by setting
314
+ // the descriptor before this single next(). Resume (foreground) and
315
+ // capture-request (background) legitimately coexist now — the render
316
+ // layer schedules the recapture off the descriptor after building the
317
+ // resumed response. A fresh hit leaves the descriptor unset.
318
+ if (cached!.shouldRevalidate) {
319
+ requestCtx._shellCapture = captureDescriptor();
320
+ }
321
+ }
322
+ handlerCalled = true;
323
+ let response: Response;
324
+ try {
325
+ response = await next();
326
+ } finally {
327
+ // Always disarm both single-request flags. A next() throw here (resume
328
+ // failure) propagates to the outer catch and rethrows; the
329
+ // version-keyed entry self-heals via axis 1 + recapture on the next
330
+ // request (v1 has no deleteShell to eagerly remove it).
331
+ if (requestCtx) {
332
+ requestCtx._shellResume = undefined;
333
+ requestCtx._shellCapture = undefined;
334
+ }
335
+ }
336
+
337
+ if (response.headers.has(SHELL_RESUMED_MARKER_HEADER)) {
338
+ log(`[ShellCache] HIT ${url.pathname}`);
339
+ return composeShellResponse(response, cached!.entry.prelude);
340
+ }
341
+
342
+ // Marker absent: the render layer did NOT resume (redirect, 404, error
343
+ // render, per-request nonce, or allReady buffering). Fail open to axis 1
344
+ // — return the live response untouched. Note: no manual onResponse-
345
+ // callback drain is needed here (or on any path in this middleware),
346
+ // because every path runs a full next() pipeline pass, which already
347
+ // drains those callbacks — unlike document-cache, which serves a fully
348
+ // cached response bypassing next() and must drain them itself.
349
+ log(`[ShellCache] PASS ${url.pathname} (resume not engaged)`);
350
+ return response;
351
+ }
352
+
353
+ // --- MISS (no entry, or reactVersion mismatch) ---
354
+ // Set the "capture wanted" descriptor before the single next(). The render
355
+ // layer reads it after building the response and, if the response is a
356
+ // servable 200 HTML document, schedules a BACKGROUND capture (router.match
357
+ // under a derived context — never a second next()). The descriptor's mere
358
+ // presence does not change the foreground render (loader masking keys off
359
+ // _shellCaptureRun, which only the background derived context sets).
360
+ if (requestCtx) requestCtx._shellCapture = captureDescriptor();
361
+ handlerCalled = true;
362
+ let response: Response;
363
+ try {
364
+ response = await next();
365
+ } finally {
366
+ // Clear the descriptor so it never leaks into a reused ctx. The render
367
+ // layer already read it (synchronously, inside next()) and captured its
368
+ // own reference for the background task, so clearing here is safe.
369
+ if (requestCtx) requestCtx._shellCapture = undefined;
370
+ }
371
+
372
+ log(`[ShellCache] MISS ${url.pathname}`);
373
+ return withShellStatus(response, "MISS");
374
+ } catch (error) {
375
+ reportCacheError(error, "cache-read", "[ShellCache] middleware");
376
+ if (handlerCalled) {
377
+ // Post-handler failure (resume/render throw, or a stream error): do not
378
+ // call next() again — that would re-run handler side effects.
379
+ throw error;
380
+ }
381
+ // Pre-handler failure (cache lookup / key generation): degrade to a full
382
+ // render.
383
+ return next();
384
+ }
385
+ };
386
+ }
@@ -136,6 +136,39 @@ export interface SegmentCacheStore<TEnv = unknown> {
136
136
  tags?: string[],
137
137
  ): Promise<void>;
138
138
 
139
+ /**
140
+ * Get a cached PPR shell entry by key.
141
+ * Returns the stored prelude/postponed pair (see ShellCacheEntry) and whether
142
+ * it should be revalidated (SWR). Used by the shell-cache middleware to serve
143
+ * a cached HTML shell and resume fizz for just the live holes.
144
+ *
145
+ * Optional: a store that does not implement the shell family disables the
146
+ * shell-cache middleware (it fails open to the normal HTML render path).
147
+ */
148
+ getShell?(
149
+ key: string,
150
+ ): Promise<{ entry: ShellCacheEntry; shouldRevalidate?: boolean } | null>;
151
+
152
+ /**
153
+ * Store a PPR shell entry with TTL and optional SWR window.
154
+ * The prelude bytes and postponed state are version- and generation-coupled
155
+ * and travel together in a single entry (they must never mix across a React
156
+ * upgrade — the reactVersion field on the entry gates that at read time).
157
+ * @param key - Cache key
158
+ * @param entry - The shell prelude/postponed/version/createdAt bundle
159
+ * @param ttlSeconds - Time-to-live in seconds
160
+ * @param swrSeconds - Optional stale-while-revalidate window in seconds
161
+ * @param tags - Optional cache tags for invalidation (participates in
162
+ * invalidateTags via the same tag machinery as the item family)
163
+ */
164
+ putShell?(
165
+ key: string,
166
+ entry: ShellCacheEntry,
167
+ ttlSeconds?: number,
168
+ swrSeconds?: number,
169
+ tags?: string[],
170
+ ): Promise<void>;
171
+
139
172
  /**
140
173
  * Get a cached function result by key.
141
174
  * Returns the serialized value, optional handle data, and staleness flag.
@@ -186,6 +219,31 @@ export interface CacheItemResult {
186
219
  tags?: string[];
187
220
  }
188
221
 
222
+ /**
223
+ * A cached PPR (Partial Pre-rendering) shell entry.
224
+ *
225
+ * One entry carries BOTH artifacts a resume needs — the rendered HTML prelude
226
+ * and React's postponed state — because the pair is version- and
227
+ * generation-coupled and must never be mixed across a React upgrade or a build
228
+ * change. The reactVersion field is the read-time gate that enforces that: the
229
+ * shell-cache middleware treats an entry whose reactVersion differs from the
230
+ * running React as a miss (the postponed blob is build-coupled and cannot be
231
+ * resumed by a different React).
232
+ */
233
+ export interface ShellCacheEntry {
234
+ /** Rendered HTML prelude bytes, base64-encoded (stores are JSON-serializing). */
235
+ prelude: string;
236
+ /**
237
+ * JSON.stringify of React's postponed state, or null when the shell settled
238
+ * with no holes (the DATA variant — served without a fizz resume).
239
+ */
240
+ postponed: string | null;
241
+ /** React.version captured at prerender time; the read-time invalidation gate. */
242
+ reactVersion: string;
243
+ /** Epoch ms when the shell was captured. */
244
+ createdAt: number;
245
+ }
246
+
189
247
  /**
190
248
  * Options for setItem() for function-level caching ("use cache").
191
249
  */
@@ -38,6 +38,7 @@ import type {
38
38
  CacheGetResult,
39
39
  CacheItemResult,
40
40
  CacheItemOptions,
41
+ ShellCacheEntry,
41
42
  } from "../types.js";
42
43
  import type { RequestContext } from "../../server/request-context.js";
43
44
  import { isPerClientSignalHeader } from "../../browser/cookie-name.js";
@@ -113,10 +114,10 @@ const VERCEL_UNSAFE_TAG_CHARS = /[,&#%?]/;
113
114
  */
114
115
  const REVALIDATION_LOCK_MS = 30_000;
115
116
 
116
- /** Family prefixes that keep the three value tiers from colliding in the single
117
- * Vercel keyspace. The router's own semantic prefixes (doc:/partial:/use-cache:)
118
- * become the suffix; `rg:` namespaces every Rango entry. */
119
- type CacheFamily = "s" | "i" | "r";
117
+ /** Family prefixes that keep the value tiers from colliding in the single Vercel
118
+ * keyspace. The router's own semantic prefixes (doc:/partial:/use-cache:) become
119
+ * the suffix; `rg:` namespaces every Rango entry. `h` is the PPR shell tier. */
120
+ type CacheFamily = "s" | "i" | "r" | "h";
120
121
 
121
122
  /** Stored envelope for a segment-tree entry (get/set). */
122
123
  interface VercelSegmentEnvelope {
@@ -158,6 +159,24 @@ interface VercelResponseEnvelope {
158
159
  t?: string[];
159
160
  }
160
161
 
162
+ /** Stored envelope for a PPR shell entry (getShell/putShell). */
163
+ interface VercelShellEnvelope {
164
+ /** base64-encoded prelude bytes. */
165
+ p: string;
166
+ /** postponed state JSON, or null (DATA variant). */
167
+ po: string | null;
168
+ /** React.version at capture. */
169
+ rv: string;
170
+ /** createdAt (ms since epoch). */
171
+ c: number;
172
+ /** staleAt (ms since epoch). */
173
+ s: number;
174
+ /** expiresAt (ms since epoch). */
175
+ e: number;
176
+ /** Tags, preserved so a stale re-stamp keeps them. */
177
+ t?: string[];
178
+ }
179
+
161
180
  /** Read-path outcome for the debug sink. */
162
181
  export type VercelCacheReadOutcome =
163
182
  | "miss"
@@ -169,7 +188,7 @@ export type VercelCacheReadOutcome =
169
188
 
170
189
  /** Diagnostic event emitted on every read when `debug` is set. */
171
190
  export interface VercelCacheReadDebugEvent {
172
- op: "get" | "getItem" | "getResponse";
191
+ op: "get" | "getItem" | "getResponse" | "getShell";
173
192
  key: string;
174
193
  outcome: VercelCacheReadOutcome;
175
194
  staleAt?: number;
@@ -707,6 +726,123 @@ export class VercelCacheStore<
707
726
  }
708
727
  }
709
728
 
729
+ // --- Shell family (PPR shell resume - getShell/putShell) ---
730
+
731
+ async getShell(
732
+ key: string,
733
+ ): Promise<{ entry: ShellCacheEntry; shouldRevalidate?: boolean } | null> {
734
+ const storeKey = this.toStoreKey(key, "h");
735
+ const started = Date.now();
736
+ let raw: unknown;
737
+ try {
738
+ raw = await this.cache.get(storeKey);
739
+ } catch (error) {
740
+ reportCacheError(error, "cache-read", "[VercelCacheStore] getShell");
741
+ this.emitDebug({ op: "getShell", key, outcome: "error" });
742
+ return null;
743
+ }
744
+ const readMs = Date.now() - started;
745
+
746
+ if (raw == null) {
747
+ this.emitDebug({ op: "getShell", key, outcome: "miss", readMs });
748
+ return null;
749
+ }
750
+ const env = this.asShellEnvelope(raw);
751
+ if (!env) {
752
+ reportCacheError(
753
+ new Error("malformed shell envelope"),
754
+ "cache-corrupt",
755
+ "[VercelCacheStore] getShell",
756
+ );
757
+ void this.safeDelete(storeKey);
758
+ this.emitDebug({ op: "getShell", key, outcome: "corrupt", readMs });
759
+ return null;
760
+ }
761
+
762
+ const now = Date.now();
763
+ if (now > env.e) {
764
+ void this.safeDelete(storeKey);
765
+ this.emitDebug({
766
+ op: "getShell",
767
+ key,
768
+ outcome: "expired",
769
+ staleAt: env.s,
770
+ expiresAt: env.e,
771
+ readMs,
772
+ });
773
+ return null;
774
+ }
775
+
776
+ const isStale = env.s > 0 && now > env.s;
777
+ if (isStale) {
778
+ this.markRevalidating(
779
+ storeKey,
780
+ env,
781
+ env.t,
782
+ "[VercelCacheStore] getShell",
783
+ );
784
+ }
785
+ this.emitDebug({
786
+ op: "getShell",
787
+ key,
788
+ outcome: isStale ? "stale-revalidate" : "fresh",
789
+ shouldRevalidate: isStale,
790
+ staleAt: env.s,
791
+ expiresAt: env.e,
792
+ readMs,
793
+ });
794
+ return {
795
+ entry: {
796
+ prelude: env.p,
797
+ postponed: env.po,
798
+ reactVersion: env.rv,
799
+ createdAt: env.c,
800
+ },
801
+ shouldRevalidate: isStale,
802
+ };
803
+ }
804
+
805
+ async putShell(
806
+ key: string,
807
+ entry: ShellCacheEntry,
808
+ ttlSeconds?: number,
809
+ swrSeconds?: number,
810
+ tags?: string[],
811
+ ): Promise<void> {
812
+ try {
813
+ const ttl = resolveTtl(ttlSeconds, this.defaults, DEFAULT_FUNCTION_TTL);
814
+ const swrWindow = resolveSwrWindow(swrSeconds, this.defaults);
815
+ const { staleAt, expiresAt } = computeExpiration(ttl, swrWindow);
816
+ // Store the CLAMPED tags (see putResponse/setItem) so dropped tags don't
817
+ // resurface on a hit; write() re-clamps idempotently.
818
+ const safeTags = this.clampTagsForWrite(
819
+ tags,
820
+ "[VercelCacheStore] putShell",
821
+ );
822
+ const env: VercelShellEnvelope = {
823
+ p: entry.prelude,
824
+ po: entry.postponed,
825
+ rv: entry.reactVersion,
826
+ c: entry.createdAt,
827
+ s: staleAt,
828
+ e: expiresAt,
829
+ t: safeTags.length > 0 ? safeTags : undefined,
830
+ };
831
+ // write() enforces the 2 MB per-item ceiling (withinSizeLimit): an
832
+ // oversized shell prelude is reported and skipped (fail-open to a full
833
+ // render), never silently no-op'd on the platform.
834
+ await this.write(
835
+ this.toStoreKey(key, "h"),
836
+ env,
837
+ ttl + swrWindow,
838
+ safeTags,
839
+ "[VercelCacheStore] putShell",
840
+ );
841
+ } catch (error) {
842
+ reportCacheError(error, "cache-write", "[VercelCacheStore] putShell");
843
+ }
844
+ }
845
+
710
846
  // --- Tags ---
711
847
 
712
848
  async invalidateTags(tags: string[]): Promise<void> {
@@ -930,6 +1066,24 @@ export class VercelCacheStore<
930
1066
  };
931
1067
  }
932
1068
 
1069
+ private asShellEnvelope(raw: unknown): VercelShellEnvelope | null {
1070
+ if (!isRecord(raw)) return null;
1071
+ const { p, po, rv, c, s, e, t } = raw;
1072
+ if (typeof p !== "string" || typeof rv !== "string") return null;
1073
+ if (po !== null && typeof po !== "string") return null;
1074
+ if (typeof c !== "number") return null;
1075
+ if (typeof s !== "number" || typeof e !== "number") return null;
1076
+ return {
1077
+ p,
1078
+ po: po as string | null,
1079
+ rv,
1080
+ c,
1081
+ s,
1082
+ e,
1083
+ t: Array.isArray(t) ? (t as string[]) : undefined,
1084
+ };
1085
+ }
1086
+
933
1087
  private asResponseEnvelope(raw: unknown): VercelResponseEnvelope | null {
934
1088
  if (!isRecord(raw)) return null;
935
1089
  const { b, st, hd, s, e, t } = raw;
package/src/index.rsc.ts CHANGED
@@ -219,6 +219,11 @@ export {
219
219
  type ReadonlyHeaders,
220
220
  } from "./server/cookie-store.js";
221
221
 
222
+ // live() — the deterministic PPR hole primitive. Real (capture-aware)
223
+ // implementation under the react-server condition; index.ts ships a client/SSR
224
+ // passthrough (there is no shell capture off this condition).
225
+ export { live } from "./server/live.js";
226
+
222
227
  // Cache tag APIs (server-only)
223
228
  // cacheTag: tag the current "use cache" entry at runtime.
224
229
  // updateTag: read-your-own-writes invalidation (awaitable, for Server Actions).