Skip to content

Commit c463a71

Browse files
authored
fix(ssr): fix unintended error on Teleport hydration mismatch (#1271)
fix #1235
1 parent d863ce7 commit c463a71

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

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

+16
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ const triggerEvent = (type: string, el: Element) => {
3737
describe('SSR hydration', () => {
3838
mockWarn()
3939

40+
beforeEach(() => {
41+
document.body.innerHTML = ''
42+
})
43+
4044
test('text', async () => {
4145
const msg = ref('foo')
4246
const { vnode, container } = mountWithHydration('foo', () => msg.value)
@@ -686,5 +690,17 @@ describe('SSR hydration', () => {
686690
// excessive children removal
687691
expect(`Hydration children mismatch`).toHaveBeenWarned()
688692
})
693+
694+
test('Teleport target has empty children', () => {
695+
const teleportContainer = document.createElement('div')
696+
teleportContainer.id = 'teleport'
697+
document.body.appendChild(teleportContainer)
698+
699+
mountWithHydration('<!--teleport start--><!--teleport end-->', () =>
700+
h(Teleport, { to: '#teleport' }, [h('span', 'value')])
701+
)
702+
expect(teleportContainer.innerHTML).toBe(`<span>value</span>`)
703+
expect(`Hydration children mismatch`).toHaveBeenWarned()
704+
})
689705
})
690706
})

packages/runtime-core/src/components/Teleport.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,8 @@ function hydrateTeleport(
314314
optimized
315315
)
316316
}
317-
;(target as TeleportTargetElement)._lpa = nextSibling(
318-
vnode.targetAnchor as Node
319-
)
317+
;(target as TeleportTargetElement)._lpa =
318+
vnode.targetAnchor && nextSibling(vnode.targetAnchor as Node)
320319
}
321320
}
322321
return vnode.anchor && nextSibling(vnode.anchor as Node)

0 commit comments

Comments
 (0)