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

Infinite loop when to Suspense mode with cacheTime: 0 #2367

Closed
nghiepdev opened this issue Jun 11, 2021 · 11 comments · Fixed by #2821
Closed

Infinite loop when to Suspense mode with cacheTime: 0 #2367

nghiepdev opened this issue Jun 11, 2021 · 11 comments · Fixed by #2821
Labels
bug Something isn't working released suspense

Comments

@nghiepdev
Copy link

Describe the bug
Suspense mode does not working when to set cacheTime: 0, data will never resolve and infinite loop API call.
I just fork example from https://react-query.tanstack.com/guides/suspense and add cacheTime: 0 option.

To Reproduce
https://codesandbox.io/s/confident-darwin-6oz9t?file=/src/index.js
Steps to reproduce the behavior:

  1. Click on "Show Project" button
  2. Check the Network tab
  3. See loop problem

Screenshots
image

Desktop (please complete the following information):

  • OS: macOS
  • Browser: Chrome
  • Version: 3.17.0
@TkDodo
Copy link
Collaborator

TkDodo commented Jun 11, 2021

confirmed. I first thought it has something to do with prefetchQuery, which might put it in the cache and then remove it again instantly because the cacheTime is 0, but that wasn't it, and it also works if we set the cacheTime to something like 1000. Do you have any leads on this? I'm not a suspense user tbh ...

@nghiepdev
Copy link
Author

Temporarily to solve this issue I'm using cacheTime: 1 instead 😭

@arnaudbzn
Copy link
Contributor

Reproduced the issue with Suspense while testing with jest and set the QueryClient defaultOptions cacheTime to 0 (see #1847).
Workaround using @nghiepit cacheTime: 1.

@TkDodo
Copy link
Collaborator

TkDodo commented Oct 14, 2021

should we have this workaround built-in? We have something similar for staleTime and suspense:

https://github.com/tannerlinsley/react-query/blob/ee7ca0bf1e813fd727f25abeb67755f43b636b3f/src/react/useBaseQuery.ts#L56-L62

It seems similar, so we could set defaultedOptions.cacheTime to 1 if defaultedOptions.suspense and defaultedOptions.cacheTime is 0. What do you think?

@vcarel
Copy link

vcarel commented Oct 14, 2021

Is it fine to use a cacheTime of 1? It will not randomly do infinite loops from time to time?

@TkDodo
Copy link
Collaborator

TkDodo commented Oct 14, 2021

cacheTime of 1 means unused items will be removed from the cache after 1ms, while 0 means they will be removed instantly. I think the distinction is irrelevant for most cases. Also, cacheTime defaults to 5 minutes, so this only covers cases where it was purposefully set to zero

@arnaudbzn
Copy link
Contributor

It seems similar, so we could set defaultedOptions.cacheTime to 1 if defaultedOptions.suspense and defaultedOptions.cacheTime is 0. What do you think?

It's a patch that we'll prevent infinite loops with useInfiniteQuery. So I'm fine with this solution.
We can also maybe add cacheTime: 0 in the "Testing" doc section (along with "Turn off retries").

@TkDodo
Copy link
Collaborator

TkDodo commented Oct 14, 2021

would you like to contribute it?

@arnaudbzn
Copy link
Contributor

would you like to contribute it?

@TkDodo Sure!, both code and docs?

@TkDodo
Copy link
Collaborator

TkDodo commented Oct 21, 2021

@arnaudbzn yes please :)

@tannerlinsley
Copy link
Collaborator

🎉 This issue has been resolved in version 3.28.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released suspense
Projects
None yet
5 participants