Skip to content

Fix steaming issues with suspence - #5407

Open
MaxwellCohen wants to merge 17 commits into
preactjs:mainfrom
MaxwellCohen:dpu-boundary-hydration
Open

MaxwellCohen wants to merge 17 commits into
preactjs:mainfrom
MaxwellCohen:dpu-boundary-hydration

Conversation

@MaxwellCohen

@MaxwellCohen MaxwellCohen commented Nov 22, 2026 •

Copy link
Copy Markdown

This is the Preact client (core and compact) side of the suspense streaming fixes. I got these fixes from using an LLM-generated streaming harness (source). This harness found 10 categories of bugs, and the core fixes help fix 6 of them.

Issue Harness tests client (this pr) server with <preact-island> (backport fixes) preactjs/preact-render-to-string#508 server with <template for> (move to DPU) preactjs/preact-render-to-string#509
A component that suspends while hydrating takes the wrong server nodes: siblings adopt the leftovers, nodes are duplicated or end up out of order lazyFirst, multiRootClient, lazyMulti, lazyNull, realLazyNoStream, twoInOne, wrapFirstServer, lazySecond, realLazy, lazyDeep, lazyRef, wrapMidClient, serverWaterfall, lazyInDiv, textLazy, resuspend Core _claim hook; the boundary takes its range; a suspend goes up to the boundary; the partial render is thrown away; the server DOM stays instead of the fallback; markers are removed on commit; refQueue rollback Marks every boundary $s=; suspensions go to the boundary Same
Nested or neighboring boundaries take each other's ranges innerPlain, outerNoRangeLazy, outerNoRange, rootBoundary, emptyPair, gateAboveNoWrap, sameTag, siblings, nest3, directNest, gateAbove The boundary takes only its own range; rangeEnd finds a range's end while a chunk is still streaming $s= opening marker; a pending range's closing marker carries its id <?start>/<?end> inside the range
The page changes while chunks are still streaming in: the client hides a boundary, re-renders or suspends again, and the late chunk overwrites it or the client overwrites the chunk hideNested, laterNested, tickProp, hideOnly, tickAttr, later A pending boundary waits for its chunk; a parent re-render doesn't start hydration early; unmount leaves the markers for the server's script; attributes update after a re-render The script drops ranges the client unmounted, wakes the waiting boundary, and hands what's left to the client when the response ends Same, in $PRS()
A keyed list is reordered while its boundaries still wait keyed, keyedComp, keyedGrow, keyedNoStream A waiting boundary isn't treated as new, and its whole range moves with it — —
SVG and MathML content streamed into a boundary ends up in the wrong namespace svgTitle, math, svgImage, svgForeign, svg — The script re-parses the content in the parent's namespace Same
A boundary fails on the server after its fallback has already streamed serverErrorLate Hydrates the $s! fallback, then renders the content on the client Hands the range to the client data-x, or a lost template
Raw HTML containing </body> splits the shell in the wrong place bodyInShell — Only a closing </body> at the end counts Same
useId gives the wrong IDs after a render that's thrown away, so a label points at the wrong input useId Restarts the counter on retry Same restart on the server (also #507) Same
A browser extension or another script adds nodes between a chunk's parts extNode, extNodeNested, foreignScript, foreignScriptNested — The island waits for its own empty comment, not any next node $PRS() finds the template by its id, not by position
The stream is cut off, or no server script reaches a pending range moduleHydrate, moduleHydrateCut A waiting boundary renders on the client once the document loads Cut-off islands are removed Same

This fix adds support for the server marking every boundary

I have tried to keep the changes as compact as possible

gzip main This PR
preact.mjs 4964 5130 (+166)
compat.mjs 4010 4409 (+399)
An app using preact/compat without Suspense 6873 7035 (+162)
An app using Suspense and lazy 7803 8314 (+511)

Note

Written with Claude Fable 5.2 and Claude Opus 5.5, which also wrote this description. A review pass with Grok 4.7 added the handling of markers that already shipped.

