`isFetchedAfterMount` was broken in commit 1c8a921 / v5.87.1 #9656
Describe the bug#
Hello!
It appears that isFetchedAfterMount was broken in 5.87.1 through that change inadvertently updating the dataUpdateCount. We're seeing behavior where the isLoading flag is set to false when data is loaded from cache, but since isFetchedAfterMount is inadvertently set to true too early we can't tell if that data was loaded from the server or from the cache. In essence, I believe that in cases where you are persisting data the isFetchedAfterMount flag might be entirely broken.
From Claude's analysis:
Root Cause Analysis#
The commit 1c8a92167769f322d0206325b98493769afb0961 introduced logic that updates initialData when a Query exists without data. The issue is that when setData is called with manual: true, it increments dataUpdateCount from 0 to 1, even though this data wasn't actually fetched from the server but just set as initial data.
The Problem Flow:#
- Prefetch scenario: A query is created via prefetching (data is undefined, status is pending)
- Observer mounts with initialData: When an observer mounts with
initialDataset - setOptions is called: The new code in
query.ts:210-218detects the query has no data and sets the initialData - setData with manual:true: This calls
setDatawithmanual: true, which incrementsdataUpdateCountfrom 0 to 1 - queryInitialState capture: The observer captures the query state as
#currentQueryInitialStatewithdataUpdateCount: 1 - isFetchedAfterMount calculation: When calculated, it compares current
dataUpdateCount(1) with initialdataUpdateCount(1), resulting in false initially, but any subsequent update makes it true prematurely
Your minimal, reproducible example#
https://codesandbox.io/p/github/0xdiid/simple-is-fetched-repro/main?import=true
Steps to reproduce#
Steps are in the repro app itself, but a quick copypaste here:
- Click "1. Prefetch Query" - Creates query with undefined data, but starts fetching with a 3 second delay
- Click "2. Mount Observer with initialData" - Triggers the bug (if pressed before 3 seconds)
- If you clicked 2 before 3 seconds, you should see isFetchedAfterMount be true which is not expected
- You can now click "3. Reset" to reset the state and wait for 3 seconds after clicking 1 to see flags set "properly"
Expected behavior#
As a user, I expect isFetchedAfterMount to only trigger when the data is fetched from outside of the persisted cache, which matches previous implementations
How often does this bug happen?#
Every time
Screenshots or Videos#
https://github.com/user-attachments/assets/77765709-a4e5-46f6-af3b-e565fc483e2a
Platform#
- OS: mac
- Browser: chrome
- Version: 140.0
Tanstack Query adapter#
react-query
TanStack Query version#
v5.87.1
TypeScript version#
v5.9.2
Additional context#
No response
TkDodo added labelspackage: query-core
this might just mean instead of calling
setDatahere:we just do
this.setState(defaultState)? I think I didn't do that because it also puts thefetchStatusback toidle, which isn't what we want.setDatawithmanualavoids this here:So I think we need a middle ground between
setDatawith manual and just setting thedefaultStateDo you want to take a look at this?
I'm not sure I have enough context on the repo/lib as a whole to make a proper suggestion here, but it does seem at a glance the
setDatawithmanualandstate.databeingundefinedwould only be hit in this newly added edge case right? So you could just do something like this:diff --git a/packages/query-core/src/query.ts b/packages/query-core/src/query.ts index a34c8630d..8e85a75d3 100644 --- a/packages/query-core/src/query.ts +++ b/packages/query-core/src/query.ts @@ -636,7 +636,9 @@ export class Query< const newState = { ...state, data: action.data, - dataUpdateCount: state.dataUpdateCount + 1, + dataUpdateCount: action.manual && state.data === undefined + ? state.dataUpdateCount // Don't increment for initial data + : state.dataUpdateCount + 1, dataUpdatedAt: action.dataUpdatedAt ?? Date.now(), error: null, isInvalidated: false,That manages to pass tests, but again I'm not sure I have the full context to know if it would break other things.
@TkDodo any timeline on a fix here? Does the above proposal look good?
This is blocking us from updating the package, and we're now starting to miss out on other improvements. Would love to get this into a release soon.
Went ahead and put it up for review cc @mihoward21 @TkDodo
TkDodo closed this
Sign in to join this conversation.
yeah that seems legit. when
initialDatais applied, we should keepdataUpdatedCountto whatever it was (likely zero).