From 1bcaddb10832ba496ce1a42cc2cc2fa331dfbb47 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Wed, 30 Aug 2023 21:44:59 -0600 Subject: [PATCH] Don't write to the cache in markResult if variables don't match --- src/core/QueryInfo.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/core/QueryInfo.ts b/src/core/QueryInfo.ts index c147a7eccd2..61e4e2d8481 100644 --- a/src/core/QueryInfo.ts +++ b/src/core/QueryInfo.ts @@ -342,14 +342,20 @@ export class QueryInfo { variables: WatchQueryOptions["variables"] ) { const { lastWrite } = this; - return !( - lastWrite && - // If cache.evict has been called since the last time we wrote this - // data into the cache, there's a chance writing this result into - // the cache will repair what was evicted. - lastWrite.dmCount === destructiveMethodCounts.get(this.cache) && - equal(variables, lastWrite.variables) && - equal(result.data, lastWrite.result.data) + return ( + // In case variables have changed and there is a race condition where the + // previous variables request finishes after the current variables + // request, skip the cache update. + equal(this.variables, variables) && + !( + lastWrite && + // If cache.evict has been called since the last time we wrote this + // data into the cache, there's a chance writing this result into + // the cache will repair what was evicted. + lastWrite.dmCount === destructiveMethodCounts.get(this.cache) && + equal(variables, lastWrite.variables) && + equal(result.data, lastWrite.result.data) + ) ); }