A Prefetched Infinity State Time Query Gets Fetched Again on Mount #8482
Send to my agentDescribe the bug#
A prefetched query with staleTime of Infinity always triggers an unexpected fetch again when the component with suspenseful query(staleTime: 0) is mounted.
Steps to reproduce#
I would like to achieve these following requirements(a very contrived one):
- Display a post list 1 to 3.
- Initially display the details of the first post.
- When the user clicks a list, always fetches the latest details.
I have provided an example here(StackBlitz): A Prefetched Infinity State Time Query Gets Fetched Again on Mount
Here is how I achieved those requirements:
- For "Display a post list 1 to 3", I render(hardcoded) a list of 3 posts.
- For "Initially display the details of the first post" I have read Prefetching & Router Integration. I know that the first post details will be needed ahead of time, so I prefetch early in the Suspense tree with the
queryClient.defaultOptions.queries.staleTimeofInfinityto avoid any refetching of a query down the Suspense tree. - For "When the user clicks a list, always fetches the latest details" I use a state of a selected post id as a query key of a suspenseful post query with
staleTimeof 0.
Expected behavior#
The behavior seems to be as expected, but when I observed the network request, there are 2 unexpected requests for the first post(instead of only one request). If I try to make the post query to have staleTime of non-zero for example Infinity there would be only 1 request but I lose the ability to always fetch for the latest post.
How often does this bug happen?#
Every time
Platform#
- OS: macOS
- Browser: Chrome
- Version: 131.0.6778.205 (Official Build) (arm64)
TanStack Query version#
v5.62.11
Here is what I found in the source:
- When the query is executed for the first time, the QueryObserver is initialized with the query.
- To construct the QueryObserver, its constructor is called, in which, eventually, it overrides the current query options (in this case, the prefetch query with
staleTime: Infinity) with query client options (in this case, the post query withstaleTime: 0). The exact line of execution is this then this. - From this point on, it loses track with an earlier prefetched query, and proceeds to fetch the post query.
At this point, I feel like, maybe, it is an expected behavior. For what I want to achieve, there might be some other way.
staleTimeis not a Query setting, but an Observer level setting. The value describes how this observer views the data - is it outdated from their perspective or not.staleTimeon imperative methods likefetchQuery,prefetchQuery,ensureQueryDataorusePrefetchQuerydoes something slightly different. As those methods don’t create an observer, we just take them to see if cached data can be returned from this call because it’s still fresh.So when you call
usePrefetchQuery(postQueryOptions(1)), it gets executed withstaleTime:Infinity, which means the prefetch will only run if there is no data at all in the cache. It will run only once.But then, your observer has
staleTime:0, which means it will always want a background refetch.There is also a “special” situation with
suspense: Because suspense unmounts your component, the combination of suspense +staleTime:0will always result in a background refetch. We guard ourselves against that here:But since you set the
staleTimeto0manually, you overwrite that.
The possible solutions here are:
- don’t use suspense - a normal
useQuerywithstaleTime:0on the observer will work as expected. - set a small
staleTimeon the observer - something like 500ms. This is enough for react to render your component after data has fetched so that the query mounting can be successfully de-duplicated. You will still get a refetch when you switch between posts unless you switch within half a second, which seems like a good enough trade-off.
- don’t use suspense - a normal
TkDodo closed this
TkDodo added labelsworks as designed
TkDodo added labelshas workaround
Thank you @TkDodo, for not only providing the workaround but also explaining the existing behavior 🙏.
Since you mentioned that:
There is also a “special” situation with suspense..
I think it is loosely related with this PR, #8398.
Would you mind sharing me some insight of why it has to be
staleTime: 1000, rather than any other arbitrary number? Without any configurations, by default, if the rendering takes longer than 1000ms, it would then refetch? I believe it was first introduced since V3.1 second is indeed quite arbitrary. it doesn’t matter how long the fetch takes, but yes, we’d expect the component to finish rendering after that time in order to avoid an immediate “refetchOnMount”.
I see, thank you for your time for sharing this 🙏.
Sign in to join this conversation.
After debugging locally, the behavior that I found is the prefetched data is not resolved yet then the query is fetched resulting in 2 fetches for the same thing(but different in stale time).