Skip to content

Commit 7257e6a

Browse files
authoredSep 20, 2024··
fix(hydration): avoid observing non-Element node (#11954)
close #11952
1 parent e075dfa commit 7257e6a

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed
 

‎packages/runtime-core/src/hydrationStrategies.ts

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const hydrateOnVisible: HydrationStrategyFactory<
4848
}
4949
}, opts)
5050
forEach(el => {
51+
if (!(el instanceof Element)) return
5152
if (elementIsVisibleInViewport(el)) {
5253
hydrate()
5354
ob.disconnect()

‎packages/vue/__tests__/e2e/hydration-strat-visible.html

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
<script>
1212
const rootMargin = location.search.match(/rootMargin=(\d+)/)?.[1] ?? 0
1313
const isFragment = location.search.includes('?fragment')
14+
const isVIf = location.search.includes('?v-if')
1415
if (isFragment) {
1516
document.getElementById('app').innerHTML =
1617
`<!--[--><!--[--><span>one</span><!--]--><button>0</button><span>two</span><!--]-->`
18+
} else if (isVIf) {
19+
document.getElementById('app').innerHTML = `<!---->`
1720
}
1821

1922
window.isHydrated = false
@@ -24,6 +27,7 @@
2427
ref,
2528
onMounted,
2629
hydrateOnVisible,
30+
createCommentVNode,
2731
} = Vue
2832

2933
const Comp = {
@@ -39,7 +43,9 @@
3943
{ onClick: () => count.value++ },
4044
count.value,
4145
)
42-
if (isFragment) {
46+
if (isVIf) {
47+
return createCommentVNode('v-if', true)
48+
} else if (isFragment) {
4349
return [[h('span', 'one')], button, h('span', 'two')]
4450
} else {
4551
return button

‎packages/vue/__tests__/e2e/hydrationStrategies.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ describe('async component hydration strategies', () => {
6565
await assertHydrationSuccess()
6666
})
6767

68+
test('visible (root v-if) should not throw error', async () => {
69+
const spy = vi.fn()
70+
const currentPage = page()
71+
currentPage.on('pageerror', spy)
72+
await goToCase('visible', '?v-if')
73+
await page().waitForFunction(() => window.isRootMounted)
74+
expect(await page().evaluate(() => window.isHydrated)).toBe(false)
75+
expect(spy).toBeCalledTimes(0)
76+
currentPage.off('pageerror', spy)
77+
})
78+
6879
test('media query', async () => {
6980
await goToCase('media')
7081
await page().waitForFunction(() => window.isRootMounted)

0 commit comments

Comments
 (0)
Please sign in to comment.