question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

<keep-alive> doesn't `destroy` cached components after when max is 1

See original GitHub issue

Version

2.6.10

Reproduction link

https://jsfiddle.net/p0te9vL3/

Steps to reproduce

  1. Open dev tools to see console
  2. Cycle through the routes
  3. View console and observe lifecycle hook logging

What is expected?

I’d expect once the max cache is reached (in the example case of 1), the component that gets pruned also gets destroyed. In the documentation it states (emphasis mine):

max

The maximum number of component instances to cache. Once this number is reached, the cached component instance that was least recently accessed will be destroyed before creating a new instance.

What is actually happening?

The pruned component is never destroyed, it’s just removed from the cache. In the example, the beforeDestroy hook is never called.


Related code: https://github.com/vuejs/vue/blob/636c9b4ef17f2062720b677cbbe613f146f4d4db/src/core/components/keep-alive.js#L37-L49

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
posvacommented, May 6, 2019

it will cache 1 component, so as soon as you switch the cache is occupied by the new component and the previous component is removed. A cache of one entry here is the same as not having any cache

0reactions
RichieChoocommented, Feb 2, 2021

@zrh122 are you sure? I haven’t tested but the pr #10015 introduced a very small change that does not appear in yours. In any case, it would be nice if we could merge both for different issues, so both contributions are taken into account 🙂

Yes, in my code i delay to call pruneCacheEntry until component mounted or updated, at that time this._vnode is equal to variable vnode.

It may cause other problem, such as last router will fresh when activated

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I destroy a cached Vue component from keep-alive?
You can call this.$destroy() before user close the tab and delete all of data and event binding in that one.
Read more >
Yes, this is how to cache pages by url with Vue, Vue Router ...
BONUS 1: You can use the max prop on the keep-alive component to limit the number of cached items. That would invalidate cache...
Read more >
KeepAlive | Vue.js
<KeepAlive> is a built-in component that allows us to conditionally cache component instances when dynamically switching between multiple components.
Read more >
records.config — Apache Traffic Server 9.1.4 documentation
After you modify records.config , run the command traffic_ctl config reload to ... Enabling keep-alive does not automatically enable purging of keep-alive ......
Read more >
3 Tuning the GlassFish Server - Oracle Help Center
The EJB container caches and pools EJB components for better performance. ... After this time, the bean is destroyed if the bean in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found