diff --git a/packages/query-core/src/query.ts b/packages/query-core/src/query.ts index 3c19ce149d..6018f5071d 100644 --- a/packages/query-core/src/query.ts +++ b/packages/query-core/src/query.ts @@ -250,7 +250,7 @@ export class Query< isStale(): boolean { return ( this.state.isInvalidated || - !this.state.dataUpdatedAt || + this.state.data === undefined || this.#observers.some((observer) => observer.getCurrentResult().isStale) ) } @@ -258,7 +258,7 @@ export class Query< isStaleByTime(staleTime = 0): boolean { return ( this.state.isInvalidated || - !this.state.dataUpdatedAt || + this.state.data === undefined || !timeUntilStale(this.state.dataUpdatedAt, staleTime) ) } @@ -329,7 +329,7 @@ export class Query< fetchOptions?: FetchOptions, ): Promise { if (this.state.fetchStatus !== 'idle') { - if (this.state.dataUpdatedAt && fetchOptions?.cancelRefetch) { + if (this.state.data !== undefined && fetchOptions?.cancelRefetch) { // Silently cancel current fetch if the user wants to cancel refetch this.cancel({ silent: true }) } else if (this.#promise) { @@ -546,7 +546,7 @@ export class Query< fetchStatus: canFetch(this.options.networkMode) ? 'fetching' : 'paused', - ...(!state.dataUpdatedAt && { + ...(state.data === undefined && { error: null, status: 'pending', }), diff --git a/packages/query-core/src/queryObserver.ts b/packages/query-core/src/queryObserver.ts index b972ab3fef..506a353dc8 100644 --- a/packages/query-core/src/queryObserver.ts +++ b/packages/query-core/src/queryObserver.ts @@ -444,7 +444,7 @@ export class QueryObserver< fetchStatus = canFetch(query.options.networkMode) ? 'fetching' : 'paused' - if (!state.dataUpdatedAt) { + if (state.data === undefined) { status = 'pending' } } @@ -536,6 +536,7 @@ export class QueryObserver< const isError = status === 'error' const isLoading = isPending && isFetching + const hasData = state.data !== undefined const result: QueryObserverBaseResult = { status, @@ -558,10 +559,10 @@ export class QueryObserver< state.errorUpdateCount > queryInitialState.errorUpdateCount, isFetching, isRefetching: isFetching && !isPending, - isLoadingError: isError && state.dataUpdatedAt === 0, + isLoadingError: isError && !hasData, isPaused: fetchStatus === 'paused', isPlaceholderData, - isRefetchError: isError && state.dataUpdatedAt !== 0, + isRefetchError: isError && hasData, isStale: isStale(query, options), refetch: this.refetch, } @@ -683,7 +684,7 @@ function shouldLoadOnMount( ): boolean { return ( options.enabled !== false && - !query.state.dataUpdatedAt && + query.state.data === undefined && !(query.state.status === 'error' && options.retryOnMount === false) ) } @@ -694,7 +695,7 @@ function shouldFetchOnMount( ): boolean { return ( shouldLoadOnMount(query, options) || - (query.state.dataUpdatedAt > 0 && + (query.state.data !== undefined && shouldFetchOn(query, options, options.refetchOnMount)) ) } diff --git a/packages/query-core/src/tests/query.test.tsx b/packages/query-core/src/tests/query.test.tsx index 632bc8636f..909cdbb6a0 100644 --- a/packages/query-core/src/tests/query.test.tsx +++ b/packages/query-core/src/tests/query.test.tsx @@ -841,6 +841,24 @@ describe('query', () => { expect(initialDataUpdatedAtSpy).toHaveBeenCalled() }) + test('should work with initialDataUpdatedAt set to zero', async () => { + const key = queryKey() + + await queryClient.prefetchQuery({ + queryKey: key, + queryFn: () => 'data', + staleTime: Infinity, + initialData: 'initial', + initialDataUpdatedAt: 0, + }) + + expect(queryCache.find({ queryKey: key })?.state).toMatchObject({ + data: 'initial', + status: 'success', + dataUpdatedAt: 0, + }) + }) + test('queries should be garbage collected even if they never fetched', async () => { const key = queryKey() diff --git a/packages/query-persist-client-core/src/__tests__/createPersister.test.ts b/packages/query-persist-client-core/src/__tests__/createPersister.test.ts index e7ae8f3538..9aece2fcd2 100644 --- a/packages/query-persist-client-core/src/__tests__/createPersister.test.ts +++ b/packages/query-persist-client-core/src/__tests__/createPersister.test.ts @@ -207,7 +207,7 @@ describe('createPersister', () => { ) await persisterFn(queryFn, context, query) - query.state.dataUpdatedAt = 0 + query.state.data = 'data0' query.fetch = vi.fn() expect(query.state.dataUpdatedAt).toEqual(0)