Skip to content

Commit b7cf7fc

Browse files
committed
wip
1 parent 2ad266e commit b7cf7fc

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

packages/react-query/src/useBaseQuery.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ export function useBaseQuery<
7070
useClearResetErrorBoundary(errorResetBoundary)
7171

7272
// this needs to be invoked before creating the Observer because that can create a cache entry
73-
const isNewCacheEntry = !client
74-
.getQueryCache()
75-
.get(defaultedOptions.queryHash)
73+
const cacheEntry = client.getQueryCache().get(defaultedOptions.queryHash)
7674

7775
const [observer] = React.useState(
7876
() =>
@@ -82,6 +80,7 @@ export function useBaseQuery<
8280
),
8381
)
8482

83+
8584
const result = observer.getOptimisticResult(defaultedOptions)
8685

8786
React.useSyncExternalStore(
@@ -130,6 +129,7 @@ export function useBaseQuery<
130129
>(defaultedOptions.queryHash),
131130
})
132131
) {
132+
console.log('throwing error')
133133
throw result.error
134134
}
135135

@@ -138,12 +138,32 @@ export function useBaseQuery<
138138
result,
139139
)
140140

141+
console.log(
142+
'prefetchInRender',
143+
defaultedOptions.experimental_prefetchInRender,
144+
{
145+
willFetch: willFetch(result, isRestoring),
146+
isRestoring: isRestoring,
147+
isLoading: result.isLoading,
148+
isFetching: result.isFetching,
149+
isServer: isServer,
150+
},
151+
)
141152
if (
142153
defaultedOptions.experimental_prefetchInRender &&
143154
!isServer &&
144155
willFetch(result, isRestoring)
145156
) {
146-
const promise = isNewCacheEntry
157+
const cacheEntryState = cacheEntry?.state
158+
159+
const shouldFetch =
160+
!cacheEntryState ||
161+
(cacheEntryState.data === undefined &&
162+
cacheEntryState.status === 'pending' &&
163+
cacheEntryState.fetchStatus === 'idle')
164+
165+
console.log({ shouldFetch })
166+
const promise = shouldFetch
147167
? // Fetch immediately on render in order to ensure `.promise` is resolved even if the component is unmounted
148168
fetchOptimistic(defaultedOptions, observer, errorResetBoundary)
149169
: // subscribe to the "cache promise" so that we can finalize the currentThenable once data comes in

0 commit comments

Comments
 (0)