# dpu out-of-order streaming workbench

- Status: `experiment`
- Track: [post-<a> hypermedia & composable islands](https://aifoc.us/labs/hypermedia-and-islands/)
- Layer: 2. Post-<a> Hypermedia & Composable UI
- Updated: 2 December 2026
- Live demo: https://dpu-out-of-order-streaming.kinlan-test.workers.dev
- Source: https://github.com/PaulKinlan/dpu-out-of-order-streaming

## Inspired by

- [hypermedia was always the point](https://aifoc.us/hypermedia/)
- [what falling ai model latency means for the web](https://aifoc.us/latency/)
- [web components are packaging for ai-generated ui](https://aifoc.us/elements/)

## Why this matters

Pages that mix fast cached summaries (80 ms) with slow upstream queries or model calls (520 ms+) normally stall first paint until the slowest top section finishes—or ship client JS that re-renders the whole DOM on every update, knocking the cursor out of active text inputs.

## What this opens up

Streaming out-of-order HTML patches over a single HTTP response using [WICG Declarative Partial Updates](https://github.com/WICG/declarative-partial-updates) (`<?start id="..."?>...<?end?>` + `<template for="...">`) so fast lower sections paint right away, late upper sections slot into reserved heights without layout jumps, mid-stream typing focus survives, and untrusted chunks are neutralized by [`setHTML()`](https://developer.mozilla.org/en-US/docs/Web/API/HTML_Sanitizer_API).

## How the tests work

1. Click **1. Run Out-of-Order Stream** to watch the fast bottom conclusion arrive at ~80 ms while the slow top summary finishes last (~520 ms), slotting into canonical reading order with reserved heights keeping `CLS ≈ 0`.
2. Click **2. Trigger Unreserved CLS Trap** to turn off height reservations and watch live `PerformanceObserver` `layout-shift` entries climb past `0.1` as late upper sections push settled content down.
3. Click **3. Run XSS Chunk Probe** to inject an untrusted chunk (`<script>`, image `onerror`, `javascript:` link) and inspect every stripped vector in the sanitizer audit panel.
4. Click **Run Automated Focus-Preservation Test** (or type into the mid-stream input while streaming) to compare surgical comment-range patching against the eager full-DOM re-render baseline.


## Hypothesis

- **Claim:** Streaming an immediate HTML skeleton with `<?start id="..."?>...<?end?>` markers and flushing sanitized `<template for="...">` chunks in completion order over one `TransformStream` delivers fast first paint without client framework overhead, keeps active input focus intact, and strips injected XSS.
- **Confirms it:** The initial skeleton arrives in under `100 ms`; fast lower sections (`sec-conclusion` at `80 ms`) paint before slow upper sections (`sec-intro` at `520 ms`) while landing in canonical order; typing focus inside the mid-stream `<input>` survives every patch; and the XSS probe leaves `window.__pwned === undefined`.
- **Bounds or disproves it:** Switching to **Unreserved Auto-Height** lets late upper patches push settled lower sections downward (`CLS > 0.1` captured live), while the eager JS full-DOM rebuild baseline churns the subtree and drops active input focus.


## Under the hood

[DPU Out-of-Order Streaming Workbench](https://dpu-out-of-order-streaming.kinlan-test.workers.dev) ([GitHub](https://github.com/PaulKinlan/dpu-out-of-order-streaming)) streams live responses from a Cloudflare Worker `TransformStream` (`GET /stream/article?delays=520,380,260,160,80`) with `Cache-Control: no-cache, no-transform` and `X-Accel-Buffering: no`. The Worker flushes an immediate HTML skeleton with `<?start id="..."?>...<?end?>` markers and then pushes `<template for="..." patch-mode="replace">` chunks as parallel timers finish.

In the browser, `public/lib/dpu-stream.js` reads chunks incrementally via `fetch().body.getReader()`, sanitizes each patch through native `Element.prototype.setHTML()` when available (falling back to a strict `DOMParser` allowlist importer with zero dangerous HTML sinks), and replaces only the DOM nodes inside the matching Comment range. A side-by-side eager JS full-DOM re-render baseline runs on the exact same incoming chunks while live `PerformanceObserver` watchers record `layout-shift` (CLS) and `long-animation-frame` (LoAF) entries.


## Key terms & further reading

- [WICG Declarative Partial Updates (DPU)](https://github.com/WICG/declarative-partial-updates) — A web proposal for updating named HTML regions in place with `<?start id="..."?>...<?end?>` markers and streamed `<template for="...">` patches without client frameworks.
- [HTML Sanitizer API (setHTML)](https://developer.mozilla.org/en-US/docs/Web/API/HTML_Sanitizer_API) — A browser API that safely parses untrusted HTML, stripping `<script>` tags, inline `on*` handlers, and `javascript:` links before DOM insertion.
- [Cumulative Layout Shift (CLS)](https://web.dev/articles/cls) — A Core Web Vital measuring unexpected page movement; scores under `0.1` confirm reserved placeholder heights prevented layout jumps.
- [Long Animation Frames (LoAF)](https://developer.mozilla.org/en-US/docs/Web/API/PerformanceLongAnimationFrameTiming) — A `PerformanceObserver` entry that flags rendering stalls over 50 ms, helping compare surgical range patching against full-DOM rebuilds.
- [TransformStream](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream) — A Web Streams API used by the Cloudflare Worker to flush the initial skeleton immediately and write out-of-order HTML patches over one HTTP response.
- [Cache-Control: no-transform & X-Accel-Buffering: no](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Cache-Control#no-transform) — HTTP response headers instructing reverse proxies not to buffer or rewrite streamed chunks until EOF.

## Open questions to iterate on

- How does native browser `<template for>` patching compare with Comment-range polyfill overhead when dozens of small token patches arrive per second?
- What minimum CSS `min-height` / `contain-intrinsic-size` reservation strategy prevents CLS when generated section lengths vary widely?

## Iteration log

- **2026-12-02:** Deployed Cloudflare Worker `TransformStream` endpoint (`/stream/article`) streaming out-of-order `<?start>`/`<?end>` + `<template for>` patches with `setHTML()` / allowlist XSS sanitization, live `PerformanceObserver` CLS/LoAF telemetry, mid-stream `<input>` focus preservation vs. full-DOM re-render comparator, dark mode, and `?embed=1` height bridging.

## Primitives tested

- `WICG Declarative Partial Updates`
- `TransformStream`
- `HTML Sanitizer API (setHTML)`
- `PerformanceObserver (CLS / LoAF)`
- `Comment Range Patching`

## Connected labs in other tracks

- [hypermedia islands lab](https://aifoc.us/labs/hypermedia-and-islands/hypermedia-islands-lab/) — Tests StretchText and View Transitions for client-side document expansion.
- [declarative <ai-prompt> cells & pinning studio](https://aifoc.us/labs/reactive-prompts/reactive-prompt-cells/) — Streams NDJSON updates into HTML custom elements with `AbortController` race protection.
- [self-observing web fault bundler](https://aifoc.us/labs/platform-observability/self-observing-web/) — Captures CLS and Long Animation Frame regressions in a bounded JSON ring buffer.

---
Canonical: https://aifoc.us/labs/hypermedia-and-islands/dpu-out-of-order-streaming/
