]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(ssr): fix hydration for node with empty text node (#7216)
authoredison <daiwei521@126.com>
Mon, 27 May 2024 08:50:49 +0000 (16:50 +0800)
committerGitHub <noreply@github.com>
Mon, 27 May 2024 08:50:49 +0000 (16:50 +0800)
packages/runtime-core/__tests__/hydration.spec.ts
packages/runtime-core/src/hydration.ts

index ec9613c95712682ab60e035435253181b8eb1425..7d90ea9f3314e39e501961a41f986483bec1fa11 100644 (file)
@@ -1160,6 +1160,21 @@ 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('<p></p>', () => 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 = '<button></button>'
index dd3da56a6247a973085b1094b224b882ddc5d091..87e4c60a2534ecb501e2f09009e9fe6680effe3b 100644 (file)
@@ -541,7 +541,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 (