I hope this PR helps. If it doesn't help, feel free to close it or modify it in any way that helps improve Preact.

MaxwellCohen and others added 6 commits November 21, 2026 16:25
preact-render-to-string marks the range of every Suspense boundary:
`<!--$s=-->` when its content is in the HTML, `<!--$s?id-->` while the
server is still to stream it, `<!--$s!-->` when the client has to
render it.

A boundary now hydrates that range and nothing else, and only once all
of it is there. While it waits (on its chunk, or on a lazy component
below it) the range stays exactly as the server sent it. When something
suspends below a boundary that is hydrating, the boundary keeps the
whole range and hydrates it from scratch later, instead of the
suspending component keeping a guess at its own nodes.

A component that is still to hydrate now moves with its DOM when a
keyed list reorders.

Co-Authored-By: Claude Fable 5.2
Core keeps one hook (options._claim) that lets a component take a range of
the DOM it hydrates, plus the generic pieces: the streaming-aware rangeEnd,
moving a suspended component's range on keyed reorders, and diffing a
boundary's attributes when a parent rendered it again while it waited.
Finding the range, client-rendering a $s! range and removing the markers
now happen in compat, so they ship only with Suspense.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Roll back refQueue with commitQueue when a render is thrown away, so a
  callback ref runs once and its cleanup isn't lost
- Remove the DOM of a portal mounted by a thrown-away render
- Keep a parked pending boundary's opening marker for the chunk script:
  no copy that starts with it removes it
- Unmount clears a pending range only up to a closer that is a sibling
- The claim scan stops at any other boundary marker
- Guard the range move in insert() against a detached cursor
- Count a suspended vnode with no kept DOM as mounting for skew
- One render callback per boundary however often it retries

Co-Authored-By: Claude Opus 5.5
The claim scan has to step over the <!--$s--> pair renderToStringAsync
writes around a component that suspended, inside its boundary's range.

Co-Authored-By: Claude Opus 5.5
Revert rangeEnd to its original loop with only the pending-closer check,
find the closer in componentWillUnmount with rangeEnd, inline the render
callback, and drop the __w check in removeOriginal. Make the useId test
check the ids it renders, not the attribute hydration keeps.

Co-Authored-By: Claude Opus 5.5
Co-Authored-By: Claude Opus 5.5
@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

📊 Tachometer Benchmark Results

Summary

A summary of the benchmark results will show here once they finish.

Results

The full results of your benchmarks will show here once they finish.

tachometer-reporter-action v2 for CI

rangeEnd() skips a nested range by recursing instead of counting depth,
and the detached-cursor check is inlined, which gzips smaller than a
helper.

Co-Authored-By: Claude Opus 5.5
Co-Authored-By: Claude Opus 5.5
Core's unmount already removes a suspended component's range. Without
markers, the server script finds no range for a late chunk and drops it,
so Suspense no longer keeps them for the script.

Co-Authored-By: Claude Opus 5.5
…cache

A waiting boundary doesn't render again before its promise settles, so one
promise per wait is enough.

Co-Authored-By: Claude Opus 5.5
A boundary can render again while it waits (forceUpdate skips
shouldComponentUpdate, e.g. on a hot reload). A new promise would replace
__w and leave the first one unresolved, so the boundary never hydrated.

Co-Authored-By: Claude Opus 5.5
A boundary waiting for its chunk only woke when the server's inline script
called __w. Without that script (cut off, another version's, or a range it
can't reach), the boundary waited forever. Resolve the wait on
DOMContentLoaded too, so the range renders on the client.

Co-Authored-By: Claude Opus 5.5
- _claim returns the range's closer as the cursor, not the node after it
- discard the boundary's own vnode and restore its _parentDom
- the marker-removal callback reuses the render's marker and fallback
- check readyState by its first letter

Co-Authored-By: Claude Opus 5.5
Co-Authored-By: Claude Opus 5.5

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant