Skip to content

Commit 889cabc

Browse files
committed
fix(hydration): prevent null cursor from re-adopting first child after in-place exhaustion
When renderInPlace adopts a node that is the last child of its host, the cursor advances to null (getNextSibling returns null). The next sibling component in the same host previously treated null as 'cursor parked outside this host' and fell back to getFirstChild, wrongly re-adopting the first child element a second time. Track which parent was exhausted by renderInPlace (inPlaceExhausted field on HydrationCapability). When the cursor is null, the getFirstChild fallback in both componentSlotLite and componentSlotImpl now checks isInPlaceExhausted(host) and skips the fallback when the host's children were already consumed by a prior in-place adoption.
1 parent a4a8f37 commit 889cabc

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

‎packages/octane/src/runtime.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11689,7 +11689,11 @@ export function componentSlotLite<P>(
1168911689
// just-cloned (empty) host, so descend to host.firstChild; later siblings
1169011690
// (and the sole-hole case) already have the cursor on the open marker.
1169111691
let open: Node | null = hydration.node;
11692-
if (open === null || (STAGED_DOM?.view(open) ?? open).parentNode !== host)
11692+
if (
11693+
open === null
11694+
? !hydration.isInPlaceExhausted(host)
11695+
: (STAGED_DOM?.view(open) ?? open).parentNode !== host
11696+
)
1169311697
open = getFirstChild(host);
1169411698
if (open !== null && hydration.isOpen(open)) {
1169511699
adoptedOpen = open;
@@ -18770,6 +18774,8 @@ class HydrationCapability {
1877018774
private readonly unframedRootRanges = new WeakMap<Node, Node>();
1877118775
/** The server node renderInPlace's body may adopt, until a template does. */
1877218776
private inPlace: Node | null = null;
18777+
/** Parent whose children renderInPlace exhausted (cursor stepped to null). */
18778+
private inPlaceExhausted: Node | null = null;
1877318779
/** Pairs discovered while matching an outer range; released with this hydration pass. */
1877418780
private matchingCloses: WeakMap<Node, Comment> | null = null;
1877518781
/** First unclaimed root sibling after a compiled root clone; undefined until known. */
@@ -19004,6 +19010,11 @@ class HydrationCapability {
1900419010
}
1900519011
}
1900619012

19013+
/** Whether renderInPlace's last adoption exhausted the children of `host`. */
19014+
isInPlaceExhausted(host: Node): boolean {
19015+
return this.inPlaceExhausted === host;
19016+
}
19017+
1900719018
/**
1900819019
* First render, by `render(target)`, of a component call that found `root`
1900919020
* at the cursor instead of a server range of its own. Its template adopts
@@ -19024,6 +19035,10 @@ class HydrationCapability {
1902419035
this.inPlace = outer;
1902519036
}
1902619037
if (adopted) this.node = getNextSibling(root);
19038+
this.inPlaceExhausted =
19039+
adopted && this.node === null
19040+
? ((STAGED_DOM?.view(root) ?? root).parentNode as Node | null)
19041+
: null;
1902719042
return adopted;
1902819043
}
1902919044

@@ -31436,7 +31451,11 @@ function componentSlotImpl(
3143631451
// In both cases mountTry/renderBlock parked the cursor on the server range's
3143731452
// `<!--[-->`; adopt from it, the same way childSlot's cursor branch does.
3143831453
let c: Node | null = hydration.node;
31439-
if (c === null || (STAGED_DOM?.view(c) ?? c).parentNode !== domParent)
31454+
if (
31455+
c === null
31456+
? !hydration.isInPlaceExhausted(domParent)
31457+
: (STAGED_DOM?.view(c) ?? c).parentNode !== domParent
31458+
)
3144031459
c = getFirstChild(domParent);
3144131460
hydrationCursor = c;
3144231461
if (c !== null && hydration.isOpen(c)) open = c;

0 commit comments

Comments
 (0)