Conversation
This branch has not been deployed
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.
When several
useSWRSubscriptionhooks share a key, the subscription is created once by the hook that mounts first, and itsnextcallback writes data through that hook'sswr.mutate. That bound mutate always targets the hook's current key (keyRef.current), not the key the subscription was created for. So if the first hook switches to another key while another hook keeps the shared subscription alive, every update for the old key is written into the cache of the new key. The hook that stayed on the old key stops updating, and the hook that moved shows data from the wrong source. Errors are not affected becausenextalready writes them with a cache helper bound tosubscriptionKey.The fix writes data with
internalMutate(cache, subscriptionKey, data, false), the same key the error path uses.Test:
should keep updating a shared key after one hook switches keysintest/use-swr-subscription.test.tsx. One component has two hooks onkeyA, switches the first one tokeyB, then emits onkeyA. Before the fix it rendersa2:a1(thekeyBhook got thekeyAdata and thekeyAhook is stale) and the test fails. After the fix it rendersundefined:a2and the test passes. The other subscription tests,tsc, oxlint and oxfmt pass.