Skip to content

Commit 98cc1f9

Browse files
authored
fix(KeepAlive): when exclude prop change, it should prune cache that not matched (#2111)
1 parent d4bf9bc commit 98cc1f9

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

packages/runtime-core/__tests__/components/KeepAlive.spec.ts

+55-2
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,25 @@ describe('KeepAlive', () => {
494494
return { viewRef, includeRef }
495495
}
496496

497-
test('on include/exclude change', async () => {
497+
function setupExclude() {
498+
const viewRef = ref('one')
499+
const excludeRef = ref('')
500+
const App = {
501+
render() {
502+
return h(
503+
KeepAlive,
504+
{
505+
exclude: excludeRef.value
506+
},
507+
() => h(views[viewRef.value])
508+
)
509+
}
510+
}
511+
render(h(App), root)
512+
return { viewRef, excludeRef }
513+
}
514+
515+
test('on include change', async () => {
498516
const { viewRef, includeRef } = setup()
499517

500518
viewRef.value = 'two'
@@ -513,7 +531,26 @@ describe('KeepAlive', () => {
513531
assertHookCalls(two, [1, 1, 1, 1, 0])
514532
})
515533

516-
test('on include/exclude change + view switch', async () => {
534+
test('on exclude change', async () => {
535+
const { viewRef, excludeRef } = setupExclude()
536+
537+
viewRef.value = 'two'
538+
await nextTick()
539+
assertHookCalls(one, [1, 1, 1, 1, 0])
540+
assertHookCalls(two, [1, 1, 1, 0, 0])
541+
542+
excludeRef.value = 'one'
543+
await nextTick()
544+
assertHookCalls(one, [1, 1, 1, 1, 1])
545+
assertHookCalls(two, [1, 1, 1, 0, 0])
546+
547+
viewRef.value = 'one'
548+
await nextTick()
549+
assertHookCalls(one, [2, 2, 1, 1, 1])
550+
assertHookCalls(two, [1, 1, 1, 1, 0])
551+
})
552+
553+
test('on include change + view switch', async () => {
517554
const { viewRef, includeRef } = setup()
518555

519556
viewRef.value = 'two'
@@ -529,6 +566,22 @@ describe('KeepAlive', () => {
529566
assertHookCalls(two, [1, 1, 1, 1, 1])
530567
})
531568

569+
test('on exclude change + view switch', async () => {
570+
const { viewRef, excludeRef } = setupExclude()
571+
572+
viewRef.value = 'two'
573+
await nextTick()
574+
assertHookCalls(one, [1, 1, 1, 1, 0])
575+
assertHookCalls(two, [1, 1, 1, 0, 0])
576+
577+
excludeRef.value = 'two'
578+
viewRef.value = 'one'
579+
await nextTick()
580+
assertHookCalls(one, [1, 1, 2, 1, 0])
581+
// two should be pruned
582+
assertHookCalls(two, [1, 1, 1, 1, 1])
583+
})
584+
532585
test('should not prune current active instance', async () => {
533586
const { viewRef, includeRef } = setup()
534587

packages/runtime-core/src/components/KeepAlive.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ const KeepAliveImpl = {
175175
() => [props.include, props.exclude],
176176
([include, exclude]) => {
177177
include && pruneCache(name => matches(include, name))
178-
exclude && pruneCache(name => matches(exclude, name))
178+
exclude && pruneCache(name => !matches(exclude, name))
179179
}
180180
)
181181

0 commit comments

Comments
 (0)