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

onBeforeUnmount hook in useQuery is undesirable when used in pinia store #1538

Closed
ADALimtec opened this issue Mar 4, 2024 · 1 comment · Fixed by #1545
Closed

onBeforeUnmount hook in useQuery is undesirable when used in pinia store #1538

ADALimtec opened this issue Mar 4, 2024 · 1 comment · Fixed by #1545

Comments

@ADALimtec
Copy link

ADALimtec commented Mar 4, 2024

Describe the bug
I was recently investigating a bug where various refetch calls for queries where not fetching data anymore after some time on the page. What I found is that queries are being stopped internally by Apollo through the onBeforeUnmount hook. While this might make sense on components, it can get troublesome on pinia stores where they are currently defined in my project. The hooks defined in a store are directed to the component the store is initialized on, and once that component is unmounted it also stops the query, even if it is still in use through the store. This might pose no problem if the store was initialized e.g. on the root component which never is unmounted, but is undesirable if initialized on any component that will unmount during the page's lifetime.

To Reproduce
Steps to reproduce the behavior:

  1. Create a pinia store and use useQuery.
  2. Initialize the pinia store (= call the use[..]Store for the first time) on a component that you can unmount through any means.
  3. Unmount the component.
  4. Call refetch from the useQuery defined in the store - it will return undefined, the query object from useQuery will be null, and the data - if being changed - will not update.

Expected behavior
In pinia stores, queries should not be stopped internally at all, regardless of the component they were first initialized on.

Versions
vue: 3.4.20
vue-apollo: 4.0.1
@apollo/client: 3.9.5

Additional context
A workaround for this issue would be to initialize every store that uses useQuery on the root component to ensure that the onBeforeUnmount hook will never be called before the lifetime of the page ends.

@676962537
Copy link

676962537 commented Mar 25, 2024

I encountered the same problem,After reading your explanation,And I also checked the source code,You're right,Your solution is ok,I initially wanted to package <keep-alive></keep-alive> outside the <router-view></router-view>,But this is not a good solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants