Skip to content

Commit 1f98a9c

Browse files
authored
fix(hmr): avoid hydration for hmr root reload (#12450)
close vitejs/vite-plugin-vue#146 close vitejs/vite-plugin-vue#477
1 parent 6264505 commit 1f98a9c

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
@@ -383,13 +383,12 @@ export function createAppAPI<HostElement>(
383383
// HMR root reload
384384
if (__DEV__) {
385385
context.reload = () => {
386+
const cloned = cloneVNode(vnode)
387+
// avoid hydration for hmr updating
388+
cloned.el = null
386389
// casting to ElementNamespace because TS doesn't guarantee type narrowing
387390
// over function boundaries
388-
render(
389-
cloneVNode(vnode),
390-
rootContainer,
391-
namespace as ElementNamespace,
392-
)
391+
render(cloned, rootContainer, namespace as ElementNamespace)
393392
}
394393
}
395394

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy