-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
keepalive include
/exclude
props don't work with <NuxtPage>
#15214
Comments
For this use case, you can pass keep alive props directly to NuxtPage to get the kind of behaviour that you want. |
I cannot find an example in the documentation, could you demonstrate ? |
Feature implemented here: nuxt/framework#7492. You would do something like: <NuxtPage :keepalive="{ include: 'index' }" /> |
I am with @Ilmion The only way I could make it work was like this in my
Coudn't make it in a per page basis with Or is not clear how this should work (https://github.com/nuxt/framework/discussions/8366) All these options wraps the page in a |
Using |
That makes more sense now. Thanks for clarifying. |
According to your explanation I came up with the following, but neither Moving the /pages
|- /ideas
|- /[id]
|- index.vue
|- index.vue
|- ideas.vue ideas.vue <template>
<div class="content">
<NuxtPage />
</div>
</template>
<script setup lang="ts">
definePageMeta({
keepalive: {
include: ['index', 'IdeaList']
}
})
</script> ideas/index.vue <template>
<div>
<IdeaList :items="items" />
</div>
</template> ideas/[id]/index.vue <template>
<div>
IdeaListItem content
</div>
</template> |
@danielroe It does not work. I have inspected it with chrome vue dev tool and here is the problem. Two joint picture from the Chrome dev tool. This bug isn't closed IMHO. |
include
/exclude
props don't work with <NuxtPage>
@danielroe any plans to fix this in the near future? I did some testing and it looks like the reason for this problem is that the direct child of KeepAlive is an anonymous component (see last comment on #14609), that has no name and for this reason Vue include/exclude does not work. The anonymous component also is the reason for #15573. As the component type below KeepAlive is always the same Vue unmounts the wrong component, resulting in a strange state. This causes KeepAlive to be unsafe to use in Nuxt at the moment, as components/composables that register onUnmounted get this hook even though the component is not unmounted. In my test, I changed the behavior so that each pageKey create its own wrapper component https://github.com/stenet/nuxt-framework-keep-alive/blob/main/packages/nuxt/src/pages/runtime/page.ts#L88. This way, these two problems were fixed. But this is probably not the best solution, since the map used grows quite fast. |
Yes, this is definitely on the roadmap and needs to be fixed. |
How can nuxt be stable with major bugs like this 😢 |
Is there any workaround for this? |
I hope this gets fixed sometime. Workaround: Use
|
@danielroe any updates on this one? Thanks |
@danielroe Doing nuxt-page inside layout/default.vue instead of slot like so Is not working as it should be. It is keeping alive the component but not excluding the excludeThis component. This was working on nuxt2 and nuxt-bridge with this syntax Is there a way to do this properly? EDIT: @vcasy I tried RouterView instead and keepAlive completely stopped working |
In the latest version v3.3.1, this bug still exists. |
After some investigation, I've noticed that the following rules:
That's why the include/exclude keepalive options don't work for There are some solutions worth to consider I've found so far;
I've also came up with a PoC code for 2nd solution, but bit hacky; --- packages/nuxt/src/pages/runtime/page.ts
+++ packages/nuxt/src/pages/runtime/page.ts
@@ -36,6 +36,8 @@ export default defineComponent({
},
setup (props, { attrs }) {
const nuxtApp = useNuxtApp()
+ const RouteProvider = defineComponent({ ...RouteProviderTemplate })
+
return () => {
return h(RouterView, { name: props.name, route: props.route, ...attrs }, {
default: (routeProps: RouterViewSlotProps) => {
@@ -44,6 +46,8 @@ export default defineComponent({
const key = generateRouteKey(routeProps, props.pageKey)
const done = nuxtApp.deferHydration()
+ RouteProvider.name = String(key)
+
const hasTransition = !!(props.transition ?? routeProps.route.meta.pageTransition ?? defaultPageTransition)
const transitionProps = hasTransition && _mergeTransitionProps([
props.transition,
@@ -77,12 +81,11 @@ function _mergeTransitionProps (routeProps: TransitionProps[]): TransitionProps
return defu(..._props)
}
-const RouteProvider = defineComponent({
- name: 'RouteProvider',
+const RouteProviderTemplate = ({
// TODO: Type props
// eslint-disable-next-line vue/require-prop-types
props: ['routeProps', 'pageKey', 'hasTransition'],
- setup (props) {
+ setup (props: any) {
// Prevent reactivity when the page will be rerendered in a different suspense fork
// eslint-disable-next-line vue/no-setup-props-destructure
const previousKey = props.pageKey |
I know the issue is being worked on by the team of nuxt, but in the mean time, does anyone have a comprehensible guide for a workaround? I don't tried this but it does not work: #15214 (comment) |
I think you need to disable keep-alive completely to for example not run into security issues from cached data that should not be cached. Enable keep-alive back with the excluded routes when this issue is fixed. When using the router directly, there are issues with the Nuxt ecosystem. |
This comment was marked as duplicate.
This comment was marked as duplicate.
+1 |
I tried it out and found that the
In summary, I hope that Nuxt can provide some temporary solutions for this issue (assuming that the API won't change) and include warnings or notifications about this problem in the documentation to help those who are facing similar problems.🌹 |
@gjssss from my conversations with Nuxt team I think they are expecting a fix in Vue 3 first |
@AndrewBogdanovTSS Indeed, I think that at least for now, some temporary solutions can be used to address this issue, or the problem can be highlighted in the Nuxt documentation. |
@gjssss absolutely agree |
nice shot |
setting app : {keepalive:true} in the nuxt config file and disabling each page i don't want to keep alive with definePageMeta({ keepalive: false }) works for me ,is there any downside using this method thanks in advance! |
There is an urgent need for someone to provide a temporary solution so that the problem can be addressed as soon as possible |
您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。
|
I think someone here already tried. The first time when a non keep-alive page is visited,the whole cache is cleared. Making the feature more or less useless. |
@danielroe @bencodezen could you please add a reference to the related vue issue here? I tried to find it but I'm not sure which one it is. Thanks! |
Any idea when this will be fixed? Or can you tell us about the fix plan for this problem? Is there any hope of solving this problem in the short term? If not, can you provide a temporary solution? |
keepalive include/exclude props don't work with |
I can't wait to try out this version, but its performance seems to be different from what I expected. The lifecycle of the component can be triggered normally, but it also triggers data requests on the page, making me feel like the page is being refreshed. However, if I only use |
I have made a series of attempts again, and currently this solution can achieve the goal I want.
|
I try it on NUXT 3.8.2, for page keepalive and it work.
note: you can also control the key by setting the key in demo: https://stackblitz.com/edit/nuxt-starter-m7k9hv?file=pages%2Falive.vue |
I don't know how to set the name for the page my code: the page src:
It can't work , can you privide a sameple code, thank you very much! |
@freezyh use this to add name
I also update the demo in the above comment. |
thank you very much, I had wright it by typescript |
Dynamically changing the include attribute will invalidate the cache. Who knows what happened |
I have the same problem |
After testing, in v3.11.1 version, has been able to solve this problem very well, here is an idea, The reason for the problem is keepalive include default read from.matched[0].components.default.__name The value is used as the name of the components, but nuxt defaults to file as the route name.
You can replace the default component name with from.name and add from.name to a list of composables or pinia states.
|
Of course, I hope that the official can match from.matched[0].components.default.__name The default value directly to the value of form.name. That would be even more perfect. |
Environment
Windows_NT
v16.16.0
3.0.0-rc.11
0.5.4
npm@8.11.0
vite
-
-
-
Reproduction
https://stackblitz.com/edit/github-xnrjjn?file=pages%2Findex.vue,pages%2Fnotkeptalive.vue
Describe the bug
App has 2 pages, one page is set to be kept alive with
definePageMeta({ keepalive: true });
the other one is not.
Changing some value in the first page, is not kept if you navigate away from it and if you come back.
Also, there is a weird display bug, as if a new mounted component of the page was added under the first one in the page before the first one being added. It make the page flicker.
If you put the same page meta in the second one (by removing the comment is the reproduction link), now it works, but the two pages are now kept alive and this is not what I want.
In base vue3, I could do something like that :
Additional context
No response
Logs
No response
The text was updated successfully, but these errors were encountered: