Skip to content

fix(mutate): scope optimisticData and populateCache per matched key - #4286

Open
spokodev wants to merge 1 commit into
vercel:mainfrom
spokodev:w33/swr-matcher-optimistic-per-key
Open

fix(mutate): scope optimisticData and populateCache per matched key#4286
spokodev wants to merge 1 commit into
vercel:mainfrom
spokodev:w33/swr-matcher-optimistic-per-key

Conversation

@spokodev

@spokodev spokodev commented Jul 2, 2026

Copy link
Copy Markdown

When global mutate is called with a key filter function, mutateByKey runs once
for every matched key through matchedKeys.map(mutateByKey). Two variables that
get reassigned during a mutation, optimisticData and populateCache, were declared
once in the outer internalMutate scope and shared across all those calls.

On the first matched key, a functional optimisticData is called and the shared
variable is overwritten with the resulting value. Every later matched key then
sees a non-function value and reuses the first key's optimistic data instead of
computing its own. A functional populateCache has the same problem for the
committed write. The result is that multiple cache entries show the wrong
optimistic value during a filtered mutation.

This moves both variables inside mutateByKey so each matched key gets its own
copy. Single-key mutations are unaffected. The mutator data transformer already
worked per key because it uses a fresh local variable; this brings optimisticData
and populateCache in line with that.

Added a test that seeds two keys with distinct committed values and fires one
filtered mutation with a functional optimisticData. Before the change the second
key rendered the first key's optimistic value; after it renders its own.

@spokodev
spokodev requested review from huozhi and shuding as code owners July 2, 2026 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant