fix: enable global mutate to revalidate useSWRInfinite data with includeSpecialKeys option#4167
Open
joseph0926 wants to merge 2 commits intovercel:mainfrom
Open
fix: enable global mutate to revalidate useSWRInfinite data with includeSpecialKeys option#4167joseph0926 wants to merge 2 commits intovercel:mainfrom
joseph0926 wants to merge 2 commits intovercel:mainfrom
Conversation
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
|
Please review and merge this ! |
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 foruseSWRInfinitedata.Investigation Process
To solve this, I added a new
includeSpecialKeysflag toMutatorOptionsto create an exception in the// Skip the special useSWRInfinite and useSWRSubscription keysconditional. This seemed promising, and I wrote several edge test cases to verify the solution.However, I discovered an additional problem:
useSWRInfinitesets an_iflag 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
_iflag 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 haveincludeSpecialKeysalso handle setting the_iflag when true.Solution
This PR adds an
includeSpecialKeysoption toMutatorOptionsthat$inf$,$sub$) in the matcher function_iflag for$inf$keys to trigger full revalidationUsage
Testing
Added comprehensive test cases covering:
useSWRanduseSWRInfinitescenariosFixes #4149