Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hydration): the component vnode's el should be updated when a mismatch occurs. #12255

Merged
merged 7 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/runtime-core/__tests__/hydration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
h,
nextTick,
onMounted,
onServerPrefetch,
openBlock,
reactive,
ref,
Expand Down Expand Up @@ -518,6 +519,45 @@ describe('SSR hydration', () => {
)
})

test('with data-allow-mismatch component when using onServerPrefetch', async () => {
const Comp = {
template: `
<div>Comp2</div>
`,
}
let foo: any
const App = {
setup() {
const flag = ref(true)
foo = () => {
flag.value = false
}
onServerPrefetch(() => (flag.value = false))
return { flag }
},
components: {
Comp,
},
template: `
<span data-allow-mismatch>
<Comp v-if="flag"></Comp>
</span>
`,
}
// hydrate
const container = document.createElement('div')
container.innerHTML = await renderToString(h(App))
createSSRApp(App).mount(container)
expect(container.innerHTML).toBe(
'<span data-allow-mismatch=""><div>Comp2</div></span>',
)
foo()
await nextTick()
expect(container.innerHTML).toBe(
'<span data-allow-mismatch=""><!--v-if--></span>',
)
})

test('Teleport unmount (full integration)', async () => {
const Comp1 = {
template: `
Expand Down
4 changes: 4 additions & 0 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,10 @@ function baseCreateRenderer(
parentSuspense,
null,
)
// when data-allow-mismatch is set, the component vnode's el may be incorrect, so it should be updated
if (initialVNode) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The vnode.el needs to be updated only if a mismatch occurs.
So this is not the proper fix. The correct fix should be in the handleMismatch function.

initialVNode.el = instance.subTree.el
}
if (__DEV__) {
endMeasure(instance, `hydrate`)
}
Expand Down