Repository navigation
fix(hydration): continue after a component adopted in place without a range - #1567
Conversation
… range A component call that finds no server range of its own, because the server rendered another @if/@switch arm there, adopts the server element at the cursor in place. Neither componentSlotImpl's singleRoot path nor componentSlotLite's no-range branch moved the cursor past that element, so the next component adopted the same element again and the server's range for it stayed on the page. When the first component's holes left the cursor inside the element, the next call claimed the enclosing block's range and discarded the arm. HydrationCapability.renderInPlace renders such a call while adopt() watches for its template adopting the node, then steps the cursor past it. The singleRoot path stamps the cursor as the block boundary only when the body adopted it (or threw), so a root rebuilt at a close marker no longer takes that marker as its own. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rwin-50350d Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Bugbot Autofix prepared a fix for the issue found in the latest run.
- ✅ Fixed: Null cursor re-adopts first child
- Added inPlaceExhausted tracking on HydrationCapability so that when renderInPlace advances the cursor to null (last child adopted), the getFirstChild fallback in componentSlotLite and componentSlotImpl is suppressed for that exhausted host, preventing re-adoption of the first child.
Or push these changes by commenting:
@cursor push 889cabcb2a
Preview (889cabcb2a)
diff --git a/packages/octane/src/runtime.ts b/packages/octane/src/runtime.ts
--- a/packages/octane/src/runtime.ts
+++ b/packages/octane/src/runtime.ts
@@ -11689,7 +11689,11 @@
// just-cloned (empty) host, so descend to host.firstChild; later siblings
// (and the sole-hole case) already have the cursor on the open marker.
let open: Node | null = hydration.node;
- if (open === null || (STAGED_DOM?.view(open) ?? open).parentNode !== host)
+ if (
+ open === null
+ ? !hydration.isInPlaceExhausted(host)
+ : (STAGED_DOM?.view(open) ?? open).parentNode !== host
+ )
open = getFirstChild(host);
if (open !== null && hydration.isOpen(open)) {
adoptedOpen = open;
@@ -18770,6 +18774,8 @@
private readonly unframedRootRanges = new WeakMap<Node, Node>();
/** The server node renderInPlace's body may adopt, until a template does. */
private inPlace: Node | null = null;
+ /** Parent whose children renderInPlace exhausted (cursor stepped to null). */
+ private inPlaceExhausted: Node | null = null;
/** Pairs discovered while matching an outer range; released with this hydration pass. */
private matchingCloses: WeakMap<Node, Comment> | null = null;
/** First unclaimed root sibling after a compiled root clone; undefined until known. */
@@ -19004,6 +19010,11 @@
}
}
+ /** Whether renderInPlace's last adoption exhausted the children of `host`. */
+ isInPlaceExhausted(host: Node): boolean {
+ return this.inPlaceExhausted === host;
+ }
+
/**
* First render, by `render(target)`, of a component call that found `root`
* at the cursor instead of a server range of its own. Its template adopts
@@ -19024,6 +19035,10 @@
this.inPlace = outer;
}
if (adopted) this.node = getNextSibling(root);
+ this.inPlaceExhausted =
+ adopted && this.node === null
+ ? ((STAGED_DOM?.view(root) ?? root).parentNode as Node | null)
+ : null;
return adopted;
}
@@ -31436,7 +31451,11 @@
// In both cases mountTry/renderBlock parked the cursor on the server range's
// `<!--[-->`; adopt from it, the same way childSlot's cursor branch does.
let c: Node | null = hydration.node;
- if (c === null || (STAGED_DOM?.view(c) ?? c).parentNode !== domParent)
+ if (
+ c === null
+ ? !hydration.isInPlaceExhausted(domParent)
+ : (STAGED_DOM?.view(c) ?? c).parentNode !== domParent
+ )
c = getFirstChild(domParent);
hydrationCursor = c;
if (c !== null && hydration.isOpen(c)) open = c;You can send follow-ups to the cloud agent here.
Reviewed by Cursor Bugbot for commit a4a8f37. Configure here.
Benchmark report
Compares js-framework: 1k/10k row operations (ms, lower is better)All 20 timed operations (20 within noise)
Application bundle size (bytes)
16 other measured values are unchanged. Public-import reachability bundles (bytes)
68 other measured values are unchanged. CI job durations
Compares the CI run for
All 26 timed jobs
|
…rwin-50350d Co-Authored-By: Claude Opus 5.5 <[email protected]> # Conflicts: # packages/octane/src/runtime.ts
…ifier The repository forbids skip/todo modifiers in committed tests, so the development-compile-only case moves into its own describe instead of `it.runIf(dev)`. Only the development compile renders that hookless call through the lite slot. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ition #1567's renderInPlace steps the cursor past a node that an unframed component adopted in place, in both the lite and the singleRoot paths. It supersedes this branch's stepPastRoot and its lite-path rebuiltTail clear: an adopted node no longer leaves the cursor on the rebuilt tail, so the sweep cannot take it. The singleRoot boundary stamps the cursor only when the body adopted it or threw (#1567), no root was rebuilt in its place, and it is not a closing marker. The fallback still finds a root rebuilt in the cursor's place before probing the anchor. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve runtime.ts conflicts with #1567: keep both the in-place adoption (renderInPlace, the inPlace check in adopt()) and the component-range claims (beginClaim/endClaim/renderClaimed/settleClaim, the claimRoots check in adopt()). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve runtime.ts against #1561, #1567 and #1576: keep main's adopted-range claims (renderClaimed) and in-place lite rendering (renderInPlace) beside the unframed-claim changes. #1567 renders a lite call that found no range against the cursor, so a hookless component that renders nothing leaves the server element to the next call. Drop this branch's routing of such calls through the missing-range path, which contradicted that. Fix the over-sweep where it happens instead: when such a call rebuilds its fragment, placeRebuilt now discards only the server content in its place, up to the next marker, and drainFrag inserts the fragment there, ahead of later siblings' ranges. The development diagnostic names the call site. renderUnframed shares the same bounded sweep (discardInPlace). The fixture's FragChild gets a hook so it is a full component call in both compiles, which is the path that case covers. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LqA51dmsggKyu7uH3zc9N2
…er its last root (#1597) * fix(hydration): continue after a component adopted in place without a range A component call that finds no server range of its own, because the server rendered another @if/@switch arm there, adopts the server element at the cursor in place. Neither componentSlotImpl's singleRoot path nor componentSlotLite's no-range branch moved the cursor past that element, so the next component adopted the same element again and the server's range for it stayed on the page. When the first component's holes left the cursor inside the element, the next call claimed the enclosing block's range and discarded the arm. HydrationCapability.renderInPlace renders such a call while adopt() watches for its template adopting the node, then steps the cursor past it. The singleRoot path stamps the cursor as the block boundary only when the body adopted it (or threw), so a root rebuilt at a close marker no longer takes that marker as its own. Co-Authored-By: Claude Opus 5.5 <[email protected]> * test(hydration): run the lite empty-component case without a test modifier The repository forbids skip/todo modifiers in committed tests, so the development-compile-only case moves into its own describe instead of `it.runIf(dev)`. Only the development compile renders that hookless call through the lite slot. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(hydration): continue after an unframed fragment component root A component call that finds no server range of its own, because the server rendered another @if arm inline there, renders in place of the server node at the cursor (renderInPlace). A template with several roots now compares every root there (fragmentRemainder) and, when they match, adopts them and moves the cursor past the last one, so the next call adopts its own server range. - Development compile (lite slot): the next call no longer reads the fragment's first root, removes it, and renders itself twice. - Production compile (renderUnframed): the body now renders in place of an element or text node. A matching template is adopted and framed by the fresh markers. A template that does not match is rebuilt without discarding anything until the body has run, so a seeded rejection still leaves the server catch arm intact. The stale sweep no longer removes a later sibling's server range or leaves its close marker behind. - adopt() tests one `claiming` flag on its common path in place of the root-remainder check, so the in-place claims add no work to framed clones. Stacked on #1567 (renderInPlace). Co-Authored-By: Claude Opus 5.5 <[email protected]> * docs(changeset): describe the unframed fragment fix against current main Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(octane): keep renderBlock's in-place exemption inside one hydration call renderBlock ships in every bundle, so the second call it made for the fresh-frame exemption pushed root-chained-jsx 2 bytes over its committed budget. HydrationCapability.rebuilds(end) now answers both questions at the one call site isFresh(end) used to fill. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(hydration): end an in-place fragment's server nodes at the enclosing range A fragment template adopted in place of a server node compared its roots against the server nodes after that node. A hole skips the shape check, so when the server's range ended where a hole was expected, the walk stepped past the range's close: the fragment matched, its end landed outside the @if arm, and production moved the fresh end marker out of the arm. The in-place walk now fails at the enclosing range's close. A fragment that does not match where it stands is rebuilt in both compiles: the lite path rebuilds it at once (rebuildFragment) instead of adopting its first root, whose holes then claimed the wrong server nodes. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>


Summary
During hydration, a component call that finds no server range of its own adopts the server element at the cursor in place. This happens when the server rendered a different
@if/@switcharm there, or when a component returns a single-root component. The production compile reaches it throughcomponentSlotImpl's singleRoot path, and the development compile throughcomponentSlotLite's no-range branch. Neither moved the hydration cursor past the adopted element:<><Leaf v="x" /><Leaf v="z" /></>hydrated over the server arm<em>x</em><!--[--><em>z</em><!--]-->left<em>z</em><!--[--><em>z</em><!--]-->. BothLeafblocks shared the first<em>, and the server's range for the second stayed on the page. Later updates to the secondLeafwrote into the first element.@ifinside<em>), the null cursor sent the next call tohost.firstChild, which is the enclosing@if's open marker. The next call claimed that range, and the whole arm disappeared.NotFoundError.Changes, all in
packages/octane/src/runtime.ts:HydrationCapability.renderInPlace(render, target, root)renders such a call whileadopt()watches whether a template adoptsroot. The watch is a privateinPlacefield. It is saved and restored for nested calls and cleared onadopt()'s in-place return. When the body adoptedroot, the cursor steps past it, wherever the body's holes left it. A body that rebuiltrootor rendered nothing there leaves the cursor where it put it. "Root still attached after the body" would not work as the signal: an empty lite body leaves the root attached without adopting it.componentSlotImpl's singleRoot path renders throughrenderInPlacewhile hydrating. It stamps the cursor as the block boundary only when the body adopted it or threw, which keeps the existing behavior for a render that suspends. Otherwise the client-mount probe finds the root the body inserted.componentSlotLite's no-range branch re-enters through a coldrenderLiteInPlacehelper. This keeps the helper's closure out of ordinary lite dispatch, assuspendFreshLiteComponentdoes.Validation
pnpm format:files:checkon the changed files; the repo-widepnpm format:checkwas not run.pnpm typecheck:filesonruntime.tsand the new test and fixture; the repo-widepnpm typecheckwas not run.pnpm test: not run. I stopped a full localoctanerun because the base had moved, and I'm rerunningoctaneandoctane-prodon this head. CI covers the other projects.New
tests/hydration/in-place-component-sibling.test.ts, covering the development and production compiles:@ifarm and a@switcharm;@ifleaves the cursor inside it;The tests assert final markup, adopted-node identity, updates after hydration, and recoverable errors and dev diagnostics. All 11 fail on
main. Three partial fixes each fail at least one case: stepping only while the cursor sits on the root, treating "root still attached" as adoption, and keeping the old stamp condition.octaneandoctane-prodprojects,tests/hydration/**plusnon-jsx-return.test.tson the merged head: 246 files, 12,158 tests passed.node benchmarks/bundle-size/run-minimal.mjs, before and after; budgets pass):Risk / follow-ups
This overlaps open PRs:
stepPastRoot, which steps only whilethis.node === root, so it misses the case where holes move the cursor inside the root. ItsfreshAfterprobe edits the samefinallyblock.Whichever PR lands second should fold in the other; the logic composes. fix(hydration): discard the server content an adopted @if/@switch arm leaves unclaimed #1565's
renderClaimedcomment that an in-place slot "parks the cursor on that node" would become stale.A lite body whose template has several roots, adopted without a range, still does not advance the cursor, because its extent is unknown without a root count. This is unchanged from
main.A separate pre-existing defect showed up while probing. In the production compile, a non-single-root component without a range (
renderUnframed) discards a later sibling's server range and leaves that range's<!--]-->behind. Another session is fixing it.Provenance
agent-authored)🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.Note
Medium Risk
Touches core hydration and component-slot paths in runtime.ts; behavior changes affect DOM adoption, cursor advancement, and block boundary stamping on first hydrate.
Overview
Fixes hydration cursor desync when a component call has no server range of its own and adopts the element at the cursor in place (typical when client and server disagree on an
@if/@switcharm, or for returned single-root components).Adds
HydrationCapability.renderInPlace, which tracks whether the body actually adopted the server node; when it did, the cursor steps past that element so the next sibling claims its own range instead of reusing the same DOM or swallowing the parent block.componentSlotLiteroutes anchored no-range first renders throughrenderLiteInPlace;componentSlotImpl’s hydrating single-root path uses the same helper and only stamps the adopted cursor as block boundaries when adoption (or a throw) happened—so toggling a rebuilt single-root child off/on no longer leaves stray nodes orNotFoundError.New hydration tests (dev + prod compiles) cover
@if/@switcharms, nested holes inside the adopted root, mismatched following siblings, empty lite components, and conditional returned roots.Reviewed by Cursor Bugbot for commit cff9e1c. Bugbot is set up for automated code reviews on this repo. Configure here.