Infinite loop when to Suspense mode with cacheTime: 0 #2367
Send to my agentDescribe 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:
- Click on "Show Project" button
- Check the
Networktab - See loop problem
Screenshots

Desktop (please complete the following information):
- OS: macOS
- Browser: Chrome
- Version: 3.17.0
TkDodo added labelsbug
TkDodo added labelssuspense
Temporarily to solve this issue I'm using
cacheTime: 1instead 😭Reproduced the issue with Suspense while testing with jest and set the
QueryClientdefaultOptionscacheTimeto0(see #1847). Workaround using @nghiepitcacheTime: 1.should we have this workaround built-in? We have something similar for staleTime and suspense:
It seems similar, so we could set
defaultedOptions.cacheTimeto1ifdefaultedOptions.suspenseanddefaultedOptions.cacheTimeis0. What do you think?Is it fine to use a cacheTime of 1? It will not randomly do infinite loops from time to time?
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
It seems similar, so we could set
defaultedOptions.cacheTimeto1ifdefaultedOptions.suspenseanddefaultedOptions.cacheTimeis0. 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 addcacheTime: 0in the "Testing" doc section (along with "Turn off retries").would you like to contribute it?
would you like to contribute it?
@TkDodo Sure!, both code and docs?
@arnaudbzn yes please :)
TkDodo closed this
:tada: This issue has been resolved in version 3.28.0 :tada:
The release is available on:
Your semantic-release bot :package::rocket:
tannerlinsley added labelsreleased
Sign in to join this conversation.
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 ...