From 175c0572b68c9835edf8f6f1e2cb5a362831984c Mon Sep 17 00:00:00 2001 From: edison1105 Date: Sat, 26 Nov 2022 10:15:42 +0800 Subject: [PATCH 1/2] fix(ssr): fix hydration for node with empty text node test: add test case test: add test case chore: update comment --- .../runtime-core/__tests__/hydration.spec.ts | 17 +++++++++++++++++ packages/runtime-core/src/hydration.ts | 4 +++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/hydration.spec.ts b/packages/runtime-core/__tests__/hydration.spec.ts index 493405b8ab8..b0404c1045e 100644 --- a/packages/runtime-core/__tests__/hydration.spec.ts +++ b/packages/runtime-core/__tests__/hydration.spec.ts @@ -955,6 +955,23 @@ describe('SSR hydration', () => { expect((vnode as any).component?.subTree.children[0].el).toBe(text) }) + // #7215 + test('empty text node', () => { + const Comp = { + render(this: any) { + return h('p', ['']) + } + } + const { container } = mountWithHydration('

', () => + h(Comp) + ) + expect(container.childNodes.length).toBe(1) + const p = container.childNodes[0] + expect(p.childNodes.length).toBe(1) + const text = p.childNodes[0] + expect(text.nodeType).toBe(3) + }) + test('app.unmount()', async () => { const container = document.createElement('DIV') container.innerHTML = '' diff --git a/packages/runtime-core/src/hydration.ts b/packages/runtime-core/src/hydration.ts index 2170a9192cf..70af6db7e9d 100644 --- a/packages/runtime-core/src/hydration.ts +++ b/packages/runtime-core/src/hydration.ts @@ -443,7 +443,9 @@ export function createHydrationFunctions( optimized ) } else if (vnode.type === Text && !vnode.children) { - continue + // #7215 create a TextNode for empty text node + // because server rendered HTML won't contain a text node + insert(vnode.el = createText(''), container) } else { hasMismatch = true if (__DEV__ && !hasWarned) { From ea34b24a1c66af45afb77b0215955cab0634a21d Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Fri, 20 Oct 2023 03:53:20 +0000 Subject: [PATCH 2/2] [autofix.ci] apply automated fixes --- packages/runtime-core/__tests__/hydration.spec.ts | 4 +--- packages/runtime-core/src/hydration.ts | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/runtime-core/__tests__/hydration.spec.ts b/packages/runtime-core/__tests__/hydration.spec.ts index de17e33b53a..e2e03aaa7d0 100644 --- a/packages/runtime-core/__tests__/hydration.spec.ts +++ b/packages/runtime-core/__tests__/hydration.spec.ts @@ -966,9 +966,7 @@ describe('SSR hydration', () => { return h('p', ['']) } } - const { container } = mountWithHydration('

', () => - h(Comp) - ) + const { container } = mountWithHydration('

', () => h(Comp)) expect(container.childNodes.length).toBe(1) const p = container.childNodes[0] expect(p.childNodes.length).toBe(1) diff --git a/packages/runtime-core/src/hydration.ts b/packages/runtime-core/src/hydration.ts index 4963beef342..068571d937b 100644 --- a/packages/runtime-core/src/hydration.ts +++ b/packages/runtime-core/src/hydration.ts @@ -447,7 +447,7 @@ export function createHydrationFunctions( } else if (vnode.type === Text && !vnode.children) { // #7215 create a TextNode for empty text node // because server rendered HTML won't contain a text node - insert(vnode.el = createText(''), container) + insert((vnode.el = createText('')), container) } else { hasMismatch = true if (__DEV__ && !hasWarned) {