Skip to content

Commit 9c4dbbc

Browse files
authored
fix(hmr): avoid hydration for hmr updating (#12262)
close #7706 close #8170
1 parent f7dad6d commit 9c4dbbc

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ import {
3232
withCtx,
3333
withDirectives,
3434
} from '@vue/runtime-dom'
35+
import type { HMRRuntime } from '../src/hmr'
3536
import { type SSRContext, renderToString } from '@vue/server-renderer'
3637
import { PatchFlags, normalizeStyle } from '@vue/shared'
3738
import { vShowOriginalDisplay } from '../../runtime-dom/src/directives/vShow'
38-
import { expect } from 'vitest'
39+
40+
declare var __VUE_HMR_RUNTIME__: HMRRuntime
41+
const { createRecord, reload } = __VUE_HMR_RUNTIME__
3942

4043
function mountWithHydration(html: string, render: () => any) {
4144
const container = document.createElement('div')
@@ -1843,6 +1846,40 @@ describe('SSR hydration', () => {
18431846
}
18441847
})
18451848

1849+
test('hmr reload child wrapped in KeepAlive', async () => {
1850+
const id = 'child-reload'
1851+
const Child = {
1852+
__hmrId: id,
1853+
template: `<div>foo</div>`,
1854+
}
1855+
createRecord(id, Child)
1856+
1857+
const appId = 'test-app-id'
1858+
const App = {
1859+
__hmrId: appId,
1860+
components: { Child },
1861+
template: `
1862+
<div>
1863+
<KeepAlive>
1864+
<Child />
1865+
</KeepAlive>
1866+
</div>
1867+
`,
1868+
}
1869+
1870+
const root = document.createElement('div')
1871+
root.innerHTML = await renderToString(h(App))
1872+
createSSRApp(App).mount(root)
1873+
expect(root.innerHTML).toBe('<div><div>foo</div></div>')
1874+
1875+
reload(id, {
1876+
__hmrId: id,
1877+
template: `<div>bar</div>`,
1878+
})
1879+
await nextTick()
1880+
expect(root.innerHTML).toBe('<div><div>bar</div></div>')
1881+
})
1882+
18461883
describe('mismatch handling', () => {
18471884
test('text node', () => {
18481885
const { container } = mountWithHydration(`foo`, () => 'bar')

packages/runtime-core/src/renderer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,12 +1208,12 @@ function baseCreateRenderer(
12081208
}
12091209
}
12101210

1211+
// avoid hydration for hmr updating
1212+
if (__DEV__ && isHmrUpdating) initialVNode.el = null
1213+
12111214
// setup() is async. This component relies on async logic to be resolved
12121215
// before proceeding
12131216
if (__FEATURE_SUSPENSE__ && instance.asyncDep) {
1214-
// avoid hydration for hmr updating
1215-
if (__DEV__ && isHmrUpdating) initialVNode.el = null
1216-
12171217
parentSuspense &&
12181218
parentSuspense.registerDep(instance, setupRenderEffect, optimized)
12191219

0 commit comments

Comments
 (0)