From 2040a6c15258da0dafb6461ec287060078ae61f7 Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Tue, 10 Dec 2024 19:45:22 +0100 Subject: [PATCH 1/2] fix(query-core): respect initialData for queryClient.ensureQueryData we used to call `getQueryData` before queryCache.build(), but building is what creates the query and potentially adds initialData. I tried to make that more obvious by reading directly from `query.state.data` --- .../query-core/src/__tests__/queryClient.test.tsx | 15 +++++++++++++++ packages/query-core/src/queryClient.ts | 7 +++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/query-core/src/__tests__/queryClient.test.tsx b/packages/query-core/src/__tests__/queryClient.test.tsx index ec01f5e272..11f2129db9 100644 --- a/packages/query-core/src/__tests__/queryClient.test.tsx +++ b/packages/query-core/src/__tests__/queryClient.test.tsx @@ -477,6 +477,21 @@ describe('queryClient', () => { }), ).resolves.toEqual('new') }) + + test('should not fetch with initialDat', async () => { + const key = queryKey() + const queryFn = vi.fn().mockImplementation(() => Promise.resolve('data')) + + await expect( + queryClient.ensureQueryData({ + queryKey: [key, 'id'], + queryFn, + initialData: 'initial', + }), + ).resolves.toEqual('initial') + + expect(queryFn).toHaveBeenCalledTimes(0) + }) }) describe('ensureInfiniteQueryData', () => { diff --git a/packages/query-core/src/queryClient.ts b/packages/query-core/src/queryClient.ts index a62a6ee9c2..7b4cf1dfdc 100644 --- a/packages/query-core/src/queryClient.ts +++ b/packages/query-core/src/queryClient.ts @@ -145,14 +145,13 @@ export class QueryClient { >( options: EnsureQueryDataOptions, ): Promise { - const cachedData = this.getQueryData(options.queryKey) + const defaultedOptions = this.defaultQueryOptions(options) + const query = this.#queryCache.build(this, defaultedOptions) + const cachedData = query.state.data if (cachedData === undefined) { return this.fetchQuery(options) } else { - const defaultedOptions = this.defaultQueryOptions(options) - const query = this.#queryCache.build(this, defaultedOptions) - if ( options.revalidateIfStale && query.isStaleByTime(resolveStaleTime(defaultedOptions.staleTime, query)) From 28b6fcc6cf44d0ef5378c4dcd8b566b1af86ca01 Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Tue, 10 Dec 2024 19:46:50 +0100 Subject: [PATCH 2/2] refactor: simplify condition --- packages/query-core/src/queryClient.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/query-core/src/queryClient.ts b/packages/query-core/src/queryClient.ts index 7b4cf1dfdc..72c3a42cbf 100644 --- a/packages/query-core/src/queryClient.ts +++ b/packages/query-core/src/queryClient.ts @@ -151,16 +151,16 @@ export class QueryClient { if (cachedData === undefined) { return this.fetchQuery(options) - } else { - if ( - options.revalidateIfStale && - query.isStaleByTime(resolveStaleTime(defaultedOptions.staleTime, query)) - ) { - void this.prefetchQuery(defaultedOptions) - } + } - return Promise.resolve(cachedData) + if ( + options.revalidateIfStale && + query.isStaleByTime(resolveStaleTime(defaultedOptions.staleTime, query)) + ) { + void this.prefetchQuery(defaultedOptions) } + + return Promise.resolve(cachedData) } getQueriesData<