Skip to content

Commit 58aaf77

Browse files
committed
fix(hydration): walk past close markers in discardArmTail
Some slots (@Try, deopt lists) park the hydration cursor on their close marker rather than advancing past the outer range. When discardArmTail later inspects the cursor, a close marker caused the isOpen check to fail, skipping leftover server content. Walk past consecutive close markers before evaluating whether the cursor reached unclaimed tail. Each close marker belongs to a finished claimed block, so what follows is definitively unclaimed. Add IfTryThenTail fixture covering a branch arm whose last element is a @Try boundary with extra server content after it.
1 parent 639dabe commit 58aaf77

3 files changed

Lines changed: 55 additions & 2 deletions

File tree

‎packages/octane/src/runtime.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19358,10 +19358,21 @@ class HydrationCapability {
1935819358
* The cursor does not move past the elements and text that a template
1935919359
* adopts either, so at an element or text node it cannot tell what the arm
1936019360
* adopted from what the server rendered for another arm. Both stay.
19361+
*
19362+
* Some slots (@try, deopt lists) park the cursor on their close marker
19363+
* rather than advancing past the outer range. A close marker is never
19364+
* ambiguous — the block it closes is finished — so the method walks past
19365+
* consecutive close markers before inspecting the unclaimed tail.
1936119366
*/
1936219367
discardArmTail(scope: Scope, slotKey: number, first: Node, end: Node): void {
19363-
const from = this.node;
19364-
if (from === first || !this.isOpen(from)) return;
19368+
let from: Node | null = this.node;
19369+
if (from === first) return;
19370+
// A close marker means a control-flow block or slot parked the cursor on
19371+
// its end rather than advancing past the outer range (e.g. @try, deopt
19372+
// lists). Walk past consecutive close markers — each belongs to a finished
19373+
// claimed block — so `from` lands on the first unclaimed sibling.
19374+
while (from !== null && from !== end && this.isClose(from)) from = getNextSibling(from);
19375+
if (from === null || from === end || !this.isOpen(from)) return;
1936519376
let stop: Node | null = null;
1936619377
let node: Node | null = from;
1936719378
while (node !== null && node !== end) {

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,33 @@ export function TryIfThenChild(props: { on: boolean; Child: ComponentBody }) @{
122122
</div>
123123
}
124124

125+
// The client arm ends with a @try boundary, which parks the cursor on its
126+
// inner close marker rather than advancing past the outer range. The server's
127+
// longer else arm has an extra component after the shared @try that the client
128+
// arm must discard.
129+
export function IfTryThenTail(props: { on: boolean }) @{
130+
<div id="r">
131+
@if (props.on) {
132+
<>
133+
@try {
134+
<Leaf v="x" />
135+
} @pending {
136+
<p>{'loading' as string}</p>
137+
}
138+
</>
139+
} @else {
140+
<>
141+
@try {
142+
<Leaf v="x" />
143+
} @pending {
144+
<p>{'loading' as string}</p>
145+
}
146+
<Leaf v="z" />
147+
</>
148+
}
149+
</div>
150+
}
151+
125152
export function DormantIf(props: { on: boolean; when: HydrationStrategy }) @{
126153
<div id="r">
127154
<Hydrate when={props.when} split={false}>

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ describe.each([
141141
expect(markup(s.host)).toBe('<em>x</em>');
142142
});
143143

144+
it('discards the server component after a @try that ends the adopted arm', async () => {
145+
const s = await hydrate('IfTryThenTail', { on: false }, { on: true });
146+
147+
expect(container.querySelector('#r')).toBe(s.host);
148+
expect(markup(s.host)).toBe('<em>x</em>');
149+
expect(s.host.querySelector('em')).toBe(s.ems[0]);
150+
expect(s.recoverable).toEqual([expect.stringMatching(STRUCTURAL)]);
151+
expect(warnings()).toEqual(dev ? [tailReport('IfTryThenTail', '@if')] : []);
152+
153+
s.render({ on: false });
154+
expect(markup(s.host)).toBe('<em>x</em><em>z</em>');
155+
s.render({ on: true });
156+
expect(markup(s.host)).toBe('<em>x</em>');
157+
});
158+
144159
it('builds the components a longer client arm adds and reports it once', async () => {
145160
const s = await hydrate('SwitchComponents', { k: 'one' }, { k: 'two' });
146161

0 commit comments

Comments
 (0)