]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(ssr): fix unintended error on `Teleport` hydration mismatch (#1271)
authorunderfin <2218301630@qq.com>
Fri, 12 Jun 2020 14:01:56 +0000 (22:01 +0800)
committerGitHub <noreply@github.com>
Fri, 12 Jun 2020 14:01:56 +0000 (10:01 -0400)
fix #1235

packages/runtime-core/__tests__/hydration.spec.ts
packages/runtime-core/src/components/Teleport.ts

index 49daa3023f3da1eb2e9b9f46335b405354f01a02..e54063fdb3796670de0913eaecd08c7affcc2df5 100644 (file)
@@ -37,6 +37,10 @@ const triggerEvent = (type: string, el: Element) => {
 describe('SSR hydration', () => {
   mockWarn()
 
+  beforeEach(() => {
+    document.body.innerHTML = ''
+  })
+
   test('text', async () => {
     const msg = ref('foo')
     const { vnode, container } = mountWithHydration('foo', () => msg.value)
@@ -686,5 +690,17 @@ describe('SSR hydration', () => {
       // excessive children removal
       expect(`Hydration children mismatch`).toHaveBeenWarned()
     })
+
+    test('Teleport target has empty children', () => {
+      const teleportContainer = document.createElement('div')
+      teleportContainer.id = 'teleport'
+      document.body.appendChild(teleportContainer)
+
+      mountWithHydration('<!--teleport start--><!--teleport end-->', () =>
+        h(Teleport, { to: '#teleport' }, [h('span', 'value')])
+      )
+      expect(teleportContainer.innerHTML).toBe(`<span>value</span>`)
+      expect(`Hydration children mismatch`).toHaveBeenWarned()
+    })
   })
 })
index 8091f014408bc395f9f254cde56f9ed5b924297b..9d8034684df13e329fb5937421f90e86cd8b9035 100644 (file)
@@ -314,9 +314,8 @@ function hydrateTeleport(
           optimized
         )
       }
-      ;(target as TeleportTargetElement)._lpa = nextSibling(
-        vnode.targetAnchor as Node
-      )
+      ;(target as TeleportTargetElement)._lpa =
+        vnode.targetAnchor && nextSibling(vnode.targetAnchor as Node)
     }
   }
   return vnode.anchor && nextSibling(vnode.anchor as Node)