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.
Proposal for
mutateTag
FunctionThis is just a proposal, so it doesn't mean we have a consensus to spport this.
Overview
The
mutateTag
function proposes a straightforward method to mutate cached data by using tags. This feature allows developers to manage mutations more effectively, enhancing ease of use and flexibility. Below is an example of how this function can be utilized.Changes
add a new
mutateTag
functionmutateTag
has a similar interface, which looks likeadd
tag
options touseSWR
,useSWRInfinite
Motivation
The
mutateTag
function enables cache mutation based on assigned tags. This approach offers several advantages over the existing "Mutate Multiple Items" method.Simplicity
While a key filter function provides flexibility, it can become complex when keys are not organized. Developers often use URLs as keys, which can lead to cumbersome mutations. For instance:
This method is too low-level and relies on URLs that may not be the best abstraction for handling mutations.
With
mutateTag
, the process is streamlined:Enhanced Support for
useSWRInfinite
A long-standing challenge with SWR has been the limited ability to perform mutations for
useSWRInfinite
, which typically require bound mutate functions. The existing "Mutate Multiple Items" technique is insufficient because it depends on internal cache data tied to specific format keys. We can use an unstable APIunstable_serialize
to mutate the internal cache data but we want to shield developers from the complexities of these special keys, making "Mutate Multiple Items" less viable.This also can't cover patterns like
useSWRInfinite
with filtering queries.For example, we have multiple keys with
useSWRInfinite
, which has different filtering conditions, and then mutated a user of the list/api/users?page=1&filter=admin
/api/users?page=2&filter=admin
/api/users?page=1&filter=none
In this case, it's impossible to mutate the cache data unless we iterate cache data directly. We can't use "Mutate Multiple Items" because it skips internal cache data for
useSWRInfinite
.By utilizing
mutateTag
, developers can effectively mutate this cached data without needing to interact with any special format keys:This will make mutations for
useSWRInfinite
drastically simple and safe.What about
useSWRSubscription
?This is a good point, Initially I considered supporting
useSWRSubscription
as well, butuseSWRSubscription
doesn't supportmutate
, so I didn't have confident to includeuseSWRSubscription
.(I could add it later if that makes sense)
Prior Art
This proposal draws inspiration from similar functionalities, such as revalidateTag in Next.js, which showcases the effectiveness of tag-based cache management.