nuxt icon indicating copy to clipboard operation
nuxt copied to clipboard

keep-alive calls unmount a component when I change a page but does not call mount when I return back

Open dxvladislavvolkov opened this issue 3 years ago • 5 comments

Environment

Nuxt Version: 3.0.0

Reproduction

https://github.com/dxvladislavvolkov/keepalive

npm i npm run dev Click on page 1 then page 3 (you will see mounted text in the console ) then page 4 (you will see unmounted in the console) then page 3 - the component doesn't call mounеed even though the unmounted hook was called

We have a problem with devextreme-vue components because they clean all content on the unmounted hook. And components need mounted hook to be called

Describe the bug

We have a problem with devextreme-vue components because they clean all content on the unmounted hook. And components need mounted hook to be called

Additional context

No response

Logs

No response

dxvladislavvolkov avatar Nov 22 '22 05:11 dxvladislavvolkov

I think that no onUnmounted should be called here at all, but only onDeactivated.

stenet avatar Nov 22 '22 11:11 stenet

I went down the rabbit hole a little deeper. It looks like the problem is occurring because the onUnmounted lifecycle hook is not matching KeepAlive.

KeepAlive removes the least recently accessed cached instance, but the onUnmounted lifecycle hook is called for the instance in the cache that was last accessed. Because of this, there is now a mismatch between the cached instances and the actual components.

In @dxvladislavvolkov's example, you can see this behavior if you click on Page 3, refresh the browser, and than click Page 1, Page 2, Page 4 and Page 5. If you never click on page 3 again, you will never see the onUnmounted hook from page 3.

I've been trying to find the Nuxt code for this, but have absolutely no idea where to start 🫤

stenet avatar Nov 22 '22 14:11 stenet

Hi, any news?

dxvladislavvolkov avatar Nov 25 '22 06:11 dxvladislavvolkov

I’ve figured out that …

definePageMeta({
    keepalive: true
});

inside a page component does not work.

It only works by setting the [keepalive] attribute to the <NuxtPage> component in the app.vue.

Seems like there is a bug.

JonathanDoelan avatar Jan 27 '23 09:01 JonathanDoelan

After testing, I believe this issue has been fixed with commit 1fde49c0f57cc50fedf91366a274c9759d1d9a39 in VueJS (>= 3.2.46).

VueJs now checkes for the VNode type + key instead of only the VNode type.

stenet avatar Feb 02 '23 06:02 stenet