AI Focus

dpu out-of-order streaming workbench

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 (<?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().

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.
Stream out-of-order HTML sections over one Cloudflare Worker TransformStream using WICG Declarative Partial Updates, measuring layout shifts (CLS), Long Animation Frames (LoAF), input focus survival, and setHTML() XSS sanitization. Open dpu out-of-order streaming workbench full screen ↗

Hypothesis

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.

What 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.

What 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 (GitHub) 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) ↗
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) ↗
A browser API that safely parses untrusted HTML, stripping <script> tags, inline on* handlers, and javascript: links before DOM insertion.
Cumulative Layout Shift (CLS) ↗
A Core Web Vital measuring unexpected page movement; scores under 0.1 confirm reserved placeholder heights prevented layout jumps.
Long Animation Frames (LoAF) ↗
A PerformanceObserver entry that flags rendering stalls over 50 ms, helping compare surgical range patching against full-DOM rebuilds.
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 ↗
HTTP response headers instructing reverse proxies not to buffer or rewrite streamed chunks until EOF.

Open questions

  • 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

  • — 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.

This is an active lab under /labs/ and stays off the main RSS feed until graduated into a full essay.

More in post-<a> hypermedia & composable islands

Connected labs

← post-<a> hypermedia & composable islands · All labs