@rangojs/router 0.0.0-experimental.147 → 0.0.0-experimental.148
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 +19 -3
- package/package.json +1 -1
- package/skills/mime-routes/SKILL.md +25 -17
- package/skills/ppr/SKILL.md +20 -10
- package/src/cache/cf/cf-cache-store.ts +37 -2
- package/src/index.rsc.ts +6 -0
- package/src/prerender/build-shell-capture.ts +17 -1
- package/src/router/content-negotiation.ts +47 -5
- package/src/router/metrics.ts +17 -2
- package/src/router/router-interfaces.ts +7 -0
- package/src/router/router-options.ts +13 -0
- package/src/router.ts +5 -0
- package/src/rsc/handler.ts +4 -2
- package/src/rsc/rsc-rendering.ts +49 -0
- package/src/rsc/shell-build-manifest.ts +40 -10
- package/src/rsc/shell-capture.ts +386 -24
- package/src/rsc/shell-serve.ts +44 -0
- package/src/rsc/ssr-setup.ts +54 -22
- package/src/server/context.ts +1 -0
- package/src/urls/pattern-types.ts +27 -0
- package/src/vite/discovery/shell-prerender-phase.ts +2 -0
- package/src/vite/discovery/state.ts +3 -1
- package/src/vite/router-discovery.ts +20 -2
|
@@ -27,7 +27,12 @@
|
|
|
27
27
|
|
|
28
28
|
import type { SegmentCacheStore, ShellCacheEntry } from "../cache/types.js";
|
|
29
29
|
import { sortedSearchString } from "../cache/cache-key-utils.js";
|
|
30
|
-
import {
|
|
30
|
+
import {
|
|
31
|
+
DEV_SHELL_PROBE_TIMEOUT_MS,
|
|
32
|
+
hasIntactShellPayload,
|
|
33
|
+
isValidShellHit,
|
|
34
|
+
} from "./shell-serve.js";
|
|
35
|
+
import { SHELL_CAPTURE_MAX_WAIT_MS } from "./shell-capture.js";
|
|
31
36
|
import { buildShellManifestKey } from "../prerender/shell-manifest-key.js";
|
|
32
37
|
|
|
33
38
|
/** One baked manifest record (the __ps asset module's default export). */
|
|
@@ -133,18 +138,37 @@ export interface DevShellLookup {
|
|
|
133
138
|
ttl: number;
|
|
134
139
|
swr?: number;
|
|
135
140
|
tags?: string[];
|
|
141
|
+
maxSnapshotBytes?: number;
|
|
142
|
+
/** Resolved `ppr.captureTimeout` (ms) — the endpoint's capture honors it. */
|
|
143
|
+
captureTimeout?: number;
|
|
136
144
|
}
|
|
137
145
|
|
|
146
|
+
/** Retry delay (~400ms) plus quiesce/fizz/store headroom past the budgets. */
|
|
147
|
+
const DEV_SHELL_RETRY_MARGIN_MS = 5_000;
|
|
148
|
+
|
|
138
149
|
/**
|
|
139
|
-
*
|
|
140
|
-
* waitUntil an unsettled fetch pends forever instead of rejecting; on
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
* capture
|
|
144
|
-
*
|
|
145
|
-
*
|
|
150
|
+
* Timed out like the dev prerender store fetch (see #697): inside a workerd
|
|
151
|
+
* waitUntil an unsettled fetch pends forever instead of rejecting; on timeout
|
|
152
|
+
* this degrades to a MISS and the runtime capture path takes over. But this
|
|
153
|
+
* fetch blocks a foreground document request and its response IS an inline
|
|
154
|
+
* capture, so the bound must cover the endpoint's FULL sequential worst case
|
|
155
|
+
* — aborting a still-healthy capture turns it into a spurious MISS. The terms
|
|
156
|
+
* of the server envelope (dev /__rsc_shell, vite/router-discovery.ts):
|
|
157
|
+
* - DEV_SHELL_PROBE_TIMEOUT_MS: the sequential /__rsc_prerender pre-flight
|
|
158
|
+
* probe (same constant on the endpoint side).
|
|
159
|
+
* - 2x the capture settle budget (`ppr.captureTimeout`, default
|
|
160
|
+
* SHELL_CAPTURE_MAX_WAIT_MS): first attempt + one in-place cold-graph retry.
|
|
161
|
+
* - DEV_SHELL_RETRY_MARGIN_MS: the ~400ms retry delay plus headroom.
|
|
162
|
+
* Deeper fix (possible follow-up): the endpoint owns ONE total deadline and
|
|
163
|
+
* this bound becomes a plain liveness backstop instead of envelope math.
|
|
146
164
|
*/
|
|
147
|
-
|
|
165
|
+
function devShellFetchTimeoutMs(captureTimeout: number | undefined): number {
|
|
166
|
+
return (
|
|
167
|
+
DEV_SHELL_PROBE_TIMEOUT_MS +
|
|
168
|
+
2 * (captureTimeout ?? SHELL_CAPTURE_MAX_WAIT_MS) +
|
|
169
|
+
DEV_SHELL_RETRY_MARGIN_MS
|
|
170
|
+
);
|
|
171
|
+
}
|
|
148
172
|
|
|
149
173
|
async function fetchDevShellEntry(
|
|
150
174
|
pathname: string,
|
|
@@ -162,9 +186,15 @@ async function fetchDevShellEntry(
|
|
|
162
186
|
});
|
|
163
187
|
if (dev.swr !== undefined) params.set("swr", String(dev.swr));
|
|
164
188
|
if (dev.tags && dev.tags.length > 0) params.set("tags", dev.tags.join(","));
|
|
189
|
+
if (dev.maxSnapshotBytes !== undefined) {
|
|
190
|
+
params.set("maxSnapshotBytes", String(dev.maxSnapshotBytes));
|
|
191
|
+
}
|
|
192
|
+
if (dev.captureTimeout !== undefined) {
|
|
193
|
+
params.set("captureTimeout", String(dev.captureTimeout));
|
|
194
|
+
}
|
|
165
195
|
try {
|
|
166
196
|
const res = await fetch(`${devUrl}/__rsc_shell?${params}`, {
|
|
167
|
-
signal: AbortSignal.timeout(
|
|
197
|
+
signal: AbortSignal.timeout(devShellFetchTimeoutMs(dev.captureTimeout)),
|
|
168
198
|
});
|
|
169
199
|
if (!res.ok) return undefined;
|
|
170
200
|
return (await res.json()) as BuildShellEntry;
|
package/src/rsc/shell-capture.ts
CHANGED
|
@@ -21,6 +21,7 @@ import { bufferToBase64 } from "../cache/cf/cf-base64.js";
|
|
|
21
21
|
import { reportCacheError } from "../cache/cache-error.js";
|
|
22
22
|
import { runBackground } from "../cache/background-task.js";
|
|
23
23
|
import { enqueueSerializedCapture } from "./capture-queue.js";
|
|
24
|
+
import { INTERNAL_RANGO_DEBUG } from "../internal-debug.js";
|
|
24
25
|
import { observePhase, PHASES } from "../router/instrument.js";
|
|
25
26
|
import {
|
|
26
27
|
runWithRequestContext,
|
|
@@ -78,8 +79,12 @@ import { resolveDeferredHandleValues } from "../handles/deferred-resolution.js";
|
|
|
78
79
|
*/
|
|
79
80
|
const FLIGHT_QUIET_HOPS = 2;
|
|
80
81
|
|
|
81
|
-
/**
|
|
82
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Default upper bound on the capture prerender wait before forcing the abort.
|
|
84
|
+
* Single owner of the default budget — shell-build-manifest.ts imports it so
|
|
85
|
+
* the dev fetch bound's envelope math cannot drift from the capture.
|
|
86
|
+
*/
|
|
87
|
+
export const SHELL_CAPTURE_MAX_WAIT_MS = 5000;
|
|
83
88
|
|
|
84
89
|
/**
|
|
85
90
|
* Upper bound on waiting for the capture's DEFERRED cache writes to settle before
|
|
@@ -369,6 +374,254 @@ function warnUntaggedShellBakeOnce(key: string): void {
|
|
|
369
374
|
);
|
|
370
375
|
}
|
|
371
376
|
|
|
377
|
+
/**
|
|
378
|
+
* Default cap (serialized UTF-8 bytes) on the capture data snapshot riding
|
|
379
|
+
* inside a shell entry, when the route's `ppr` option does not set
|
|
380
|
+
* `maxSnapshotBytes`. 8 MiB: the snapshot shares the stored envelope with the
|
|
381
|
+
* base64 prelude and the postponed blob, and the tightest store value limit is
|
|
382
|
+
* Cloudflare KV's 25 MiB — 8 MiB of snapshot leaves the envelope well under it
|
|
383
|
+
* while still fitting any sane pinned-ring payload. Applied ONLY in
|
|
384
|
+
* captureAndStoreShell (the single defaulting site — resolvePprConfig passes
|
|
385
|
+
* the option through undefaulted), so every producer and direct caller gets
|
|
386
|
+
* the same policy. Over the cap the snapshot is skipped (shell still stored;
|
|
387
|
+
* pinned reads drift — see PartialPrerenderProps.maxSnapshotBytes).
|
|
388
|
+
*/
|
|
389
|
+
export const DEFAULT_PPR_MAX_SNAPSHOT_BYTES: number = 8 * 1024 * 1024;
|
|
390
|
+
|
|
391
|
+
/** Cached encoder for the snapshot byte measurement (one per module, not per capture). */
|
|
392
|
+
const SNAPSHOT_BYTE_ENCODER = new TextEncoder();
|
|
393
|
+
|
|
394
|
+
/** Keys already warned about an over-cap snapshot (once per key per isolate). */
|
|
395
|
+
const warnedOverCapSnapshots = new Set<string>();
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Warn once per key that the capture data snapshot exceeded the route's
|
|
399
|
+
* `maxSnapshotBytes` cap and was skipped. The shell entry is still stored and
|
|
400
|
+
* served — only the pinned-read replay is lost, so shell-baked cached content
|
|
401
|
+
* can drift from the frozen prelude between capture and HIT and hydration
|
|
402
|
+
* repairs it client-side (the pre-snapshot behavior). Once per key: the same
|
|
403
|
+
* page recaptures on every TTL roll and would otherwise re-warn forever.
|
|
404
|
+
*/
|
|
405
|
+
function warnSnapshotOverCapOnce(
|
|
406
|
+
key: string,
|
|
407
|
+
snapshotBytes: number,
|
|
408
|
+
capBytes: number,
|
|
409
|
+
): void {
|
|
410
|
+
if (warnedOverCapSnapshots.has(key)) return;
|
|
411
|
+
warnedOverCapSnapshots.add(key);
|
|
412
|
+
console.warn(
|
|
413
|
+
`[rango] Shell capture for "${key}" recorded a ${snapshotBytes}-byte data ` +
|
|
414
|
+
`snapshot, over the ${capBytes}-byte cap — the snapshot was skipped and ` +
|
|
415
|
+
"the shell was stored without it. The page keeps serving, but cached " +
|
|
416
|
+
"content baked into the shell is no longer pinned: if it drifts before " +
|
|
417
|
+
"the shell's TTL, hydration repairs the mismatch client-side. Raise the " +
|
|
418
|
+
"cap via the route's ppr option ({ maxSnapshotBytes }) if the entry " +
|
|
419
|
+
"still fits your store's value limit (Cloudflare KV: 25 MiB per value), " +
|
|
420
|
+
"or shrink the cache()'d data the shell bakes.",
|
|
421
|
+
);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* One structured event from the background capture pipeline, mirroring the
|
|
426
|
+
* CFCacheReadDebugEvent pattern (cache/cf/cf-cache-types.ts): typed fields an
|
|
427
|
+
* operator can assert against, emitted per attempt and per skip, so the
|
|
428
|
+
* stored / no-shell / refused / backed-off lifecycle is observable outside
|
|
429
|
+
* dev console warnings. Configured via `createRouter({ debugShellCapture })`.
|
|
430
|
+
*/
|
|
431
|
+
export interface ShellCaptureDebugEvent {
|
|
432
|
+
/** Shell cache key the event is about. */
|
|
433
|
+
key: string;
|
|
434
|
+
/**
|
|
435
|
+
* What happened:
|
|
436
|
+
* - stored / redirect / no-shell / refused: one capture ATTEMPT's outcome
|
|
437
|
+
* (see CaptureAttemptOutcome for the semantics of each)
|
|
438
|
+
* - error: the capture task failed with a genuine error (also routed through
|
|
439
|
+
* reportCacheError; the key is backed off)
|
|
440
|
+
* - skip-in-flight: scheduleShellCapture found a capture already running for
|
|
441
|
+
* the key (stampede guard) and scheduled nothing
|
|
442
|
+
* - skip-backoff: the key is inside its refused-capture backoff window and
|
|
443
|
+
* the capture was not attempted
|
|
444
|
+
* - backoff: the key entered (or escalated) backoff after a terminal
|
|
445
|
+
* no-shell — carries the new backoff state
|
|
446
|
+
*/
|
|
447
|
+
outcome:
|
|
448
|
+
| "stored"
|
|
449
|
+
| "redirect"
|
|
450
|
+
| "no-shell"
|
|
451
|
+
| "refused"
|
|
452
|
+
| "error"
|
|
453
|
+
| "skip-in-flight"
|
|
454
|
+
| "skip-backoff"
|
|
455
|
+
| "backoff";
|
|
456
|
+
/** Attempt number (1 = first, 2 = in-place retry). Absent on skips. */
|
|
457
|
+
attempt?: number;
|
|
458
|
+
/** Wall-clock ms of the whole attempt (barrier + render + drain + put). */
|
|
459
|
+
attemptMs?: number;
|
|
460
|
+
/**
|
|
461
|
+
* Wall-clock ms the pre-render WRITE BARRIER waited on the foreground
|
|
462
|
+
* request's deferred cache writes (bounded by SHELL_CAPTURE_WRITE_BARRIER_MS).
|
|
463
|
+
*/
|
|
464
|
+
barrierWaitMs?: number;
|
|
465
|
+
/**
|
|
466
|
+
* Wall-clock ms spent awaiting the capture's own deferred cache writes
|
|
467
|
+
* before the snapshot drain (bounded by SHELL_SNAPSHOT_WRITE_SETTLE_MS).
|
|
468
|
+
*/
|
|
469
|
+
writeSettleMs?: number;
|
|
470
|
+
/** Stored prelude size in bytes (pre-base64). */
|
|
471
|
+
preludeBytes?: number;
|
|
472
|
+
/** Serialized snapshot size in UTF-8 bytes. Absent when nothing was recorded. */
|
|
473
|
+
snapshotBytes?: number;
|
|
474
|
+
/** True when the snapshot exceeded maxSnapshotBytes and was dropped. */
|
|
475
|
+
snapshotSkipped?: boolean;
|
|
476
|
+
/** Consecutive failure count in the key's backoff entry, when one exists. */
|
|
477
|
+
backoffFailures?: number;
|
|
478
|
+
/** Ms remaining in the key's backoff window, when one exists. */
|
|
479
|
+
backoffRemainingMs?: number;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Debug sink for the capture pipeline, mirroring {@link CFCacheDebug}: `true`
|
|
484
|
+
* logs each event to console (visible via `wrangler tail`), a function
|
|
485
|
+
* receives the events for programmatic capture. Off by default.
|
|
486
|
+
*/
|
|
487
|
+
export type ShellCaptureDebug =
|
|
488
|
+
| boolean
|
|
489
|
+
| ((event: ShellCaptureDebugEvent) => void);
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Compact single-line form of an event's fields, shared by the console sink
|
|
493
|
+
* and the dev Server-Timing mirror's `desc` (rsc-rendering). Plain
|
|
494
|
+
* alphanumerics/`=`/`-`/`()` only, so it needs no quoted-string escaping.
|
|
495
|
+
*/
|
|
496
|
+
export function describeShellCaptureEvent(
|
|
497
|
+
event: ShellCaptureDebugEvent,
|
|
498
|
+
): string {
|
|
499
|
+
const parts: string[] = [event.outcome];
|
|
500
|
+
if (event.attempt !== undefined) parts.push(`attempt=${event.attempt}`);
|
|
501
|
+
if (event.attemptMs !== undefined) parts.push(`${event.attemptMs}ms`);
|
|
502
|
+
if (event.barrierWaitMs !== undefined) {
|
|
503
|
+
parts.push(`barrier=${event.barrierWaitMs}ms`);
|
|
504
|
+
}
|
|
505
|
+
if (event.writeSettleMs !== undefined) {
|
|
506
|
+
parts.push(`write-settle=${event.writeSettleMs}ms`);
|
|
507
|
+
}
|
|
508
|
+
if (event.preludeBytes !== undefined) {
|
|
509
|
+
parts.push(`prelude=${event.preludeBytes}b`);
|
|
510
|
+
}
|
|
511
|
+
if (event.snapshotBytes !== undefined) {
|
|
512
|
+
parts.push(
|
|
513
|
+
`snapshot=${event.snapshotBytes}b${event.snapshotSkipped ? " (over cap, skipped)" : ""}`,
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
if (event.backoffFailures !== undefined) {
|
|
517
|
+
parts.push(`backoff-failures=${event.backoffFailures}`);
|
|
518
|
+
}
|
|
519
|
+
if (event.backoffRemainingMs !== undefined) {
|
|
520
|
+
parts.push(`backoff-remaining=${event.backoffRemainingMs}ms`);
|
|
521
|
+
}
|
|
522
|
+
return parts.join(" ");
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/** The `debugShellCapture: true` console sink: one compact line per event. */
|
|
526
|
+
function consoleCaptureDebugSink(event: ShellCaptureDebugEvent): void {
|
|
527
|
+
console.log(
|
|
528
|
+
`[ShellCache][debug] ${event.key} ${describeShellCaptureEvent(event)}`,
|
|
529
|
+
);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* Resolve the `debugShellCapture` router option to a callable sink, or
|
|
534
|
+
* undefined when off. The INTERNAL_RANGO_DEBUG env-flag fallback lives HERE
|
|
535
|
+
* (not at a call site) so every producer that resolves a sink inherits it;
|
|
536
|
+
* an explicit `false` wins over the env flag.
|
|
537
|
+
*/
|
|
538
|
+
export function resolveShellCaptureDebugSink(
|
|
539
|
+
option: ShellCaptureDebug | undefined,
|
|
540
|
+
): ((event: ShellCaptureDebugEvent) => void) | undefined {
|
|
541
|
+
if (option === false) return undefined;
|
|
542
|
+
if (option === true) return consoleCaptureDebugSink;
|
|
543
|
+
if (typeof option === "function") return option;
|
|
544
|
+
return INTERNAL_RANGO_DEBUG ? consoleCaptureDebugSink : undefined;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Attempt-terminal outcomes recorded for the dev Server-Timing mirror. Skip
|
|
549
|
+
* events are excluded so a later request's skip cannot overwrite the
|
|
550
|
+
* interesting terminal event before a metrics-enabled request reads it.
|
|
551
|
+
*/
|
|
552
|
+
const TIMING_RECORDED_OUTCOMES = new Set<ShellCaptureDebugEvent["outcome"]>([
|
|
553
|
+
"stored",
|
|
554
|
+
"redirect",
|
|
555
|
+
"no-shell",
|
|
556
|
+
"refused",
|
|
557
|
+
"error",
|
|
558
|
+
]);
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* Dev-only last-terminal-event-per-key buffer backing the Server-Timing
|
|
562
|
+
* mirror: the capture runs AFTER its triggering response is committed, so its
|
|
563
|
+
* outcome can only ride a LATER response's header. rsc-rendering consumes this
|
|
564
|
+
* on the next ppr GET for the key when the metrics store is active
|
|
565
|
+
* (debugPerformance) and appends a `ppr:capture` Server-Timing entry. Dev-only
|
|
566
|
+
* (isDevMode) so production isolates never grow the map; FIFO-capped because
|
|
567
|
+
* with debugPerformance OFF nothing ever drains it, and a long dev session
|
|
568
|
+
* sweeping many URLs would otherwise accumulate one entry per shell key
|
|
569
|
+
* forever.
|
|
570
|
+
*/
|
|
571
|
+
const lastCaptureEventsForTiming = new Map<string, ShellCaptureDebugEvent>();
|
|
572
|
+
const MAX_TIMING_EVENT_KEYS = 100;
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* Consume (read-and-clear) the buffered terminal capture event for `key`, so
|
|
576
|
+
* one capture reports into exactly one later response's Server-Timing.
|
|
577
|
+
*/
|
|
578
|
+
export function takeCaptureDebugEventForTiming(
|
|
579
|
+
key: string,
|
|
580
|
+
): ShellCaptureDebugEvent | undefined {
|
|
581
|
+
const event = lastCaptureEventsForTiming.get(key);
|
|
582
|
+
if (event) lastCaptureEventsForTiming.delete(key);
|
|
583
|
+
return event;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Publish one capture debug event: buffer terminal outcomes for the dev
|
|
588
|
+
* Server-Timing mirror, then hand the event to the configured sink. A
|
|
589
|
+
* throwing sink is swallowed — diagnostics must never fail a capture.
|
|
590
|
+
*/
|
|
591
|
+
function publishCaptureDebugEvent(
|
|
592
|
+
descriptor: Pick<ShellCaptureDescriptor, "debugSink">,
|
|
593
|
+
event: ShellCaptureDebugEvent,
|
|
594
|
+
): void {
|
|
595
|
+
if (isDevMode() && TIMING_RECORDED_OUTCOMES.has(event.outcome)) {
|
|
596
|
+
// Refresh insertion order for the FIFO cap, then evict the oldest key.
|
|
597
|
+
lastCaptureEventsForTiming.delete(event.key);
|
|
598
|
+
if (lastCaptureEventsForTiming.size >= MAX_TIMING_EVENT_KEYS) {
|
|
599
|
+
const oldest = lastCaptureEventsForTiming.keys().next().value;
|
|
600
|
+
if (oldest !== undefined) lastCaptureEventsForTiming.delete(oldest);
|
|
601
|
+
}
|
|
602
|
+
lastCaptureEventsForTiming.set(event.key, event);
|
|
603
|
+
}
|
|
604
|
+
const sink = descriptor.debugSink;
|
|
605
|
+
if (!sink) return;
|
|
606
|
+
try {
|
|
607
|
+
sink(event);
|
|
608
|
+
} catch {
|
|
609
|
+
// Diagnostics only: a throwing consumer sink must never fail the capture.
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/** Current backoff state fields for `key` (empty when no backoff entry). */
|
|
614
|
+
function backoffFields(
|
|
615
|
+
key: string,
|
|
616
|
+
): Pick<ShellCaptureDebugEvent, "backoffFailures" | "backoffRemainingMs"> {
|
|
617
|
+
const entry = refusedCaptures.get(key);
|
|
618
|
+
if (!entry) return {};
|
|
619
|
+
return {
|
|
620
|
+
backoffFailures: entry.failures,
|
|
621
|
+
backoffRemainingMs: Math.max(0, entry.until - Date.now()),
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
|
|
372
625
|
export interface FlightCaptureGate {
|
|
373
626
|
/** Identity passthrough of the source stream; feed this to captureShellHTML. */
|
|
374
627
|
stream: ReadableStream<Uint8Array>;
|
|
@@ -548,9 +801,32 @@ export interface ShellCaptureDescriptor {
|
|
|
548
801
|
ttl?: number;
|
|
549
802
|
swr?: number;
|
|
550
803
|
tags?: string[];
|
|
804
|
+
/**
|
|
805
|
+
* Per-route capture settle budget in ms (`ppr.captureTimeout`, resolved by
|
|
806
|
+
* resolvePprConfig). Feeds captureShellHTML's maxWaitMs — the ONE deadline
|
|
807
|
+
* bounding the whole capture, so it covers BOTH the fizz prerender AND the
|
|
808
|
+
* deferred-material settle window (the handlesBaked/loader-container
|
|
809
|
+
* holdUntil that keeps the gate from freezing while top-level pushes are
|
|
810
|
+
* pending). Undefined = SHELL_CAPTURE_MAX_WAIT_MS (5000).
|
|
811
|
+
*/
|
|
812
|
+
captureTimeout?: number;
|
|
551
813
|
store?: SegmentCacheStore<any>;
|
|
552
814
|
/** Gates the concise per-attempt capture breadcrumbs (INTERNAL_RANGO_DEBUG). */
|
|
553
815
|
debug?: boolean;
|
|
816
|
+
/**
|
|
817
|
+
* Cap (serialized UTF-8 bytes) on the entry's capture data snapshot; over it
|
|
818
|
+
* the snapshot is skipped and the shell stored without it (reported once per
|
|
819
|
+
* key). Absent = DEFAULT_PPR_MAX_SNAPSHOT_BYTES, applied in
|
|
820
|
+
* captureAndStoreShell — the single defaulting site.
|
|
821
|
+
*/
|
|
822
|
+
maxSnapshotBytes?: number;
|
|
823
|
+
/**
|
|
824
|
+
* Structured capture-pipeline debug sink, resolved from
|
|
825
|
+
* `createRouter({ debugShellCapture })` (or INTERNAL_RANGO_DEBUG) via
|
|
826
|
+
* {@link resolveShellCaptureDebugSink}. Receives one
|
|
827
|
+
* {@link ShellCaptureDebugEvent} per attempt/skip.
|
|
828
|
+
*/
|
|
829
|
+
debugSink?: (event: ShellCaptureDebugEvent) => void;
|
|
554
830
|
}
|
|
555
831
|
|
|
556
832
|
/**
|
|
@@ -574,10 +850,20 @@ export function scheduleShellCapture(
|
|
|
574
850
|
descriptor: ShellCaptureDescriptor,
|
|
575
851
|
): void {
|
|
576
852
|
const key = descriptor.key;
|
|
577
|
-
if (inFlightCaptures.has(key))
|
|
853
|
+
if (inFlightCaptures.has(key)) {
|
|
854
|
+
publishCaptureDebugEvent(descriptor, { key, outcome: "skip-in-flight" });
|
|
855
|
+
return;
|
|
856
|
+
}
|
|
578
857
|
// Refused/failed within the window → skip the doomed re-render (one probe per
|
|
579
858
|
// key per window per isolate). Expired entries self-evict inside the check.
|
|
580
|
-
if (isCaptureBackedOff(key))
|
|
859
|
+
if (isCaptureBackedOff(key)) {
|
|
860
|
+
publishCaptureDebugEvent(descriptor, {
|
|
861
|
+
key,
|
|
862
|
+
outcome: "skip-backoff",
|
|
863
|
+
...backoffFields(key),
|
|
864
|
+
});
|
|
865
|
+
return;
|
|
866
|
+
}
|
|
581
867
|
inFlightCaptures.add(key);
|
|
582
868
|
const captureTask = async () => {
|
|
583
869
|
try {
|
|
@@ -595,12 +881,24 @@ export function scheduleShellCapture(
|
|
|
595
881
|
// off so the next requests don't re-probe it. A `redirect` has no shell but
|
|
596
882
|
// is not a doomed render — leave the backoff untouched.
|
|
597
883
|
if (outcome === "stored") clearCaptureBackoff(key);
|
|
598
|
-
else if (outcome === "no-shell")
|
|
884
|
+
else if (outcome === "no-shell") {
|
|
885
|
+
markCaptureBackoff(key);
|
|
886
|
+
publishCaptureDebugEvent(descriptor, {
|
|
887
|
+
key,
|
|
888
|
+
outcome: "backoff",
|
|
889
|
+
...backoffFields(key),
|
|
890
|
+
});
|
|
891
|
+
}
|
|
599
892
|
} catch (error) {
|
|
600
893
|
// Detached background task — pass reqCtx so onError still fires when the ALS
|
|
601
894
|
// context is gone. A genuine failure recurs, so back it off too (re-probe
|
|
602
895
|
// once per window, not every request) and report it once.
|
|
603
896
|
markCaptureBackoff(key);
|
|
897
|
+
publishCaptureDebugEvent(descriptor, {
|
|
898
|
+
key,
|
|
899
|
+
outcome: "error",
|
|
900
|
+
...backoffFields(key),
|
|
901
|
+
});
|
|
604
902
|
reportCacheError(error, "cache-write", "[ShellCache] capture", reqCtx);
|
|
605
903
|
} finally {
|
|
606
904
|
inFlightCaptures.delete(key);
|
|
@@ -633,6 +931,21 @@ export function scheduleShellCapture(
|
|
|
633
931
|
*/
|
|
634
932
|
type CaptureAttemptOutcome = "stored" | "redirect" | "no-shell" | "refused";
|
|
635
933
|
|
|
934
|
+
/**
|
|
935
|
+
* Per-attempt observability fields, filled along the capture path (barrier in
|
|
936
|
+
* attemptCapture, the rest in captureAndStoreShell) and folded into the
|
|
937
|
+
* attempt's {@link ShellCaptureDebugEvent} by runShellCapture. A plain mutable
|
|
938
|
+
* bag, not a return value: captureAndStoreShell's outcome type stays a string
|
|
939
|
+
* union its existing callers (producer B, tests) consume unchanged.
|
|
940
|
+
*/
|
|
941
|
+
interface CaptureAttemptStats {
|
|
942
|
+
barrierWaitMs?: number;
|
|
943
|
+
writeSettleMs?: number;
|
|
944
|
+
preludeBytes?: number;
|
|
945
|
+
snapshotBytes?: number;
|
|
946
|
+
snapshotSkipped?: boolean;
|
|
947
|
+
}
|
|
948
|
+
|
|
636
949
|
/**
|
|
637
950
|
* Run the shell capture with a single in-place retry, then store the result.
|
|
638
951
|
*
|
|
@@ -665,15 +978,37 @@ async function runShellCapture(
|
|
|
665
978
|
? (message: string) => console.log(message)
|
|
666
979
|
: () => {};
|
|
667
980
|
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
981
|
+
// One attempt + its structured debug event: the stats object rides through
|
|
982
|
+
// attemptCapture/captureAndStoreShell collecting the observability fields
|
|
983
|
+
// (barrier wait, write-settle wait, prelude/snapshot bytes), and the event
|
|
984
|
+
// folds them with the outcome. A genuine render error skips the attempt
|
|
985
|
+
// event — scheduleShellCapture's catch publishes the terminal `error` event.
|
|
986
|
+
const timedAttempt = async (
|
|
987
|
+
attempt: number,
|
|
988
|
+
): Promise<CaptureAttemptOutcome> => {
|
|
989
|
+
const stats: CaptureAttemptStats = {};
|
|
990
|
+
const start = performance.now();
|
|
991
|
+
const outcome = await attemptCapture(
|
|
992
|
+
ctx,
|
|
993
|
+
request,
|
|
994
|
+
env,
|
|
995
|
+
url,
|
|
996
|
+
reqCtx,
|
|
997
|
+
ssrModule,
|
|
998
|
+
descriptor,
|
|
999
|
+
stats,
|
|
1000
|
+
);
|
|
1001
|
+
publishCaptureDebugEvent(descriptor, {
|
|
1002
|
+
key: descriptor.key,
|
|
1003
|
+
outcome,
|
|
1004
|
+
attempt,
|
|
1005
|
+
attemptMs: Math.round(performance.now() - start),
|
|
1006
|
+
...stats,
|
|
1007
|
+
});
|
|
1008
|
+
return outcome;
|
|
1009
|
+
};
|
|
1010
|
+
|
|
1011
|
+
const first = await timedAttempt(1);
|
|
677
1012
|
// "refused" is deterministic (identity guard / rejected bake-lane loader —
|
|
678
1013
|
// its own warning already fired): no retry, and the caller backs the key off
|
|
679
1014
|
// exactly like a structural no-shell.
|
|
@@ -690,15 +1025,7 @@ async function runShellCapture(
|
|
|
690
1025
|
`[ShellCache] capture attempt 1/2 for ${descriptor.key} aborted before shell completed (cold modules?) — retrying`,
|
|
691
1026
|
);
|
|
692
1027
|
await delay(retryDelayMs);
|
|
693
|
-
const second = await
|
|
694
|
-
ctx,
|
|
695
|
-
request,
|
|
696
|
-
env,
|
|
697
|
-
url,
|
|
698
|
-
reqCtx,
|
|
699
|
-
ssrModule,
|
|
700
|
-
descriptor,
|
|
701
|
-
);
|
|
1028
|
+
const second = await timedAttempt(2);
|
|
702
1029
|
if (second === "refused") return "no-shell";
|
|
703
1030
|
if (second !== "no-shell") return second;
|
|
704
1031
|
|
|
@@ -767,6 +1094,7 @@ async function attemptCapture(
|
|
|
767
1094
|
reqCtx: RequestContext<any>,
|
|
768
1095
|
ssrModule: SSRModule,
|
|
769
1096
|
descriptor: ShellCaptureDescriptor,
|
|
1097
|
+
stats: CaptureAttemptStats,
|
|
770
1098
|
): Promise<CaptureAttemptOutcome> {
|
|
771
1099
|
// WRITE BARRIER (ordering edge, not a narrower race): settle the foreground's
|
|
772
1100
|
// already-scheduled background tasks — its deferred ring-3/ring-1 cache writes —
|
|
@@ -777,7 +1105,9 @@ async function attemptCapture(
|
|
|
777
1105
|
// skipped, cache-store middleware's write path gated off by state.cacheHit), so
|
|
778
1106
|
// prelude, snapshot, and ring-3 agree on the foreground's generation. Runs per
|
|
779
1107
|
// attempt (the retry re-checks; already-settled promises are free).
|
|
1108
|
+
const barrierStart = performance.now();
|
|
780
1109
|
await settleTrackedBackgroundTasks(reqCtx, SHELL_CAPTURE_WRITE_BARRIER_MS);
|
|
1110
|
+
stats.barrierWaitMs = Math.round(performance.now() - barrierStart);
|
|
781
1111
|
|
|
782
1112
|
const { derivedCtx, freshHandleStore } = deriveShellCaptureContext(
|
|
783
1113
|
reqCtx,
|
|
@@ -817,6 +1147,7 @@ async function attemptCapture(
|
|
|
817
1147
|
freshHandleStore,
|
|
818
1148
|
derivedCtx,
|
|
819
1149
|
descriptor,
|
|
1150
|
+
stats,
|
|
820
1151
|
);
|
|
821
1152
|
});
|
|
822
1153
|
}
|
|
@@ -1014,6 +1345,7 @@ async function captureAndStoreShell(
|
|
|
1014
1345
|
handleStore: HandleStore,
|
|
1015
1346
|
reqCtx: RequestContext<any>,
|
|
1016
1347
|
capture: ShellCaptureDescriptor,
|
|
1348
|
+
stats?: CaptureAttemptStats,
|
|
1017
1349
|
): Promise<Exclude<CaptureAttemptOutcome, "redirect">> {
|
|
1018
1350
|
const captureShellHTML = ssrModule.captureShellHTML!;
|
|
1019
1351
|
|
|
@@ -1102,10 +1434,12 @@ async function captureAndStoreShell(
|
|
|
1102
1434
|
// captureShellHTML CONSUMES the (gated) stream — it is not also SSR'd.
|
|
1103
1435
|
let result: Awaited<ReturnType<typeof captureShellHTML>>;
|
|
1104
1436
|
try {
|
|
1437
|
+
// One deadline for the whole capture — semantics spec'd on the option
|
|
1438
|
+
// (PartialPrerenderProps.captureTimeout, urls/pattern-types.ts).
|
|
1105
1439
|
result = await observePhase(PHASES.ssr, () =>
|
|
1106
1440
|
captureShellHTML(gate.stream, {
|
|
1107
1441
|
quiesce,
|
|
1108
|
-
maxWaitMs: SHELL_CAPTURE_MAX_WAIT_MS,
|
|
1442
|
+
maxWaitMs: capture.captureTimeout ?? SHELL_CAPTURE_MAX_WAIT_MS,
|
|
1109
1443
|
}),
|
|
1110
1444
|
);
|
|
1111
1445
|
} catch (error) {
|
|
@@ -1141,6 +1475,7 @@ async function captureAndStoreShell(
|
|
|
1141
1475
|
if (result === null) {
|
|
1142
1476
|
return "no-shell";
|
|
1143
1477
|
}
|
|
1478
|
+
if (stats) stats.preludeBytes = result.prelude.length;
|
|
1144
1479
|
|
|
1145
1480
|
// Store per the flag's key/ttl/swr/tags, into the flag's store: the middleware
|
|
1146
1481
|
// threads the SAME store it resolved for its getShell read (options.store ??
|
|
@@ -1180,7 +1515,11 @@ async function captureAndStoreShell(
|
|
|
1180
1515
|
const recording = getRecordingStore(reqCtx._cacheStore);
|
|
1181
1516
|
let snapshot: ShellSnapshotRecord[] | undefined;
|
|
1182
1517
|
if (recording) {
|
|
1518
|
+
const settleStart = performance.now();
|
|
1183
1519
|
await recording.settleWrites(SHELL_SNAPSHOT_WRITE_SETTLE_MS);
|
|
1520
|
+
if (stats) {
|
|
1521
|
+
stats.writeSettleMs = Math.round(performance.now() - settleStart);
|
|
1522
|
+
}
|
|
1184
1523
|
snapshot = recording.drainSnapshot();
|
|
1185
1524
|
}
|
|
1186
1525
|
|
|
@@ -1239,6 +1578,29 @@ async function captureAndStoreShell(
|
|
|
1239
1578
|
}
|
|
1240
1579
|
}
|
|
1241
1580
|
|
|
1581
|
+
// Snapshot size guard (issue #651): the snapshot duplicates every pinned
|
|
1582
|
+
// cache value inside the shell entry, so a page over a large cache()
|
|
1583
|
+
// segment can push the stored envelope toward store value limits (KV caps
|
|
1584
|
+
// a value at 25 MiB) with no signal — the kv.put rejects deep inside
|
|
1585
|
+
// waitUntil. Measure the serialized snapshot (UTF-8 bytes of the JSON that
|
|
1586
|
+
// rides in the envelope) AFTER the loader family is appended, and over the
|
|
1587
|
+
// cap store the shell WITHOUT it: pinned reads then fall back to the live
|
|
1588
|
+
// store on a HIT (documented drift — hydration repairs a mismatch
|
|
1589
|
+
// client-side, the pre-snapshot behavior), which beats losing the whole
|
|
1590
|
+
// entry to a store-side write rejection. Reported once per key.
|
|
1591
|
+
if (snapshot && snapshot.length > 0) {
|
|
1592
|
+
const snapshotBytes = SNAPSHOT_BYTE_ENCODER.encode(
|
|
1593
|
+
JSON.stringify(snapshot),
|
|
1594
|
+
).length;
|
|
1595
|
+
if (stats) stats.snapshotBytes = snapshotBytes;
|
|
1596
|
+
const cap = capture.maxSnapshotBytes ?? DEFAULT_PPR_MAX_SNAPSHOT_BYTES;
|
|
1597
|
+
if (snapshotBytes > cap) {
|
|
1598
|
+
warnSnapshotOverCapOnce(capture.key, snapshotBytes, cap);
|
|
1599
|
+
snapshot = undefined;
|
|
1600
|
+
if (stats) stats.snapshotSkipped = true;
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1603
|
+
|
|
1242
1604
|
// Shell tags snapshot at the WRITE BARRIER, not at stream construction: by
|
|
1243
1605
|
// here the capture has quiesced and the deferred cache writes were awaited, so
|
|
1244
1606
|
// tags recorded AFTER an await in async shell content (and by async
|
package/src/rsc/shell-serve.ts
CHANGED
|
@@ -30,11 +30,47 @@ export const SHELL_STATUS_HEADER = "x-rango-shell";
|
|
|
30
30
|
*/
|
|
31
31
|
export const DEFAULT_PPR_TTL_SECONDS = 300;
|
|
32
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Timeout for the dev /__rsc_shell endpoint's sequential /__rsc_prerender
|
|
35
|
+
* pre-flight probe (vite/router-discovery.ts). Hoisted here so the client-side
|
|
36
|
+
* fetch bound (shell-build-manifest.ts devShellFetchTimeoutMs) enumerates the
|
|
37
|
+
* SAME term of the endpoint's worst-case envelope — the two cannot drift.
|
|
38
|
+
*/
|
|
39
|
+
export const DEV_SHELL_PROBE_TIMEOUT_MS: number = 10_000;
|
|
40
|
+
|
|
33
41
|
/** The route's ppr option normalized to a concrete policy. */
|
|
34
42
|
export interface ResolvedPprConfig {
|
|
35
43
|
ttl: number;
|
|
36
44
|
swr?: number;
|
|
37
45
|
tags?: string[];
|
|
46
|
+
/**
|
|
47
|
+
* Snapshot size cap, passed through undefaulted (like swr/tags): the single
|
|
48
|
+
* defaulting site is captureAndStoreShell (DEFAULT_PPR_MAX_SNAPSHOT_BYTES in
|
|
49
|
+
* shell-capture.ts), so direct descriptor callers and resolved configs
|
|
50
|
+
* cannot drift.
|
|
51
|
+
*/
|
|
52
|
+
maxSnapshotBytes?: number;
|
|
53
|
+
/**
|
|
54
|
+
* Capture settle budget in ms (`ppr.captureTimeout`). Undefined = the
|
|
55
|
+
* capture default (SHELL_CAPTURE_MAX_WAIT_MS, 5000) — the default's single
|
|
56
|
+
* owner stays shell-capture.ts so build/runtime producers cannot drift.
|
|
57
|
+
*/
|
|
58
|
+
captureTimeout?: number;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Validate the raw `ppr.captureTimeout` option: a finite number >= 1ms passes
|
|
63
|
+
* through; anything else (including 0/negative/NaN/Infinity/non-number)
|
|
64
|
+
* resolves to undefined, which means "use the capture default" downstream.
|
|
65
|
+
* Mirrors the prefetch-limit option policy: invalid values silently fall back
|
|
66
|
+
* to the default rather than throwing at request time. Also the boundary
|
|
67
|
+
* re-normalizer for the dev /__rsc_shell endpoint (vite/router-discovery.ts),
|
|
68
|
+
* whose param crossed an HTTP query string.
|
|
69
|
+
*/
|
|
70
|
+
export function normalizeCaptureTimeout(value: unknown): number | undefined {
|
|
71
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 1
|
|
72
|
+
? value
|
|
73
|
+
: undefined;
|
|
38
74
|
}
|
|
39
75
|
|
|
40
76
|
/**
|
|
@@ -42,6 +78,12 @@ export interface ResolvedPprConfig {
|
|
|
42
78
|
* route does not declare `ppr` (or declares `ppr: false`) — the caller then does
|
|
43
79
|
* NOTHING: no store read, no capture, no logs. Pure axis 1, zero cost.
|
|
44
80
|
*
|
|
81
|
+
* The route's NAME is irrelevant here (and everywhere on the shell lane):
|
|
82
|
+
* nameless `path()` routes register their EntryData under a synthesized
|
|
83
|
+
* `$path_*` manifest key with the `ppr` option intact (urls/path-helper.ts),
|
|
84
|
+
* so a nameless entry resolves exactly like a named one — pinned by the
|
|
85
|
+
* nameless-ppr e2e in both apps (issue #714).
|
|
86
|
+
*
|
|
45
87
|
* PPR is a DOCUMENT-level property of the page route; there is no subtree
|
|
46
88
|
* inheritance (declaring it on a layout is not supported — a follow-up).
|
|
47
89
|
*/
|
|
@@ -56,6 +98,8 @@ export function resolvePprConfig(
|
|
|
56
98
|
ttl: ppr.ttl ?? DEFAULT_PPR_TTL_SECONDS,
|
|
57
99
|
swr: ppr.swr,
|
|
58
100
|
tags: ppr.tags,
|
|
101
|
+
maxSnapshotBytes: ppr.maxSnapshotBytes,
|
|
102
|
+
captureTimeout: normalizeCaptureTimeout(ppr.captureTimeout),
|
|
59
103
|
};
|
|
60
104
|
}
|
|
61
105
|
|