From: edison Date: Tue, 13 May 2025 14:15:50 +0000 (+0800) Subject: fix(hmr): avoid hydration for hmr updating (#12262) X-Git-Tag: v3.5.14~5 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9c4dbbc5185125835ad3e49baba303bd54676111;p=thirdparty%2Fvuejs%2Fcore.git fix(hmr): avoid hydration for hmr updating (#12262) close #7706 close #8170 --- diff --git a/packages/runtime-core/__tests__/hydration.spec.ts b/packages/runtime-core/__tests__/hydration.spec.ts index 56011d0635..1c27f321ec 100644 --- a/packages/runtime-core/__tests__/hydration.spec.ts +++ b/packages/runtime-core/__tests__/hydration.spec.ts @@ -32,10 +32,13 @@ import { withCtx, withDirectives, } from '@vue/runtime-dom' +import type { HMRRuntime } from '../src/hmr' import { type SSRContext, renderToString } from '@vue/server-renderer' import { PatchFlags, normalizeStyle } from '@vue/shared' import { vShowOriginalDisplay } from '../../runtime-dom/src/directives/vShow' -import { expect } from 'vitest' + +declare var __VUE_HMR_RUNTIME__: HMRRuntime +const { createRecord, reload } = __VUE_HMR_RUNTIME__ function mountWithHydration(html: string, render: () => any) { const container = document.createElement('div') @@ -1843,6 +1846,40 @@ describe('SSR hydration', () => { } }) + test('hmr reload child wrapped in KeepAlive', async () => { + const id = 'child-reload' + const Child = { + __hmrId: id, + template: `
foo
`, + } + createRecord(id, Child) + + const appId = 'test-app-id' + const App = { + __hmrId: appId, + components: { Child }, + template: ` +
+ + + +
+ `, + } + + const root = document.createElement('div') + root.innerHTML = await renderToString(h(App)) + createSSRApp(App).mount(root) + expect(root.innerHTML).toBe('
foo
') + + reload(id, { + __hmrId: id, + 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/renderer.ts b/packages/runtime-core/src/renderer.ts index 022571050b..e43348f85e 100644 --- a/packages/runtime-core/src/renderer.ts +++ b/packages/runtime-core/src/renderer.ts @@ -1208,12 +1208,12 @@ function baseCreateRenderer( } } + // avoid hydration for hmr updating + if (__DEV__ && isHmrUpdating) initialVNode.el = null + // setup() is async. This component relies on async logic to be resolved // before proceeding if (__FEATURE_SUSPENSE__ && instance.asyncDep) { - // avoid hydration for hmr updating - if (__DEV__ && isHmrUpdating) initialVNode.el = null - parentSuspense && parentSuspense.registerDep(instance, setupRenderEffect, optimized)