You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.staleTime of Infinity to 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 staleTime of 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
The text was updated successfully, but these errors were encountered:
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).
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 with staleTime: 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.
Describe the bug
A prefetched query with
staleTime
ofInfinity
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):
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:
queryClient.defaultOptions.queries.staleTime
ofInfinity
to avoid any refetching of a query down the Suspense tree.staleTime
of 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 exampleInfinity
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
TanStack Query version
v5.62.11
The text was updated successfully, but these errors were encountered: