Skip to content

Commit b194674

Browse files
edison1105camc314
authored andcommitted
fix(hmr): avoid hydration for hmr root reload (vuejs#12450)
close vitejs/vite-plugin-vue#146 close vitejs/vite-plugin-vue#477
1 parent 4e4705f commit b194674

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,6 +1880,26 @@ describe('SSR hydration', () => {
18801880
expect(root.innerHTML).toBe('<div><div>bar</div></div>')
18811881
})
18821882

1883+
test('hmr root reload', async () => {
1884+
const appId = 'test-app-id'
1885+
const App = {
1886+
__hmrId: appId,
1887+
template: `<div>foo</div>`,
1888+
}
1889+
1890+
const root = document.createElement('div')
1891+
root.innerHTML = await renderToString(h(App))
1892+
createSSRApp(App).mount(root)
1893+
expect(root.innerHTML).toBe('<div>foo</div>')
1894+
1895+
reload(appId, {
1896+
__hmrId: appId,
1897+
template: `<div>bar</div>`,
1898+
})
1899+
await nextTick()
1900+
expect(root.innerHTML).toBe('<div>bar</div>')
1901+
})
1902+
18831903
describe('mismatch handling', () => {
18841904
test('text node', () => {
18851905
const { container } = mountWithHydration(`foo`, () => 'bar')

packages/runtime-core/src/apiCreateApp.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,12 @@ export function createAppAPI<HostElement>(
377377
// HMR root reload
378378
if (__DEV__) {
379379
context.reload = () => {
380+
const cloned = cloneVNode(vnode)
381+
// avoid hydration for hmr updating
382+
cloned.el = null
380383
// casting to ElementNamespace because TS doesn't guarantee type narrowing
381384
// over function boundaries
382-
render(
383-
cloneVNode(vnode),
384-
rootContainer,
385-
namespace as ElementNamespace,
386-
)
385+
render(cloned, rootContainer, namespace as ElementNamespace)
387386
}
388387
}
389388

0 commit comments

Comments
 (0)