]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(keep-alive): avoid cache suspense comment root (#11479)
authoredison <daiwei521@126.com>
Wed, 7 Aug 2024 03:24:17 +0000 (11:24 +0800)
committerGitHub <noreply@github.com>
Wed, 7 Aug 2024 03:24:17 +0000 (11:24 +0800)
packages/runtime-core/__tests__/components/Suspense.spec.ts
packages/runtime-core/src/components/KeepAlive.ts

index 8de5b3182d0d6b9e354d71e3e06ef17c72dde774..62765dad987bd7e5b76620b3099a69c83df38403 100644 (file)
@@ -8,6 +8,7 @@ import {
   KeepAlive,
   Suspense,
   type SuspenseProps,
+  createCommentVNode,
   h,
   nextTick,
   nodeOps,
@@ -2085,6 +2086,35 @@ describe('Suspense', () => {
     expect(serializeInner(root)).toBe(`<div>async2</div>`)
   })
 
+  test('KeepAlive + Suspense + comment slot', async () => {
+    const toggle = ref(false)
+    const Async = defineAsyncComponent({
+      render() {
+        return h('div', 'async1')
+      },
+    })
+    const App = {
+      render() {
+        return h(KeepAlive, null, {
+          default: () => {
+            return h(Suspense, null, {
+              default: toggle.value ? h(Async) : createCommentVNode('v-if'),
+            })
+          },
+        })
+      },
+    }
+
+    const root = nodeOps.createElement('div')
+    render(h(App), root)
+    expect(serializeInner(root)).toBe(`<!--v-if-->`)
+
+    toggle.value = true
+    await nextTick()
+    await Promise.all(deps)
+    expect(serializeInner(root)).toBe(`<div>async1</div>`)
+  })
+
   // #6416 follow up / #10017
   test('Suspense patched during HOC async component re-mount', async () => {
     const key = ref('k')
index a2c8c2bf1a75834f36b5e1ef36d29d87f0dd15f7..74cb1b1c12d20181f335d3d92e83a5c87176a5f2 100644 (file)
@@ -8,6 +8,7 @@ import {
   getCurrentInstance,
 } from '../component'
 import {
+  Comment,
   type VNode,
   type VNodeProps,
   cloneVNode,
@@ -287,6 +288,12 @@ const KeepAliveImpl: ComponentOptions = {
       }
 
       let vnode = getInnerChild(rawVNode)
+      // #6028 Suspense ssContent maybe a comment VNode, should avoid caching it
+      if (vnode.type === Comment) {
+        current = null
+        return vnode
+      }
+
       const comp = vnode.type as ConcreteComponent
 
       // for async components, name check should be based in its loaded