Skip to content

Commit 1638c02

Browse files
0xdiidautofix-ci[bot]TkDodo
authored
fix(query-core): don't update dataUpdateCount when setting initial data (#9743)
* fix isFetchedAfterMount * changeset * ci: apply automated fixes * call setState directly * changeset update * successState * ci: apply automated fixes * just set success state --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
1 parent 4878815 commit 1638c02

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

.changeset/eight-webs-buy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/query-core': patch
3+
---
4+
5+
Fixed isFetchedAfterMount in cases where initialData is applied

packages/query-core/src/__tests__/query.test.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,4 +1304,47 @@ describe('query', () => {
13041304
data: 'data1',
13051305
})
13061306
})
1307+
1308+
test('should not increment dataUpdateCount when setting initialData on prefetched query', async () => {
1309+
const key = queryKey()
1310+
const queryFn = vi.fn().mockImplementation(() => 'fetched-data')
1311+
1312+
// First prefetch the query (creates query without data)
1313+
queryClient.prefetchQuery({
1314+
queryKey: key,
1315+
queryFn,
1316+
})
1317+
1318+
const query = queryCache.find({ queryKey: key })!
1319+
expect(query.state.data).toBeUndefined()
1320+
expect(query.state.dataUpdateCount).toBe(0)
1321+
1322+
// Now create an observer with initialData
1323+
const observer = new QueryObserver(queryClient, {
1324+
queryKey: key,
1325+
queryFn,
1326+
initialData: 'initial-data',
1327+
})
1328+
1329+
// The query should now have the initial data but dataUpdateCount should still be 0
1330+
// since this was not fetched data but initial data
1331+
expect(query.state.data).toBe('initial-data')
1332+
expect(query.state.dataUpdateCount).toBe(0)
1333+
1334+
// Get the initial state as captured by the observer
1335+
const result = observer.getCurrentResult()
1336+
expect(result.data).toBe('initial-data')
1337+
expect(result.isFetchedAfterMount).toBe(false) // This should be false since no actual fetch occurred
1338+
1339+
// Now trigger a refetch through the observer to simulate real-world usage
1340+
await observer.refetch()
1341+
1342+
// After actual fetch, dataUpdateCount should increment
1343+
expect(query.state.dataUpdateCount).toBe(1)
1344+
expect(query.state.data).toBe('fetched-data')
1345+
1346+
// And isFetchedAfterMount should now be true
1347+
const updatedResult = observer.getCurrentResult()
1348+
expect(updatedResult.isFetchedAfterMount).toBe(true)
1349+
})
13071350
})

packages/query-core/src/query.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,9 @@ export class Query<
210210
if (this.state && this.state.data === undefined) {
211211
const defaultState = getDefaultState(this.options)
212212
if (defaultState.data !== undefined) {
213-
this.setData(defaultState.data, {
214-
updatedAt: defaultState.dataUpdatedAt,
215-
manual: true,
216-
})
213+
this.setState(
214+
successState(defaultState.data, defaultState.dataUpdatedAt),
215+
)
217216
this.#initialState = defaultState
218217
}
219218
}
@@ -635,12 +634,8 @@ export class Query<
635634
case 'success':
636635
const newState = {
637636
...state,
638-
data: action.data,
637+
...successState(action.data, action.dataUpdatedAt),
639638
dataUpdateCount: state.dataUpdateCount + 1,
640-
dataUpdatedAt: action.dataUpdatedAt ?? Date.now(),
641-
error: null,
642-
isInvalidated: false,
643-
status: 'success' as const,
644639
...(!action.manual && {
645640
fetchStatus: 'idle' as const,
646641
fetchFailureCount: 0,
@@ -710,6 +705,16 @@ export function fetchState<
710705
} as const
711706
}
712707

708+
function successState<TData>(data: TData | undefined, dataUpdatedAt?: number) {
709+
return {
710+
data,
711+
dataUpdatedAt: dataUpdatedAt ?? Date.now(),
712+
error: null,
713+
isInvalidated: false,
714+
status: 'success' as const,
715+
}
716+
}
717+
713718
function getDefaultState<
714719
TQueryFnData,
715720
TError,

0 commit comments

Comments
 (0)