Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A Prefetched Infinity State Time Query Gets Fetched Again on Mount #8482

Open
kivvvvv opened this issue Dec 29, 2024 · 2 comments
Open

A Prefetched Infinity State Time Query Gets Fetched Again on Mount #8482

kivvvvv opened this issue Dec 29, 2024 · 2 comments

Comments

@kivvvvv
Copy link

kivvvvv commented Dec 29, 2024

Describe 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):

  1. Display a post list 1 to 3.
  2. Initially display the details of the first post.
  3. 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:

  1. For "Display a post list 1 to 3", I render(hardcoded) a list of 3 posts.
  2. 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.
  3. 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

@kivvvvv
Copy link
Author

kivvvvv commented Jan 1, 2025

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

@kivvvvv
Copy link
Author

kivvvvv commented Jan 1, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant