Resume
← Blog

2026.11.24 · 5 min · html · browsers · javascript · ai

Stop rebuilding your AI answer on every chunk.

Chat UIs stream an answer with innerHTML += chunk, which re-parses everything on screen each time. Chrome 157 ships el.streamAppendHTML(): pipe the response into it and the browser's own HTML parser appends each chunk, sanitized by default. The pipe chain, the catches, and where it works. As of 2026.11.

Every chat UI streams its answer token by token, and the usual way to show it is el.innerHTML += chunk. That line throws away the whole answer and rebuilds it on every chunk. Chrome 157, stable since 2026.11.03, ships a native alternative: el.streamAppendHTML(). One line, no library, and the safe version sanitizes by default.

Watch the 60-second version on Instagram.

Why innerHTML += is the slow way

It looks like appending. It isn't. += reads the whole HTML string back out, adds the chunk, then destroys every node and parses the lot again. In Chrome's own words, "each time a new chunk is added, the entire set of previous chunks plus the new chunk need to be re-parsed as HTML", and then re-rendered.

el.innerHTML += chunkPARSED515305075WHOLE ANSWER, EVERY CHUNKel.streamAppendHTML()PARSED510152025NEW CHUNK ONLYFIG. 01 / RE-PARSED EVERY CHUNK

The units in the figure are illustrative: five equal chunks of 5. With += the parsed total climbs 5, 15, 30, 50, 75; appended, it's 5, 10, 15, 20, 25. Total work with += grows with the square of the answer's length. That's my own arithmetic from Chrome's description, not a quoted figure.

The cost isn't only time. Because every node is rebuilt, state on those nodes goes too: an open <details> snaps shut, a selection disappears. And a chunk can end mid-tag, like <a hr. By the HTML tokenizer's end-of-file rules, the cut-off tag is dropped rather than shown as text, then reappears with the next chunk, and open elements get closed and rebuilt. So what you see is popping and flicker plus lost state, not raw <a hr on screen. (That last part follows from the spec rules; it wasn't tested.)

The fix: pipe the stream into the element

streamAppendHTML() returns a WritableStream that takes text. Pipe the response into it and the browser's real HTML parser builds nodes as chunks arrive, without re-parsing what's already on screen. A tag split across two chunks is held by the parser until it completes.

answer.js
const res = await fetch("/api/answer");          // server streams HTML text
await res.body
  .pipeThrough(new TextDecoderStream())          // bytes -> text
  .pipeTo(answerEl.streamAppendHTML());          // sanitized by default
res.bodyFETCHBYTESTextDecoderStreamTEXTanswerEl.streamAppendHTML()OLD NODES STAY PUTFIG. 02 / FETCH, DECODE, APPEND

The pipe chain is Chrome's own pattern (their docs show it with streamHTMLUnsafe). Don't skip TextDecoderStream: without it you write bytes, and the page shows 60,112,62. You may also see res.textStream() as a shorter form; its stable status is unconfirmed, so I'm not relying on it.

streamAppendHTML is one of a family, stream{Append}HTML{Unsafe}: streamHTML, streamHTMLUnsafe, streamAppendHTML, streamAppendHTMLUnsafe, plus prepend, before, after and replaceWith variants. Each returns a WritableStream. The signature is streamAppendHTML(options); on the safe variants the sanitizer option defaults to "default". Non-streaming siblings such as appendHTML() ship in 157 too.

A stop button is one option: pass { signal } to pipeTo. It rejects with AbortError and keeps the partial answer on screen.

What people get wrong

  • "It renders Markdown." It parses HTML only, and most LLMs answer in Markdown, which would show up as plain text. Have the backend ask the model for HTML, or convert the Markdown to HTML before it streams.
  • "The default sanitizer keeps my markup." In azukiazusa's tests it strips <img>, <details>, <summary>, class, id and style (not checked against the spec). To allow more, pass { sanitizer: new Sanitizer({ elements: […] }) }.
  • "I'll use the Unsafe variant, scripts don't run." runScripts defaults to false, but inline handlers like onerror still run. Don't use Unsafe for LLM output.
  • "The polyfill streams." GoogleChromeLabs/html-setters-polyfill buffers the HTML and applies it once the stream ends.

Where it works

Status (2026.11)
Chrome157, on by default, no flag (desktop, Android, WebView). Dev trial behind #enable-experimental-web-platform-features from 148
EdgeMirrors Chromium in browser-compat-data; Edge 157 itself unconfirmed
FirefoxNot implemented; Mozilla's position is positive
SafariNot implemented; WebKit: no signal

It's in the WHATWG HTML spec, in the dynamic markup insertion section, and still marked experimental. Older sources say it ships in 152 or 155; both are stale. Feature-detect and keep a fallback:

detect.js
if ("streamAppendHTML" in Element.prototype) {
  // pipe into answerEl.streamAppendHTML()
} else {
  // your current path
}

People have asked for this for a long time: whatwg/html issue #2142, "a way to stream content into an element", dates from 2016.

On a client project I'd ship the pipe behind that feature check today and keep the existing renderer as the fallback, because Chrome users get the win now and nobody else loses anything.

The short version

  1. innerHTML += chunk re-parses and rebuilds the whole answer on every chunk, and loses state like an open <details>.
  2. el.streamAppendHTML() returns a WritableStream; pipe res.body through TextDecoderStream into it and only new chunks get parsed.
  3. It parses HTML, not Markdown, so the server must send HTML.
  4. The safe variant sanitizes by default and strips more than you might expect; never use Unsafe for LLM output.
  5. Chrome 157+ only. Detect with "streamAppendHTML" in Element.prototype and fall back.

Sources: Chrome Platform Status, Renewed HTML insertion & streaming methods · Chrome 157 release notes · MDN browser-compat-data, Element.json · Chromium Dash, M157 schedule · blink-dev, Ready for Developer Testing · Chrome for Developers, Declarative partial updates · WICG explainer, Resulting API · Chrome for Developers, Render streamed LLM responses · azukiazusa, stream-append-html · whatwg/html #2142 · Hacker News · blink-dev, Intent to Ship