Skip to content

Commit 58e368d

Browse files
committed
fix(runtime-vapor): use logical index for nthChild hydration
1 parent 4efae81 commit 58e368d

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

packages/runtime-vapor/__tests__/hydration.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7454,6 +7454,34 @@ describe('mismatch handling', () => {
74547454
)
74557455
})
74567456

7457+
test('nthChild hydration uses logical index after inserted sibling', async () => {
7458+
const { container, data } = await testWithVaporApp(
7459+
`
7460+
<template>
7461+
<div>
7462+
<components.Child />
7463+
<span>static</span>
7464+
<p>static</p>
7465+
<section>{{ data }}</section>
7466+
</div>
7467+
</template>
7468+
`,
7469+
{
7470+
Child: '<template><a>child</a></template>',
7471+
},
7472+
)
7473+
7474+
expect(container.innerHTML).toBe(
7475+
`<div><a>child</a><span>static</span><p>static</p><section>foo</section></div>`,
7476+
)
7477+
7478+
data.value = 'bar'
7479+
await nextTick()
7480+
expect(container.innerHTML).toBe(
7481+
`<div><a>child</a><span>static</span><p>static</p><section>bar</section></div>`,
7482+
)
7483+
})
7484+
74577485
test('single-root nested v-if hydration keeps static siblings', async () => {
74587486
const { container, data } = await testWithVaporApp(`
74597487
<template>

packages/runtime-vapor/src/dom/node.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@ export function child(node: InsertionParent, logicalIndex?: number): Node {
4949
}
5050

5151
/*@__NO_SIDE_EFFECTS__*/
52-
export function nthChild(node: InsertionParent, i: number): Node {
52+
export function nthChild(
53+
node: InsertionParent,
54+
i: number,
55+
logicalIndex: number = i,
56+
): Node {
5357
if (isHydrating) {
54-
return locateChildByLogicalIndex(node, i)!
58+
return locateChildByLogicalIndex(node, logicalIndex)!
5559
}
5660
return node.childNodes[i]
5761
}

0 commit comments

Comments
 (0)