From 81351dc7fbdabcfa0f545f7d924c31a3c367e496 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E3=81=8B=E3=81=A3=E3=81=93=E3=81=8B=E3=82=8A?=
<67428053+kakkokari-gtyih@users.noreply.github.com>
Date: Wed, 7 Aug 2024 12:58:44 +0900
Subject: [PATCH] fix(keep-alive): fix render error in cached is undefined
(#11496)
fix #11427
close #11431
---
.../__tests__/components/Suspense.spec.ts | 22 ++++++++++++-------
.../runtime-core/src/components/KeepAlive.ts | 2 +-
2 files changed, 15 insertions(+), 9 deletions(-)
diff --git a/packages/runtime-core/__tests__/components/Suspense.spec.ts b/packages/runtime-core/__tests__/components/Suspense.spec.ts
index 62765dad9..65e801de2 100644
--- a/packages/runtime-core/__tests__/components/Suspense.spec.ts
+++ b/packages/runtime-core/__tests__/components/Suspense.spec.ts
@@ -2036,7 +2036,7 @@ describe('Suspense', () => {
expect(serializeInner(root)).toBe(`
sync
`)
})
- // #10899
+ // #10899 / #11427
test('KeepAlive + Suspense switch before branch resolves', async () => {
const Async1 = defineAsyncComponent({
render() {
@@ -2053,14 +2053,20 @@ describe('Suspense', () => {
const root = nodeOps.createElement('div')
const App = {
render() {
- return h(KeepAlive, null, {
- default: () => {
- return h(Suspense, null, {
- default: h(components[viewRef.value]),
- fallback: h('div', 'loading'),
- })
+ return h(
+ KeepAlive,
+ {
+ max: 1,
},
- })
+ {
+ default: () => {
+ return h(Suspense, null, {
+ default: h(components[viewRef.value]),
+ fallback: h('div', 'loading'),
+ })
+ },
+ },
+ )
},
}
render(h(App), root)
diff --git a/packages/runtime-core/src/components/KeepAlive.ts b/packages/runtime-core/src/components/KeepAlive.ts
index 74cb1b1c1..8fe1088b2 100644
--- a/packages/runtime-core/src/components/KeepAlive.ts
+++ b/packages/runtime-core/src/components/KeepAlive.ts
@@ -206,7 +206,7 @@ const KeepAliveImpl: ComponentOptions = {
function pruneCacheEntry(key: CacheKey) {
const cached = cache.get(key) as VNode
- if (!current || !isSameVNodeType(cached, current)) {
+ if (cached && (!current || !isSameVNodeType(cached, current))) {
unmount(cached)
} else if (current) {
// current active instance should no longer be kept-alive.