swr
swr copied to clipboard
Cache flaw: mutate of cache keys is ignored when no consuming hook is mounted
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.
- When ComponentA is mounted - data is loaded and put into cache
- Then we switch to ComponentB and press the button that calls "mutate" with the cache key of ComponentB.
- Then we go back to ComponentA expecting to refetch, but it doesn't.
Additional Context
SWR version: all versions