Describe the bug#
useSuspenseQueries will request data again right after receiving a response. As a side effect, this causes the result of useSuspenseQueries to change, which will trigger useEffects unnecessarily and under the right conditions, infinite query requesting.
Your minimal, reproducible example#
https://codesandbox.io/p/sandbox/tanstack-query-bug-forked-gzjkr9?file=%2Fsrc%2FApp.js%3A30%2C56
Steps to reproduce#
- use
useSuspenseQueries and mock multiple requests with promises and setTimeout
- set a suspense boundary on the component that will the do the requesting
- console log the requests being done and the responses received
- reload the page and watch for the re-requesting of data
Expected behavior#
I expect the data request to happen once.
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
OS: MacOS
Browser: Chrome
Version: 119.0.6045.199
Tanstack Query adapter#
react-query
TanStack Query version#
5.12.2
TypeScript version#
No response
Additional context#
No response
There is likely a problem with how we setup
staleTimein suspense. Usually, with a singleuseSuspenseQuery, any small staleTime is "good enough", because the query suspends, and then the component renders immediately after the request returns. We set the staleTime to 1 second:https://github.com/TanStack/query/blob/e7f6d3ae8b2f4230ec8cbea6f4cb8d4af83b7655/packages/react-query/src/suspense.ts#L24-L30
to a avoid a refetch-on-mount here.
However, with
useSuspenseQueries, we suspend until all queries have finished. Now the first query might finish fast, but the others take a longer time, in which case there will be a refetch for the first query because when the component that uses it finally mounts, more thanstaleTimetime has passed already.Your example though is flawed. You only see two useEffect calls because of
StrictMode. data is stable. But we can reproduce the issue I'm talking about if we set thetimeoutto a higher value than the defaultstaleTime, in which case we will seeCall query Nmore than once. This is what I would phrase as an issue, not the multiple effects being called. Your example actually doesn't show multiple requests being made, at least for me 😅Here's a fork with what I mean.
The log output I'm seeing is:
This is exactly because of the
staleTimerefetch you get on mount. query 3 only fetches once because it finishes last. The expected output imo would be:However, I don't really know how we can reliably prevent a refetch on the first mount after the query has suspended :(