Sub topic 11
Rendering partial output in the UI
This chapter assumes you can already read deltas from a stream in the browser (the previous chapters) and that you know basic DOM APIs. It covers what to do with each piece of text once it arrives.
Append to state, then draw
Keep one string for the message so far. Each delta is appended to it, and the UI shows the whole string. Don't insert each delta as its own DOM node. A model can emit dozens of deltas per second, and per-delta DOM work makes the page stutter.
Instead, batch the drawing. Deltas update the string immediately, and the screen updates at most once per animation frame.
A minimal renderer
function createRenderer(element) {
let text = "";
let scheduled = false;
function flush() {
scheduled = false;
const stickToBottom =
element.scrollHeight - element.scrollTop - element.clientHeight < 80;
element.textContent = text;
if (stickToBottom) element.scrollTop = element.scrollHeight;
}
return {
append(delta) {
text += delta;
if (!scheduled) {
scheduled = true;
requestAnimationFrame(flush);
}
},
finish() {
flush();
return text;
},
};
}Use it as the onData callback from the fetch reader: call renderer.append(delta) for each delta and renderer.finish() when the stream ends or is cancelled.
Decisions that matter
- Use
textContent, notinnerHTML. Model output is untrusted. It can echo attacker text from a web page or document you gave it. Assigning it toinnerHTMLallows script injection. - Scroll only if the user is at the bottom. The renderer measures the distance from the bottom before it updates. If the user has scrolled up to read, it leaves them there. Forcing the view down on every frame makes reading earlier text impossible.
- Render the final text once more. Call
finish()so that the last frame is always drawn, even if the tab was in the background and frames were paused.
Partial Markdown
Plain text is simple. Markdown is harder because a partial string is often invalid. A message that has received ` `py but not the closing fence is an unclosed code block. A half-written **bold` has no closing marker.
Common approaches:
- Show plain text while streaming and render Markdown once at the end. This is simple, but the layout jumps when the final render happens.
- Re-render the full Markdown string each frame. Most parsers tolerate unclosed constructs by treating the rest as part of the block. This costs more CPU on long messages.
- Use a streaming-aware Markdown renderer that closes open constructs for display.
If you convert Markdown to HTML, sanitize the HTML with a library such as DOMPurify before inserting it.
Interface state
Track a status with three values: streaming, done, and error. While streaming, show a stop button and disable the send button. On done, restore the controls. On error, keep the partial text visible and add a retry option, because the user can still use what arrived.
Exercise
A streamed answer is drawn with element.innerHTML = text on every delta. Name two problems this causes and one fix for each.
Show answer
Problem 1: it is a security risk, because model output is untrusted and can inject script. Fix: use textContent, or sanitize any HTML first. Problem 2: it redraws the DOM for every delta, which causes stutter at high token rates. Fix: append to a string buffer and write it to the DOM once per animation frame with requestAnimationFrame.