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

Cache flaw: mutate of cache keys is ignored when no consuming hook is mounted #2964

Open
3plusalpha opened this issue May 13, 2024 · 0 comments

Comments

@3plusalpha
Copy link

Bug report

Description / Observed Behavior

When using mutate from const { mutate } from 'swr'; to globally enforce a revalidation,
then the mutate is silently ignored for those cases where a consuming useSWR hook useSWR(key) is not mounted.

This renders the global mutate method as unreliable because a developer can never be sure that the cache marked by the key is actually marked as stale and the content is refetched.

Expected Behavior

When calling mutate from const { mutate } from 'swr'; with an arbitrary key the cache is marked stale and with the next mount of a consuming hook using that key the data is refetched.
When the cache wasn't marked as stale in the first place the cache would be used on mount

Repro Steps / Code Example

Given are one route component and two child components: ComponentA and ComponentB.
ComponentA fetches data and uses a cache key.
ComponentB contains a button that calls mutate.

ComponentA and ComponentB are never mounted at the same time.

  1. When ComponentA is mounted - data is loaded and put into cache
  2. Then we switch to ComponentB and press the button that calls "mutate" with the cache key of ComponentB.
  3. Then we go back to ComponentA expecting to refetch, but it doesn't.

Additional Context

SWR version: all versions

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