Skip to content
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

Possible memory leak example in the docs could be a feature useSafeWatch #23043

Closed
RodrigoProjects opened this issue Sep 6, 2023 · 3 comments

Comments

@RodrigoProjects
Copy link
Contributor

RodrigoProjects commented Sep 6, 2023

Environment

Every Environment

Reproduction

I really believe a reproduction is not needed but tell me otherwise and I create one.

Describe the bug

Regarding useLazyAsyncData example: https://nuxt.com/docs/api/composables/use-lazy-async-data#example

image

We are making a watch call in an async context in this example, after further investigation I found some docs in Vue 3 oficial site (https://vuejs.org/guide/essentials/watchers.html#stopping-a-watcher) that say the automatic process of stopping watchers does not work in async contexts.

My workaround for this was making a new hook called useSafeWatch that looks something like this:

./useSafeWatch.ts

export default useSafeWatch() {
  const watchersToBeCleaned = []
  onBeforeUnmount(() => {
    watchersToBeCleaned.forEach(stopWatcher => stopWatcher())
  })
  return {
    watch: (...args) => {
      watchersToBeCleaned.push(watch(...args))
    }
  }
}

./usingTheHook.vue

<script setup>
const { watch } = useSafeWatch()
</script>

Another solution is making something like unctx and store the current context (vue instance in this case) in globalThis and window but this seems to be an upstream solution...

Is this something worth implementing a PR? Computeds seem to be another issue here?

Additional context

No response

Logs

No response

@RodrigoProjects
Copy link
Contributor Author

I see this being a problem at client side, but I don't fully grasp if watchers stay bond on the current vue instance or only on the dependency array? Maybe someone can share some insights :)

@manniL
Copy link
Member

manniL commented Dec 27, 2023

I think the first step would be to check if it actually leads to a client-side memory leak in Nuxt 🤔

For example by:

  1. Creating two pages, registering such watchers
  2. Navigate between them 1000x times and check the memory consumption graph

@manniL
Copy link
Member

manniL commented Apr 3, 2024

Resolved via #26554

@manniL manniL closed this as not planned Won't fix, can't repro, duplicate, stale Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants