@tanstack/query-core 4.35.3 → 4.36.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/query-core",
3
- "version": "4.35.3",
3
+ "version": "4.36.1",
4
4
  "description": "The framework agnostic core that powers TanStack Query",
5
5
  "author": "tannerlinsley",
6
6
  "license": "MIT",
package/src/hydration.ts CHANGED
@@ -139,19 +139,15 @@ export function hydrate(
139
139
  )
140
140
  })
141
141
 
142
- queries.forEach((dehydratedQuery) => {
143
- const query = queryCache.get(dehydratedQuery.queryHash)
144
-
145
- // Reset fetch status to idle in the dehydrated state to avoid
146
- // query being stuck in fetching state upon hydration
147
- const dehydratedQueryState = {
148
- ...dehydratedQuery.state,
149
- fetchStatus: 'idle' as const,
150
- }
142
+ queries.forEach(({ queryKey, state, queryHash }) => {
143
+ const query = queryCache.get(queryHash)
151
144
 
152
145
  // Do not hydrate if an existing query exists with newer data
153
146
  if (query) {
154
- if (query.state.dataUpdatedAt < dehydratedQueryState.dataUpdatedAt) {
147
+ if (query.state.dataUpdatedAt < state.dataUpdatedAt) {
148
+ // omit fetchStatus from dehydrated state
149
+ // so that query stays in its current fetchStatus
150
+ const { fetchStatus: _ignored, ...dehydratedQueryState } = state
155
151
  query.setState(dehydratedQueryState)
156
152
  }
157
153
  return
@@ -162,10 +158,15 @@ export function hydrate(
162
158
  client,
163
159
  {
164
160
  ...options?.defaultOptions?.queries,
165
- queryKey: dehydratedQuery.queryKey,
166
- queryHash: dehydratedQuery.queryHash,
161
+ queryKey,
162
+ queryHash,
163
+ },
164
+ // Reset fetch status to idle to avoid
165
+ // query being stuck in fetching state upon hydration
166
+ {
167
+ ...state,
168
+ fetchStatus: 'idle',
167
169
  },
168
- dehydratedQueryState,
169
170
  )
170
171
  })
171
172
  }
@@ -427,7 +427,7 @@ describe('dehydration and rehydration', () => {
427
427
  queryClient.clear()
428
428
  })
429
429
 
430
- test('should set the fetchStatus to idle in all cases when dehydrating', async () => {
430
+ test('should set the fetchStatus to idle when creating a query with dehydrate', async () => {
431
431
  const queryCache = new QueryCache()
432
432
  const queryClient = createQueryClient({ queryCache })
433
433
 
@@ -466,4 +466,36 @@ describe('dehydration and rehydration', () => {
466
466
  hydrate(hydrationClient, parsed)
467
467
  expect(hydrationCache.find(['string'])?.state.fetchStatus).toBe('idle')
468
468
  })
469
+
470
+ test('should not change fetchStatus when updating a query with dehydrate', async () => {
471
+ const queryClient = createQueryClient()
472
+
473
+ const options = {
474
+ queryKey: ['string'],
475
+ queryFn: async () => {
476
+ await sleep(10)
477
+ return 'string'
478
+ },
479
+ } as const
480
+
481
+ await queryClient.prefetchQuery(options)
482
+
483
+ const dehydrated = dehydrate(queryClient)
484
+ expect(
485
+ dehydrated.queries.find((q) => q.queryHash === '["string"]')?.state
486
+ .fetchStatus,
487
+ ).toBe('idle')
488
+ const stringified = JSON.stringify(dehydrated)
489
+
490
+ // ---
491
+ const parsed = JSON.parse(stringified)
492
+ const hydrationCache = new QueryCache()
493
+ const hydrationClient = createQueryClient({ queryCache: hydrationCache })
494
+
495
+ const promise = hydrationClient.prefetchQuery(options)
496
+ hydrate(hydrationClient, parsed)
497
+ expect(hydrationCache.find(['string'])?.state.fetchStatus).toBe('fetching')
498
+ await promise
499
+ expect(hydrationCache.find(['string'])?.state.fetchStatus).toBe('idle')
500
+ })
469
501
  })