Skip to content

Commit 1299ad6

Browse files
fix: make sure queries which have dataUpdatedAt of 0 will be run (TanStack#1556)
1 parent d25ab3e commit 1299ad6

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/core/query.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -464,18 +464,18 @@ export class Query<
464464

465465
const hasInitialData = typeof options.initialData !== 'undefined'
466466

467-
const initialDataUpdatedAt =
468-
hasInitialData &&
469-
(typeof options.initialDataUpdatedAt === 'function'
467+
const initialDataUpdatedAt = hasInitialData
468+
? typeof options.initialDataUpdatedAt === 'function'
470469
? (options.initialDataUpdatedAt as () => number | undefined)()
471-
: options.initialDataUpdatedAt)
470+
: options.initialDataUpdatedAt
471+
: 0
472472

473473
const hasData = typeof data !== 'undefined'
474474

475475
return {
476476
data,
477477
dataUpdateCount: 0,
478-
dataUpdatedAt: hasData ? initialDataUpdatedAt || Date.now() : 0,
478+
dataUpdatedAt: hasData ? initialDataUpdatedAt ?? Date.now() : 0,
479479
error: null,
480480
errorUpdateCount: 0,
481481
errorUpdatedAt: 0,

src/react/tests/useQuery.test.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,6 +2107,37 @@ describe('useQuery', () => {
21072107
})
21082108
})
21092109

2110+
it('should fetch if "initial data updated at" is exactly 0', async () => {
2111+
const key = queryKey()
2112+
const states: UseQueryResult<string>[] = []
2113+
2114+
function Page() {
2115+
const state = useQuery(key, () => 'data', {
2116+
staleTime: 10 * 1000, // 10 seconds
2117+
initialData: 'initial',
2118+
initialDataUpdatedAt: 0,
2119+
})
2120+
states.push(state)
2121+
return null
2122+
}
2123+
2124+
renderWithClient(queryClient, <Page />)
2125+
2126+
await sleep(100)
2127+
2128+
expect(states.length).toBe(2)
2129+
expect(states[0]).toMatchObject({
2130+
data: 'initial',
2131+
isStale: true,
2132+
isFetching: true,
2133+
})
2134+
expect(states[1]).toMatchObject({
2135+
data: 'data',
2136+
isStale: false,
2137+
isFetching: false,
2138+
})
2139+
})
2140+
21102141
it('should keep initial data when the query key changes', async () => {
21112142
const key = queryKey()
21122143
const states: UseQueryResult<{ count: number }>[] = []

0 commit comments

Comments
 (0)