From 5901f25d81e3f088ab7855c0479d6cc30ec4bfed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E4=B8=9C?= Date: Tue, 16 Jul 2024 14:41:26 +0800 Subject: [PATCH 1/3] fix(keep-alive): The value of include is empty string should cache nothing(#11366) --- packages/runtime-core/src/components/KeepAlive.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/runtime-core/src/components/KeepAlive.ts b/packages/runtime-core/src/components/KeepAlive.ts index a2c8c2bf1a7..05d99f98916 100644 --- a/packages/runtime-core/src/components/KeepAlive.ts +++ b/packages/runtime-core/src/components/KeepAlive.ts @@ -220,7 +220,8 @@ const KeepAliveImpl: ComponentOptions = { watch( () => [props.include, props.exclude], ([include, exclude]) => { - include && pruneCache(name => matches(include, name)) + ;(include || typeof include === 'string') && + pruneCache(name => matches(include, name)) exclude && pruneCache(name => !matches(exclude, name)) }, // prune post-render after `current` has been updated @@ -300,7 +301,8 @@ const KeepAliveImpl: ComponentOptions = { const { include, exclude, max } = props if ( - (include && (!name || !matches(include, name))) || + ((include || typeof include === 'string') && + (!name || !matches(include, name))) || (exclude && name && matches(exclude, name)) ) { current = vnode From 12afb295e20d58c167ad1f62edc37877e6164e84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E4=B8=9C?= Date: Thu, 18 Jul 2024 09:26:56 +0800 Subject: [PATCH 2/3] fix(keep-alive): add test case and modify code --- .../__tests__/components/KeepAlive.spec.ts | 10 ++++++++++ packages/runtime-core/src/components/KeepAlive.ts | 10 ++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/packages/runtime-core/__tests__/components/KeepAlive.spec.ts b/packages/runtime-core/__tests__/components/KeepAlive.spec.ts index 6d3f6a9b8b6..902563032b2 100644 --- a/packages/runtime-core/__tests__/components/KeepAlive.spec.ts +++ b/packages/runtime-core/__tests__/components/KeepAlive.spec.ts @@ -539,6 +539,16 @@ describe('KeepAlive', () => { await nextTick() assertHookCalls(one, [2, 2, 1, 1, 1]) assertHookCalls(two, [1, 1, 1, 1, 0]) + + includeRef.value = '' + await nextTick() + assertHookCalls(one, [2, 2, 1, 1, 1]) + assertHookCalls(two, [1, 1, 1, 1, 1]) + + viewRef.value = 'two' + await nextTick() + assertHookCalls(one, [2, 2, 1, 1, 2]) + assertHookCalls(two, [2, 2, 1, 1, 1]) }) test('on exclude change', async () => { diff --git a/packages/runtime-core/src/components/KeepAlive.ts b/packages/runtime-core/src/components/KeepAlive.ts index 05d99f98916..349d74bbb62 100644 --- a/packages/runtime-core/src/components/KeepAlive.ts +++ b/packages/runtime-core/src/components/KeepAlive.ts @@ -220,9 +220,8 @@ const KeepAliveImpl: ComponentOptions = { watch( () => [props.include, props.exclude], ([include, exclude]) => { - ;(include || typeof include === 'string') && - pruneCache(name => matches(include, name)) - exclude && pruneCache(name => !matches(exclude, name)) + include != null && pruneCache(name => matches(include, name)) + exclude != null && pruneCache(name => !matches(exclude, name)) }, // prune post-render after `current` has been updated { flush: 'post', deep: true }, @@ -301,9 +300,8 @@ const KeepAliveImpl: ComponentOptions = { const { include, exclude, max } = props if ( - ((include || typeof include === 'string') && - (!name || !matches(include, name))) || - (exclude && name && matches(exclude, name)) + (include != null && (!name || !matches(include, name))) || + (exclude != null && name && matches(exclude, name)) ) { current = vnode return rawVNode From f8ce9d6041fa6158d9b756ad8b6fb252120b52e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E4=B8=9C?= Date: Mon, 12 Aug 2024 16:32:34 +0800 Subject: [PATCH 3/3] fix(keep-alive): add test case --- .../__tests__/components/KeepAlive.spec.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/runtime-core/__tests__/components/KeepAlive.spec.ts b/packages/runtime-core/__tests__/components/KeepAlive.spec.ts index 902563032b2..5aa3b80284b 100644 --- a/packages/runtime-core/__tests__/components/KeepAlive.spec.ts +++ b/packages/runtime-core/__tests__/components/KeepAlive.spec.ts @@ -568,6 +568,16 @@ describe('KeepAlive', () => { await nextTick() assertHookCalls(one, [2, 2, 1, 1, 1]) assertHookCalls(two, [1, 1, 1, 1, 0]) + + excludeRef.value = '' + await nextTick() + assertHookCalls(one, [2, 2, 1, 1, 1]) + assertHookCalls(two, [1, 1, 1, 1, 0]) + + excludeRef.value = 'two' + await nextTick() + assertHookCalls(one, [2, 2, 1, 1, 1]) + assertHookCalls(two, [1, 1, 1, 1, 1]) }) test('on include change + view switch', async () => {