Skip to content

Add template for dpu support after PR #508 - #509

Open
MaxwellCohen wants to merge 28 commits into
preactjs:mainfrom
MaxwellCohen:dpu-boundary-streaming
Open

MaxwellCohen wants to merge 28 commits into
preactjs:mainfrom
MaxwellCohen:dpu-boundary-streaming

Conversation

@MaxwellCohen

@MaxwellCohen MaxwellCohen commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

This is a stacked PR for #508.
It adds a back template for support.

This is the Preact server-side of the suspense streaming fixes. I got these fixes from using an LLM-generated streaming harness (source).

With this pr adds <template for> steaming

Issue Harness tests client (preactjs/preact#5407) server with <preact-island> (#508) server with <template for> (#508 + #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 (#507) Same

The boundaries are updated to the following from #509

Key changes in this pr:

Boundary state main #509
Rendered on the server no marker at all <!--$s=-->…<!--/$s-->
Pending (still streaming) <!--$s:ID-->fallback<!--/$s:ID--> <!--$s?ID--><?start name="ID">fallback<?end><!--/$s?ID-->
Errored, left to the client <!--$s!:ID-->fallback<!--/$s:ID--> <!--$s!-->fallback<!--/$s--> (no id)
Suspended in renderToStringAsync <!--$s-->…<!--/$s--> unchanged
ID the vnode id in decimal the vnode id in base 36
Content <div hidden> wrapping <preact-island hidden data-target=ID>…</preact-island> <template for="ID">…</template><script data-p="ID">$PRS()</script>, with no <div hidden> wrapper
SVG/MathML parent not handled the content is wrapped in <svg>/<math> and the script gets data-s
Leave to the client an inline script searches for $s:ID and renames it to $s!:ID <script data-p="ID" data-x>$PRS()</script>

JS size changes

main #508 #509
Init script, minified 1,135 B 859 B 659 B
Init script, gzip 589 B 509 B 419 B (−170 B against main, −90 B against #508)
Script per chunk none (<preact-island>) none (<preact-island>…<!---->) <script data-p="id">$PRS()</script>

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 7 commits November 21, 2026 19:54
Every Suspense boundary writes a marked range: `<!--$s=-->` around
content that is in the HTML, `<!--$s?id-->` around the fallback of a
boundary that is still to be streamed, `<!--$s!-->` around a fallback
that is left to the client.

A streamed boundary's content follows in `<preact-island hidden
data-target="id">`, as before, and an empty comment. The island moves
its content into the range once that comment has arrived, then tells
hydration the content is there. An island cut off by the end of the
response hands its range to the client.

While streaming, the boundary itself catches what suspends or fails
below it, so its range covers all of its children and a nested boundary
is never sent before the HTML that holds its range.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Browsers with Declarative Partial Updates stream a boundary's content
into its range while they parse the template. The `$PRS` init script
patches it in elsewhere, and does what the island did: wake the
boundary, reparse SVG and MathML, drop ranges the client unmounted, and
hand ranges to the client when the server gives up or the response ends.

A pending range holds `<?start>`/`<?end>` around its fallback, which
mark what the template replaces.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	src/lib/client.js
#	test/client.test.jsx
Co-Authored-By: Claude Opus 5.5
Co-Authored-By: Claude Opus 5.5
…dpu-boundary-streaming

# Conflicts:
#	src/lib/chunked.js
#	src/lib/client.js
@changeset-bot

changeset-bot Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e004fdb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
preact-render-to-string Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@MaxwellCohen MaxwellCohen changed the title Dpu boundary streaming Add template for dpu support after PR #508 Nov 22, 2026
@MaxwellCohen
MaxwellCohen marked this pull request as ready for review November 22, 2026 17:53
…eaming

# Conflicts:
#	src/lib/chunked.js
#	test/compat/render-chunked.test.jsx
Co-Authored-By: Claude Opus 5.5
Create the comment iterator up front and find a range's closer through
the marker map in one helper.

Co-Authored-By: Claude Opus 5.5
The server wraps a boundary's content in <svg> or <math> when its parent
is in that namespace, so the parser creates the right elements. The init
script unwraps it instead of reparsing the range.

Co-Authored-By: Claude Opus 5.5
# Conflicts:
#	src/lib/chunked.js
#	src/lib/client.js
#	test/client.test.jsx
…andling

The client now removes the range of a pending boundary it unmounts, so the
script no longer needs to drop ranges marked with __w = 0. Every range
still pending when the document loads becomes $s!.

Co-Authored-By: Claude Opus 5.5
# Conflicts:
#	src/lib/client.js
#	test/client.test.jsx
Without reparsing, the moved nodes are the same ones the document's
iterator already indexed, so the TreeWalker pass and the first-copy-wins
check go.

Co-Authored-By: Claude Opus 5.5
Per-response ids starting at 0 collided when two streamed renders end up
in one document.

Co-Authored-By: Claude Sonnet 5.5
# Conflicts:
#	test/compat/recoverable.test.jsx
#	test/compat/render-chunked.test.jsx
The vnode id keeps boundaries apart within a process; a random prefix keeps
them apart from another server's response in the same document. Base 36
keeps them short.

Co-Authored-By: Claude Opus 5.5
An island now completes only once the empty comment the server writes after
it arrives, so a node an extension adds after it can't make it patch half a
chunk. On DOMContentLoaded the script only drops cut-off islands: compat now
ends the wait for pending ranges itself. annotation-xml switches back to
HTML only with an HTML encoding.

Co-Authored-By: Claude Opus 5.5
Drop the DOMContentLoaded handler: compat ends the wait for pending ranges
itself.

Co-Authored-By: Claude Opus 5.5
$PRS took the template only from right before its script, so a node added
in between (by a browser extension) left the range to the client and the
template behind.

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