Custom cache strategy and matching rules for KeepAlive #283
Replies: 15 comments 6 replies
-
We really need these features. |
Beta Was this translation helpful? Give feedback.
-
Hope to merge soon. |
Beta Was this translation helpful? Give feedback.
-
This is nice. I'm only worried about the Wouldn't it be possible to only have the Another solution would be calling a method on the What was the point of adding |
Beta Was this translation helpful? Give feedback.
-
In vue2.x I can custom a component to replace KeepAlive.But in vue3.x its impossible. |
Beta Was this translation helpful? Give feedback.
-
I realy realy need it, otherwise I would need to change a lot of project code 😢 |
Beta Was this translation helpful? Give feedback.
-
Hello, I have to say in advance that I don't quite understand the proposed solution fully. It looks too complex for the task that I/other people want to accomplish based on what I have read (vuejs/core#2077, https://stackoverflow.com/questions/70113769/manually-remove-and-destroy-a-component-from-keep-alive, https://stackoverflow.com/questions/65163775/how-to-destroy-unmount-vue-js-3-components, https://stackoverflow.com/questions/68995879/vue-3-force-unmount-when-deactivated) to name a few. The current situation is - most of the solutions ether involve using For example, if we have cached instances of the same <router-view v-slot="{ Component }">
<keep-alive :include="Item,SomethingElse">
<component :is="Component" :key="$route.fullPath" />
</keep-alive>
</router-view> Given that we visited the page 3 times under different IDs:
Would the proposed solution allow to destroy/unmount a specific instance upon an event in app? Let's say user removed the item from the database so we want the cache of Overall it would be good to know when the solution will be implemented (or the latest status of it), given that after 2 months it's going to be a year since this topic was created. Edit (19.01.2022)In addition to above, I would also like to empathize on required (personally required/subjective) caching customization granularity. If I understand correctly - So let's say we have 3 components we want to cache
If we set (currently) maximum to 1 then every single component is allowed to have 1 cached instance. Is this correct? In comparison, in real world scenario, for the |
Beta Was this translation helpful? Give feedback.
-
I have another solution, it's mini change for current API and easy to implement.
export interface KeepAliveCache {
include: Ref<MatchPattern | undefined>
exclude: Ref<MatchPattern | undefined>
max: Ref<number | string | undefined>
cache: Map<string, VNode>
remove: (key: string) => void
clear: () => void
}
Here is a full example <template>
<button @click="onCloseTab">Close Current Tab</button>
<button @click="onCloseAllTabs">Close All Tabs</button>
<router-view v-slot="{ Component, route }">
<keep-alive :cache="pageCache">
<component :is="Component" :key="route.fullPath" />
</keep-alive>
</router-view>
</template>
<script lang="ts">
import { defineComponent, createKeepAliveCache, ref } from 'vue'
export default defineComponent({
name: 'App',
setup() {
const currentPath = ref('')
const pageCache = createKeepAliveCache()
pageCache.include.value = ['Component1']
function onCloseTab() {
pageCache.remove(currentPath.value)
}
function onCloseAllTabs() {
pageCache.clear()
}
return {
pageCache,
onCloseTab,
onCloseAllTabs
}
}
})
</script> I document this in a RFC , more detail please check RFC Link And I also did a prototype implement base on this RFC, currently it's work. Implement Link |
Beta Was this translation helpful? Give feedback.
-
Any plan about this keep-alive RFC? |
Beta Was this translation helpful? Give feedback.
-
呜呜 请官方下场看看,很久了 |
Beta Was this translation helpful? Give feedback.
-
Adding my vote for this feature |
Beta Was this translation helpful? Give feedback.
-
really need |
Beta Was this translation helpful? Give feedback.
-
I'm waiting this too for managing component states easier, I hope we'll see this feature coming this year. <3 |
Beta Was this translation helpful? Give feedback.
-
To anybody still interested in this, I found a way of making it work, although it's still not super great. I'm on Vue 2.7.10 and vue-router 3.6.5.
The parent component within the top level router-link contains this:
The trick is in whatever component you're rendering as a child with multiple versions, for example Add a
All of this needs to happen inside whatever is being displayed in the router-link. There definitely needs to be a better API for managing cached state, but at the very least this avoids the most common pitfalls of having multiple child components clog up memory and ignore whatever |
Beta Was this translation helpful? Give feedback.
-
I agree with @pavlexander. Many of the RFC's referenced issues (and what brought me here today) are tied to the simple fact that Vue 3 has no way to remove a single component from keep-alive's cache as we could indirectly with Vue 2's component $destroy() method. While I appreciate the RFC author is trying to address multiple cache issues including this missing Vue 3 cache capability, the proposed RFC change is substantial and we are not seeing any incremental cache improvements because they are all tied to one big overhaul change to caching. I think we should try to break down caching changes; as a start, at least into two parts (1) address control of removing a single item and (2) fine-grain cache control (e.g. implementation, hooks). In my opinion, #1 is a core capability of KeepAlive or any caching system. As a suggestion on how to implement #1, we could employ the provide/inject mechanism of Vue 3 here. (Vue 2 is reaching EOL soon and is out-of-scope at this point.) KeepAlive component can provide some cache-service object and any enclosing component can inject this object. As a start, the cache-service object can provide one simple method that the component can use to remove items including itself; e.g. |
Beta Was this translation helpful? Give feedback.
-
Discussion Thread for "Custom cache strategy and matching rules for KeepAlive"
Links
Beta Was this translation helpful? Give feedback.
All reactions