Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global mutate fails when cache provider is overridden #2799

Open
TrevorBurnham opened this issue Sep 21, 2023 · 0 comments
Open

Global mutate fails when cache provider is overridden #2799

TrevorBurnham opened this issue Sep 21, 2023 · 0 comments

Comments

@TrevorBurnham
Copy link

Bug report

The mutate function from

import { mutate } from "swr";

is a no-op when a custom cache provider is used.

Description / Observed Behavior

I switched some of my code over to using the global mutate function and found that it worked fine in my application, but failed in my unit tests: Performing the mutation failed to trigger re-render in components that consumed the relevant data. I eventually tracked down the difference in behavior to the custom cache provider I was using, as advised in the docs (Reset Cache Between Test Cases).

Expected Behavior

I'd expected the mutate function imported directly from swr to have the same behavior as

const { mutate } = useSWRConfig()

since this difference isn't noted in the Global Mutate docs.

In implementation terms, this difference makes sense: The custom cache provider is scoped to components rendered under that <SWRConfig>, and the global mutate function can't be scoped in that way. But as a user, it's very surprising that the global mutate function would stop working in my unit tests because I'm following the recommended steps for resetting the cache.

Some possible solutions, in order of preference:

  1. Provide a more direct way of resetting the cache than using a custom provider, e.g. a global resetCache function that would clear the default provider cache. I could call it in a beforeEach when running my tests.
  2. Update the docs for Reset Cache Between Test Cases to warn about this side effect.

Additional Context

swr v2.2.2

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

No branches or pull requests

1 participant