fix: enable global mutate to revalidate useSWRInfinite data with includeSpecialKeys option #4167
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
After investigating this issue, I traced its origins back to [Issue #1670](#1670) where it was first reported. Following this, [RFC #1946](#1946) mentioned improvements that would address this problem, and the RFC discussion explicitly stated that "special keys would be excluded." This was then implemented in [PR #1989](#1989) where special keys were indeed excluded.
However, the problem is that global mutate ignores special keys - specifically the
$inf$
keys - causing revalidation failures foruseSWRInfinite
data.Investigation Process
To solve this, I added a new
includeSpecialKeys
flag toMutatorOptions
to create an exception in the// Skip the special useSWRInfinite and useSWRSubscription keys
conditional. This seemed promising, and I wrote several edge test cases to verify the solution.However, I discovered an additional problem:
useSWRInfinite
sets an_i
flag in the cache when calling its own mutate, which the fetcher checks to determine whether to refetch all pages. The issue was that global mutate had no way to set this flag (at least not within the scope of what a regular contributor could modify in a single PR).While there are several potential solutions - like automatically setting the
_i
flag during fetcher traversal (though this makes responsibility boundaries unclear) or adding a new flag (which would mean adding two flags just for global mutate) - the final solution was to haveincludeSpecialKeys
also handle setting the_i
flag when true.Solution
This PR adds an
includeSpecialKeys
option toMutatorOptions
that$inf$
,$sub$
) in the matcher function_i
flag for$inf$
keys to trigger full revalidationUsage
Testing
Added comprehensive test cases covering:
useSWR
anduseSWRInfinite
scenariosFixes #4149