]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(hmr): avoid hydration for hmr root reload (#12450)
authoredison <daiwei521@126.com>
Tue, 13 May 2025 14:23:23 +0000 (22:23 +0800)
committerGitHub <noreply@github.com>
Tue, 13 May 2025 14:23:23 +0000 (22:23 +0800)
close vitejs/vite-plugin-vue#146
close vitejs/vite-plugin-vue#477

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

index 1c27f321ec8ee5aad42b143153c1176bb535365b..20519cf997fe0668819d98291972b4e920151a35 100644 (file)
@@ -1880,6 +1880,26 @@ describe('SSR hydration', () => {
     expect(root.innerHTML).toBe('<div><div>bar</div></div>')
   })
 
+  test('hmr root reload', async () => {
+    const appId = 'test-app-id'
+    const App = {
+      __hmrId: appId,
+      template: `<div>foo</div>`,
+    }
+
+    const root = document.createElement('div')
+    root.innerHTML = await renderToString(h(App))
+    createSSRApp(App).mount(root)
+    expect(root.innerHTML).toBe('<div>foo</div>')
+
+    reload(appId, {
+      __hmrId: appId,
+      template: `<div>bar</div>`,
+    })
+    await nextTick()
+    expect(root.innerHTML).toBe('<div>bar</div>')
+  })
+
   describe('mismatch handling', () => {
     test('text node', () => {
       const { container } = mountWithHydration(`foo`, () => 'bar')
index 748de866f75ebfa50847da47af39cd15f07ada5c..cba5e4ede02aedb52829cdab03e1beecec398729 100644 (file)
@@ -383,13 +383,12 @@ export function createAppAPI<HostElement>(
           // 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)
             }
           }