/** * Stream remote fragments (Cloudflare Fragments pattern). * * Host emits a shell with `` placeholders. Each * remote SSRs its own slice in parallel, and the host swaps the placeholder * for the streamed HTML. When a fragment fails / times out, its `fallback` * markup is used instead. * * Two surfaces: * - `renderFragmentsToString(...)` — concatenated single-shot HTML * - `renderFragmentsToReadableStream` — Web ReadableStream that flushes the * shell head, then each fragment as it resolves (parallel, out-of-order * OK, then placed back into its slot via a tiny inline script). * * Pure-data; works in Workers, Edge, Node. */ export interface FragmentSpec { name: string; /** Async fragment renderer — returns the HTML for this slot. */ render: (signal: AbortSignal) => Promise; /** Markup if `render` fails or times out. Default: empty string. */ fallback?: string; /** Per-fragment timeout. Default: parent `timeoutMs`. */ timeoutMs?: number; } export interface RenderFragmentsOptions { /** Shell HTML containing `` placeholders. */ shell: string; fragments: FragmentSpec[]; /** Default timeout for every fragment. Default: 5_000. */ timeoutMs?: number; /** External cancel signal (e.g. client disconnect). */ signal?: AbortSignal; /** Per-fragment outcome — useful for telemetry. */ onFragment?: (event: FragmentOutcome) => void; /** * CSP nonce applied to the runtime + per-fragment `