Skip to content
Source
Sign inSign up
TanStack/query

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

Send to my agent
Closednghiepdev(GitHub profile, opens in a new tab) opened this issue 11 comments

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
  1. 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 ...

  2. TkDodo added labelsbug

  3. TkDodo added labelssuspense

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

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

  6. 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?

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

  8. 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

  9. 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").

  10. would you like to contribute it?

  11. would you like to contribute it?

    @TkDodo Sure!, both code and docs?

  12. @arnaudbzn yes please :)

  13. TkDodo closed this

  14. :tada: This issue has been resolved in version 3.28.0 :tada:

    The release is available on:

    Your semantic-release bot :package::rocket:

  15. tannerlinsley added labelsreleased

Sign in to join this conversation.