Skip to content

Commit d3f10b4

Browse files
committed
fix(hydration): clear inPlace for fragment bodies so parkPast records the arm tail
A fragment body adopted in renderInPlace never cleared the inPlace sentinel, so renderInPlace returned false, parkPast never ran, and renderAdoptedArm returned null for the parked cursor. discardArmTail then kept the extra server nodes from a longer server arm on screen. Clear inPlace in the fragment match path of adopt(), add parkHere() for callers that only need to record armTail at the cursor renderInPlace already parked, and advance the cursor past all fragment roots (including multi-root fragments via inPlacePark).
1 parent 2828027 commit d3f10b4

3 files changed

Lines changed: 81 additions & 3 deletions

File tree

‎packages/octane/src/runtime.ts‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11888,7 +11888,7 @@ function renderLiteInPlace<P>(
1188811888
root,
1188911889
)
1189011890
)
11891-
hydration.parkPast(root, parentScope);
11891+
hydration.parkHere(parentScope);
1189211892
}
1189311893

1189411894
// Keep the fresh-subtree callback's extra captures out of ordinary lite dispatch.
@@ -18831,6 +18831,12 @@ class HydrationCapability {
1883118831
private readonly unframedRootRanges = new WeakMap<Node, Node>();
1883218832
/** The server node renderInPlace's body may adopt, until a template does. */
1883318833
private inPlace: Node | null = null;
18834+
/**
18835+
* When a fragment body clears `inPlace`, the close of its last root's range
18836+
* (or the last root itself when it is not a range). renderInPlace steps past
18837+
* it so the cursor lands after all fragment roots, not just the first.
18838+
*/
18839+
private inPlacePark: Node | null = null;
1883418840
/** Pairs discovered while matching an outer range; released with this hydration pass. */
1883518841
private matchingCloses: WeakMap<Node, Comment> | null = null;
1883618842
/** First unclaimed root sibling after a compiled root clone; undefined until known. */
@@ -19210,14 +19216,19 @@ class HydrationCapability {
1921019216
renderInPlace<T>(render: (target: T) => void, target: T, root: Node): boolean {
1921119217
const outer = this.inPlace;
1921219218
this.inPlace = root;
19219+
this.inPlacePark = null;
1921319220
let adopted = false;
1921419221
try {
1921519222
render(target);
1921619223
adopted = this.inPlace === null;
1921719224
} finally {
1921819225
this.inPlace = outer;
1921919226
}
19220-
if (adopted) this.node = getNextSibling(root);
19227+
if (adopted) {
19228+
const last = this.inPlacePark;
19229+
this.inPlacePark = null;
19230+
this.node = last !== null ? getNextSibling(last) : getNextSibling(root);
19231+
}
1922119232
return adopted;
1922219233
}
1922319234

@@ -19676,6 +19687,19 @@ class HydrationCapability {
1967619687
}
1967719688
}
1967819689

19690+
/**
19691+
* Record the arm tracking at the current cursor, which renderInPlace already
19692+
* parked past the adopted content. Unlike parkPast this does not move the
19693+
* cursor: renderInPlace already advanced it past all roots (including the
19694+
* extra roots of a fragment body).
19695+
*/
19696+
parkHere(parent: Scope): void {
19697+
if (parent === this.arm) {
19698+
this.armTail = this.node;
19699+
this.armSlots = parent.slots.length;
19700+
}
19701+
}
19702+
1967919703
/**
1968019704
* First render of an @if or @switch arm into the server range it adopted,
1968119705
* `block`'s own. Returns the node from which whatever the server rendered
@@ -20298,6 +20322,19 @@ class HydrationCapability {
2029820322
: lazyFragmentRootMatches(cursor, lazy!, this))
2029920323
) {
2030020324
if (cursor === this.claimFrom) this.claimRoots(cursor, template ?? lazy!);
20325+
if (cursor === this.inPlace) {
20326+
this.inPlace = null;
20327+
const count = templateRootCount(template ?? lazy!);
20328+
if (count > 1) {
20329+
let node: Node | null = cursor;
20330+
for (let i = count; i > 1; i--) {
20331+
node = this.sibling(node!, 1);
20332+
if (node === null || isBlockClose(node)) break;
20333+
}
20334+
if (node !== null && !isBlockClose(node))
20335+
this.inPlacePark = this.isOpen(node) ? this.close(node) : node;
20336+
}
20337+
}
2030120338
return { __oct_vfrag: true, firstChild: cursor } as unknown as Node;
2030220339
}
2030320340
return this.rebuildFragment(template ?? resolveLazyTemplate(lazy!), cursor, loc);
@@ -32413,7 +32450,7 @@ function componentSlotImpl(
3241332450
}
3241432451
}
3241532452
// An arm that ends with this root cannot read its end off the cursor.
32416-
if (adopted) hydration!.parkPast(hydrationCursor!, parentScope);
32453+
if (adopted) hydration!.parkHere(parentScope);
3241732454
} else {
3241832455
const b = createBlock(
3241932456
'dynamic',

‎packages/octane/tests/hydration/_fixtures/branch-arm-tail.tsrx‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,33 @@ export function IfLaterHost(props: { on: boolean }) @{
237237
</div>
238238
}
239239

240+
// A hookless component whose body is a fragment (not a single host element).
241+
function FragLeaf(props: { v: string }) @{
242+
<>
243+
{props.v as string}
244+
</>
245+
}
246+
247+
// The arm's second component returns a fragment body, which finds no server
248+
// range and adopts the text node there in place. The server's arm has another
249+
// element after it that must be discarded.
250+
export function IfLaterFragment(props: { on: boolean }) @{
251+
<div id="r">
252+
@if (props.on) {
253+
<>
254+
<Leaf v="x" />
255+
<FragLeaf v="y" />
256+
</>
257+
} @else {
258+
<>
259+
<Leaf v="x" />
260+
y
261+
<b>{'z' as string}</b>
262+
</>
263+
}
264+
</div>
265+
}
266+
240267
// A component in a hole of the arm's own template finds no server range and
241268
// adopts the element there in place. The template already recorded where the
242269
// arm ends, after its last root.

‎packages/octane/tests/hydration/branch-arm-tail.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,19 @@ describe.each([
254254
expect(markup(s.host)).toBe('<em>x</em><b>b</b>');
255255
});
256256

257+
it('discards the server tail after a fragment body adopted in place and reports it', async () => {
258+
const s = await hydrate('IfLaterFragment', { on: false }, { on: true });
259+
260+
expect(container.querySelector('#r')).toBe(s.host);
261+
expect(markup(s.host)).toBe('<em>x</em>y');
262+
expect(s.recoverable).toHaveLength(1);
263+
264+
s.render({ on: false });
265+
expect(markup(s.host)).toBe('<em>x</em>y<b>z</b>');
266+
s.render({ on: true });
267+
expect(markup(s.host)).toBe('<em>x</em>y');
268+
});
269+
257270
it('builds the components a longer client arm adds and reports it once', async () => {
258271
const s = await hydrate('SwitchComponents', { k: 'one' }, { k: 'two' });
259272

@@ -277,6 +290,7 @@ describe.each([
277290
{ name: 'IfRangeAfterHost', props: { on: false }, html: '<em>x</em><b>b</b><em>z</em>' },
278291
{ name: 'IfInheritedHost', props: { on: true }, html: '<em>x</em>' },
279292
{ name: 'IfLaterHost', props: { on: true }, html: '<em>x</em><em>y</em>' },
293+
{ name: 'IfLaterFragment', props: { on: true }, html: '<em>x</em>y' },
280294
{ name: 'HoleWithoutRange', props: { on: true }, html: '<em>x</em><b>b</b>' },
281295
{
282296
name: 'NestedArms',

0 commit comments

Comments
 (0)