Leaving slide mode.

Parser-Aware MutationObserver

Client-side reflection of server-driven UI

Noam Rosenthal & Keith Cirkel
IRC: #whatwg

TPAC 2026
Dublin, Ireland & online
26–30 October 2026

Explainer: https://github.com/WICG/declarative-partial-updates/blob/main/parser-mutation-records-explainer.md. Overall theme: client-side reflection of server-driven UI—parseEnd is for hydration & subtree readiness, and template is for flow control & error recovery.

1. Hydration (dom#662, wc#809)

<!-- Chunk 1 --> <s-select><s-option>A</s-option> <!-- Chunk 2 --> <s-option>B</s-option></s-select>

When the HTML parser sees a start tag, it inserts the element into the DOM immediately before parsing its children. If a network chunk splits inside the element, addedNodes and connectedCallback() see a half-parsed subtree with no signal when the closing tag arrives.

1. Hydration & Subtree Readiness

Four structural (non-text) use cases: (1) compound custom elements like <s-select>, tabs, data tables, or WebGL scenes that index/validate child elements and want to avoid O(N^2) work or false "missing child" errors mid-stream; (2) streaming DOM morphers (idiomorph, Turbo, Marko) that otherwise delete existing live DOM nodes because the incoming subtree isn't finished yet; (3) SSR island hydration that expects the component's full DOM tree; and (4) View Transitions and entry animations on complete subtrees ( plus text/code evaluation like syntax highlighters and math blocks).

Workarounds Today

Today libraries choose between waiting for DOMContentLoaded (giving up incremental rendering), timers (which break on slow networks), or walking ancestors on every MutationRecord to check for a nextSibling (which costs 100ms+ on large pages and still fails for last-child chains).

2. Flow & Error Control (#110)

<!-- Target: <main><?start name="results">...<?end></main> --> <template for="results" data-turn="3"> <p>Streamed response...</p> </template>

When the parser processes a <template for> patch, it consumes the <template> element itself and streams its content directly into the target region (<?start name="results"> inside <main>). MutationObserver only sees individual child insertions inside <main>—the patch wrapper, its attributes, and its start/end/abort boundaries are completely invisible.

2. Patch Flow Control & Error Recovery

What applications need from <template for> observation (#110 from OpenAI, html#13008): (1) attribution—telling server-streamed patches apart from local client mutations; (2) reading metadata attributes like data-turn="3" off the template; (3) lifecycle hooks for patch start and completion; and (4) error recovery when a stream truncates before </template> or targets a missing marker.

Parsing vs. Imperative & Dynamic DOM

“If we expose a parser-completion hook, developers will rely on it and break imperative DOM creation (createElement + appendChild).”

1. body.append(el) connectedCallback() → 0 children ❌ 2. el.append(child) Sync task: 1. body.append(el) + 2. el.append(child) → MO Microtask: sees complete subtree ✔

Why MutationObserver avoids the classic connectedCallback() / parsedCallback() trap: if someone writes const el = document.createElement('s-select'); document.body.append(el); el.append(option);, connectedCallback() fires synchronously on body.append(el) before option is attached. By contrast, a MutationObserver runs at the microtask checkpoint—after both synchronous append() calls have completed—unifying parser completion and dynamic DOM mutations.

1. Parse-End — Option A: Buffer Until </el>

observer.observe(document, { childList: true, subtree: true, waitForParseEnd: true // Hold parser insertions until </el> }); for (const r of records) for (const n of r.addedNodes) if (n instanceof Element && n.matches("s-select")) setup(n); // Full parsed subtree is already in the DOM

Option A: instead of introducing a new record shape, tell the MutationObserver to buffer parser-inserted elements until their closing tag is popped from the stack of open elements (and if observing a specific root element, buffer child mutations until that root closes). Pros: standard addedNodes loops work unchanged across single-chunk, multi-chunk, and dynamic appendChild. Trade-offs: on subtree: true, inner elements close and fire before outer ancestors (bottom-up order), and open containers aren't reported to the observer until they close.

1. Parse-End — Option B: isParsing + Completion Record

for (const r of records) { for (const n of r.addedNodes) if (!n.isParsing) setup(n); // Closed in same chunk if (r.type === "parseEnd") // (or empty childList on r.target) setup(r.target); // Closed in later chunk }

Option B: keep addedNodes firing on the start tag as today, expose element.isParsing (true while open on the parser stack, false once popped), and emit a completion record only when an element that was still open at the end of an earlier chunk closes in a later chunk. Pros: preserves top-down ordering and lets observers see open containers immediately. Trade-offs: callbacks need two branches to handle same-chunk addedNodes vs. cross-chunk completion.

1. Parse-End — Option C: record.parsedElements

observer.observe(document, { childList: true, subtree: true, parseEnd: true }); for (const r of records) for (const el of r.parsedElements) if (el.matches("s-select")) setup(el);

Option C: attach a parsedElements NodeList to MutationRecord listing elements whose closing tags were popped during that batch (or pair with a Custom Element parsedCallback()). Pros: explicit and doesn't change addedNodes timing. Trade-offs: allocates an extra list and separates parser completion from the addedNodes loop used for imperative DOM insertions.

2. Flow & Error Control (template: true)

const target = r.templateAttributes?.get("for"); const turn = r.templateAttributes?.get("data-turn"); // r.templateStatus: "start" | "update" | "end" | "abort"

For <template for> patches, MutationRecord carries templateAttributes (a shared ReadOnlyMap of the template's attributes) and templateStatus ("start", "update", "end", or "abort" on stream truncation / missing target). This also composes with buffering if an observer wants to receive the patch only when </template> finishes or aborts.

Discussion

Participate: WICG/declarative-partial-updates (parser-mutation-records-explainer.md · #110)

Open the floor for discussion on the three design options for surfacing parse-end (buffering addedNodes vs. isParsing + completion record vs. parsedElements) and <template for> lifecycle metadata.