From 1a28687e9b0a085c69154ef7458e312250e0c417 Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Sat, 27 Aug 2022 23:04:57 -0400 Subject: [PATCH 1/2] Use better check for "forced query" in `queryThunk` condition check --- packages/toolkit/src/query/core/buildThunks.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/toolkit/src/query/core/buildThunks.ts b/packages/toolkit/src/query/core/buildThunks.ts index e912922d1f..6da388bb85 100644 --- a/packages/toolkit/src/query/core/buildThunks.ts +++ b/packages/toolkit/src/query/core/buildThunks.ts @@ -480,16 +480,18 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".` const fulfilledVal = requestState?.fulfilledTimeStamp // Order of these checks matters. - // In order for `upsertQueryData` to successfully run while an existing request is - /// in flight, we have to check `isForcedQuery` before `status === 'pending'`, - // otherwise `queryThunk` will bail out and not run at all. - - // if this is forced, continue - if (isForcedQuery(arg, state)) return true + // In order for `upsertQueryData` to successfully run while an existing request is in flight, + /// we have to check for that first, otherwise `queryThunk` will bail out and not run at all. + const isUpsertQuery = + typeof arg[forceQueryFnSymbol] === 'function' && arg.forceRefetch + if (isUpsertQuery) return true // Don't retry a request that's currently in-flight if (requestState?.status === 'pending') return false + // if this is forced, continue + if (isForcedQuery(arg, state)) return true + // Pull from the cache unless we explicitly force refetch or qualify based on time if (fulfilledVal) // Value is cached and we didn't specify to refresh, skip it. From d2e200a11843ceaaab094f4a4f0f25e9f048ef2f Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Sat, 27 Aug 2022 23:15:16 -0400 Subject: [PATCH 2/2] Also run handlers if action has hydration info --- packages/toolkit/src/query/core/buildMiddleware/index.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/toolkit/src/query/core/buildMiddleware/index.ts b/packages/toolkit/src/query/core/buildMiddleware/index.ts index 840d7a03bb..09f2412e12 100644 --- a/packages/toolkit/src/query/core/buildMiddleware/index.ts +++ b/packages/toolkit/src/query/core/buildMiddleware/index.ts @@ -91,8 +91,12 @@ export function buildMiddleware< // This looks for actions that aren't specific to the API slice windowEventsHandler(action, mwApi, stateBefore) - if (isThisApiSliceAction(action)) { - // Only run these additional checks if the actions are part of the API slice + if ( + isThisApiSliceAction(action) || + context.hasRehydrationInfo(action) + ) { + // Only run these additional checks if the actions are part of the API slice, + // or the action has hydration-related data for (let handler of handlers) { handler(action, mwApi, stateBefore) }