Skip to content

fix(keep-alive): bug prune oldest entry #10015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/components/keep-alive.js
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@ export default {
keys.push(key)
// prune oldest entry
if (this.max && keys.length > parseInt(this.max)) {
pruneCacheEntry(cache, keys[0], keys, this._vnode)
pruneCacheEntry(cache, keys[0], keys, vnode)
}
}

50 changes: 50 additions & 0 deletions test/unit/features/component/component-keep-alive.spec.js
Original file line number Diff line number Diff line change
@@ -572,6 +572,56 @@ describe('Component keep-alive', () => {
}).then(done)
})

// #10015
it('prune cache on max set 1', done => {
const spyA = jasmine.createSpy()
const spyB = jasmine.createSpy()
const spyAD = jasmine.createSpy()
const spyBD = jasmine.createSpy()

function assertCount (calls) {
expect([
spyA.calls.count(),
spyAD.calls.count(),
spyB.calls.count(),
spyBD.calls.count(),
]).toEqual(calls)
}

const vm = new Vue({
template: `
<keep-alive max="1">
<component :is="n"></component>
</keep-alive>
`,
data: {
n: 'aa'
},
components: {
aa: {
template: '<div>a</div>',
created: spyA,
destroyed: spyAD
},
bb: {
template: '<div>bbb</div>',
created: spyB,
destroyed: spyBD
}
}
}).$mount()

assertCount([1, 0, 0, 0])
vm.n = 'bb'
waitForUpdate(() => {
// should prune A because max cache reached
assertCount([1, 1, 1, 0])
vm.n = 'aa'
}).then(() => {
assertCount([2, 1, 1, 1])
}).then(done)
})

it('should warn unknown component inside', () => {
new Vue({
template: `<keep-alive><foo/></keep-alive>`