@tanstack/react-query 5.56.1 → 5.59.0

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/src/types.ts CHANGED
@@ -101,7 +101,10 @@ export type UseQueryResult<
101
101
  export type UseSuspenseQueryResult<
102
102
  TData = unknown,
103
103
  TError = DefaultError,
104
- > = OmitKeyof<DefinedQueryObserverResult<TData, TError>, 'isPlaceholderData'>
104
+ > = OmitKeyof<
105
+ DefinedQueryObserverResult<TData, TError>,
106
+ 'isPlaceholderData' | 'promise'
107
+ >
105
108
 
106
109
  export type DefinedUseQueryResult<
107
110
  TData = unknown,
@@ -123,7 +126,7 @@ export type UseSuspenseInfiniteQueryResult<
123
126
  TError = DefaultError,
124
127
  > = OmitKeyof<
125
128
  DefinedInfiniteQueryObserverResult<TData, TError>,
126
- 'isPlaceholderData'
129
+ 'isPlaceholderData' | 'promise'
127
130
  >
128
131
 
129
132
  export interface UseMutationOptions<
@@ -1,27 +1,29 @@
1
1
  'use client'
2
2
  import * as React from 'react'
3
3
 
4
- import { notifyManager } from '@tanstack/query-core'
5
- import { useQueryErrorResetBoundary } from './QueryErrorResetBoundary'
4
+ import { isServer, notifyManager } from '@tanstack/query-core'
6
5
  import { useQueryClient } from './QueryClientProvider'
7
- import { useIsRestoring } from './isRestoring'
6
+ import { useQueryErrorResetBoundary } from './QueryErrorResetBoundary'
8
7
  import {
9
8
  ensurePreventErrorBoundaryRetry,
10
9
  getHasError,
11
10
  useClearResetErrorBoundary,
12
11
  } from './errorBoundaryUtils'
12
+ import { useIsRestoring } from './isRestoring'
13
13
  import {
14
14
  ensureSuspenseTimers,
15
15
  fetchOptimistic,
16
16
  shouldSuspend,
17
+ willFetch,
17
18
  } from './suspense'
18
- import type { UseBaseQueryOptions } from './types'
19
+ import { noop } from './utils'
19
20
  import type {
20
21
  QueryClient,
21
22
  QueryKey,
22
23
  QueryObserver,
23
24
  QueryObserverResult,
24
25
  } from '@tanstack/query-core'
26
+ import type { UseBaseQueryOptions } from './types'
25
27
 
26
28
  export function useBaseQuery<
27
29
  TQueryFnData,
@@ -67,6 +69,9 @@ export function useBaseQuery<
67
69
 
68
70
  useClearResetErrorBoundary(errorResetBoundary)
69
71
 
72
+ // this needs to be invoked before creating the Observer because that can create a cache entry
73
+ const isNewCacheEntry = !client.getQueryState(options.queryKey)
74
+
70
75
  const [observer] = React.useState(
71
76
  () =>
72
77
  new Observer<TQueryFnData, TError, TData, TQueryData, TQueryKey>(
@@ -131,6 +136,25 @@ export function useBaseQuery<
131
136
  result,
132
137
  )
133
138
 
139
+ if (
140
+ defaultedOptions.experimental_prefetchInRender &&
141
+ !isServer &&
142
+ willFetch(result, isRestoring)
143
+ ) {
144
+ const promise = isNewCacheEntry
145
+ ? // Fetch immediately on render in order to ensure `.promise` is resolved even if the component is unmounted
146
+ fetchOptimistic(defaultedOptions, observer, errorResetBoundary)
147
+ : // subscribe to the "cache promise" so that we can finalize the currentThenable once data comes in
148
+ client.getQueryCache().get(defaultedOptions.queryHash)?.promise
149
+
150
+ promise?.catch(noop).finally(() => {
151
+ if (!observer.hasListeners()) {
152
+ // `.updateResult()` will trigger `.#currentThenable` to finalize
153
+ observer.updateResult()
154
+ }
155
+ })
156
+ }
157
+
134
158
  // Handle result property usage tracking
135
159
  return !defaultedOptions.notifyOnChangeProps
136
160
  ? observer.trackResult(result)