Skip to content

Commit

Permalink
fix(keep-alive): avoid cache suspense comment root (#11479)
Browse files Browse the repository at this point in the history
  • Loading branch information
edison1105 authored Aug 7, 2024
1 parent 536f623 commit a917c05
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/runtime-core/__tests__/components/Suspense.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
KeepAlive,
Suspense,
type SuspenseProps,
createCommentVNode,
h,
nextTick,
nodeOps,
Expand Down Expand Up @@ -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')
Expand Down
7 changes: 7 additions & 0 deletions packages/runtime-core/src/components/KeepAlive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getCurrentInstance,
} from '../component'
import {
Comment,
type VNode,
type VNodeProps,
cloneVNode,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a917c05

Please sign in to comment.