From 1f98a9c493d01c21befa90107f0593bc92a58932 Mon Sep 17 00:00:00 2001 From: edison Date: Tue, 13 May 2025 22:23:23 +0800 Subject: [PATCH] fix(hmr): avoid hydration for hmr root reload (#12450) close vitejs/vite-plugin-vue#146 close vitejs/vite-plugin-vue#477 --- .../runtime-core/__tests__/hydration.spec.ts | 20 +++++++++++++++++++ packages/runtime-core/src/apiCreateApp.ts | 9 ++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/runtime-core/__tests__/hydration.spec.ts b/packages/runtime-core/__tests__/hydration.spec.ts index 1c27f321ec..20519cf997 100644 --- a/packages/runtime-core/__tests__/hydration.spec.ts +++ b/packages/runtime-core/__tests__/hydration.spec.ts @@ -1880,6 +1880,26 @@ describe('SSR hydration', () => { expect(root.innerHTML).toBe('
bar
') }) + test('hmr root reload', async () => { + const appId = 'test-app-id' + const App = { + __hmrId: appId, + template: `
foo
`, + } + + const root = document.createElement('div') + root.innerHTML = await renderToString(h(App)) + createSSRApp(App).mount(root) + expect(root.innerHTML).toBe('
foo
') + + reload(appId, { + __hmrId: appId, + template: `
bar
`, + }) + await nextTick() + expect(root.innerHTML).toBe('
bar
') + }) + describe('mismatch handling', () => { test('text node', () => { const { container } = mountWithHydration(`foo`, () => 'bar') diff --git a/packages/runtime-core/src/apiCreateApp.ts b/packages/runtime-core/src/apiCreateApp.ts index 748de866f7..cba5e4ede0 100644 --- a/packages/runtime-core/src/apiCreateApp.ts +++ b/packages/runtime-core/src/apiCreateApp.ts @@ -383,13 +383,12 @@ export function createAppAPI( // HMR root reload if (__DEV__) { context.reload = () => { + const cloned = cloneVNode(vnode) + // avoid hydration for hmr updating + cloned.el = null // casting to ElementNamespace because TS doesn't guarantee type narrowing // over function boundaries - render( - cloneVNode(vnode), - rootContainer, - namespace as ElementNamespace, - ) + render(cloned, rootContainer, namespace as ElementNamespace) } } -- 2.47.2