Skip to content

Commit 9d3ee8e

Browse files
committed
fix(hydration): handle v-if on insertion parent
1 parent 6505a8f commit 9d3ee8e

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,28 @@ describe('Vapor Mode hydration', () => {
11341134
expect(container.innerHTML).toBe(`<div>foo</div><!---->`)
11351135
})
11361136

1137+
test('v-if on insertion parent', async () => {
1138+
const data = ref(true)
1139+
const { container } = await testHydration(
1140+
`<template>
1141+
<div v-if="data">
1142+
<components.Child/>
1143+
</div>
1144+
</template>`,
1145+
{ Child: `<template>foo</template>` },
1146+
data,
1147+
)
1148+
expect(container.innerHTML).toBe(`<div>foo</div><!--${anchorLabel}-->`)
1149+
1150+
data.value = false
1151+
await nextTick()
1152+
expect(container.innerHTML).toBe(`<!--${anchorLabel}-->`)
1153+
1154+
data.value = true
1155+
await nextTick()
1156+
expect(container.innerHTML).toBe(`<div>foo</div><!--${anchorLabel}-->`)
1157+
})
1158+
11371159
test('v-if/else-if/else chain - switch branches', async () => {
11381160
const data = ref('a')
11391161
const { container } = await testHydration(

packages/runtime-vapor/src/apiCreateIf.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,37 @@ import {
99
import { renderEffect } from './renderEffect'
1010
import { DynamicFragment } from './fragment'
1111

12+
const ifStack = [] as DynamicFragment[]
13+
const insertionParents = new WeakMap<DynamicFragment, Node[]>()
14+
15+
/**
16+
* Collects insertionParents inside an if block during hydration
17+
* When the if condition becomes false on the client, clears the
18+
* HTML of these insertionParents to prevent duplicate rendering
19+
* results when the condition becomes true again
20+
*
21+
* Example:
22+
* const t2 = _template("<div></div>")
23+
* const n2 = _createIf(() => show.value, () => {
24+
* const n5 = t2()
25+
* _setInsertionState(n5)
26+
* const n4 = _createComponent(Comp) // renders `<span></span>`
27+
* return n5
28+
* })
29+
*
30+
* After hydration, the HTML of `n5` is `<div><span></span></div>` instead of `<div></div>`.
31+
* When `show.value` becomes false, the HTML of `n5` needs to be cleared,
32+
* to avoid duplicated rendering when `show.value` becomes true again.
33+
*/
34+
export function collectInsertionParents(insertionParent: ParentNode): void {
35+
const currentIf = ifStack[ifStack.length - 1]
36+
if (currentIf) {
37+
let nodes = insertionParents.get(currentIf)
38+
if (!nodes) insertionParents.set(currentIf, (nodes = []))
39+
nodes.push(insertionParent)
40+
}
41+
}
42+
1243
export function createIf(
1344
condition: () => any,
1445
b1: BlockFn,
@@ -27,7 +58,19 @@ export function createIf(
2758
isHydrating || __DEV__
2859
? new DynamicFragment(IF_ANCHOR_LABEL)
2960
: new DynamicFragment()
61+
if (isHydrating) {
62+
;(frag as DynamicFragment).teardown = () => {
63+
const nodes = insertionParents.get(frag as DynamicFragment)
64+
if (nodes) {
65+
nodes.forEach(p => ((p as Element).innerHTML = ''))
66+
insertionParents.delete(frag as DynamicFragment)
67+
}
68+
;(frag as DynamicFragment).teardown = undefined
69+
}
70+
ifStack.push(frag as DynamicFragment)
71+
}
3072
renderEffect(() => (frag as DynamicFragment).update(condition() ? b1 : b2))
73+
isHydrating && ifStack.pop()
3174
}
3275

3376
if (!isHydrating && _insertionParent) {

packages/runtime-vapor/src/fragment.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export class DynamicFragment extends VaporFragment {
5959
* indicates forwarded slot
6060
*/
6161
forwarded?: boolean
62+
teardown?: () => void
6263

6364
constructor(anchorLabel?: string) {
6465
super([])
@@ -97,6 +98,7 @@ export class DynamicFragment extends VaporFragment {
9798
// teardown previous branch
9899
if (this.scope) {
99100
this.scope.stop()
101+
if (parent) this.teardown && this.teardown()
100102
const mode = transition && transition.mode
101103
if (mode) {
102104
applyTransitionLeaveHooks(this.nodes, transition, renderBranch)

packages/runtime-vapor/src/insertionState.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { collectInsertionParents } from './apiCreateIf'
2+
import { isHydrating } from './dom/hydration'
3+
14
export let insertionParent:
25
| (ParentNode & {
36
// dynamic node position - hydration only
@@ -21,6 +24,10 @@ export let insertionAnchor: Node | 0 | undefined
2124
export function setInsertionState(parent: ParentNode, anchor?: Node | 0): void {
2225
insertionParent = parent
2326
insertionAnchor = anchor
27+
28+
if (isHydrating) {
29+
collectInsertionParents(parent)
30+
}
2431
}
2532

2633
export function resetInsertionState(): void {

0 commit comments

Comments
 (0)