Open brandones opened 1 year ago
Ping, would love some support on this
Running into a similar issue where the global mutate imported from 'swr' does not trigger a revalidation for hook instances that are using mutable keys, in my case an object. If I have a useSwr call with an object as a key, and then somewhere else call the global mutate
with a copy of that object, there is no attempted revalidation in the hook.
Even if I create a memoized object and then pass that into both the hook and global mutate directly this behavior persists, so I don't think its a "same" object issue.
I never figured out why this issue was happening; why it was the case that mutate was only being called on immutable keys. The deeper problem, which my project resolved, was that the SWR cache was not being shared across the different microfrontends in my application. So if one microfrontend called mutate
it would only run that on the useSWR
calls that had been made within that microfrontend. Sharing the SWR cache across the application allows the mutate
call to act on the useSWR
calls from other parts of the application. The fact that mutate
seemed to get called on the useSWRImmutable
keys remains mysterious, but it is no longer my mystery.
Bug report
Description / Observed Behavior
Calling
mutate
with a filter function key only runs that filter function on the key that usesuseSWRImmutable
.Expected Behavior
I expect the filter function to be called on all existing SWR keys except the ones using
useSWRImmutable
.Repro Steps / Code Example
I need to update and display orders. The display code is in a different microfrontend and different module from the update code.
The order display hook:
The order update hook (tried this with both
useSWRConfig
and withimport { mutate } from "swr"
):On calling that mutate function, the entire console log output is
or sometimes
Indicating that the only call that has its key checked are these:
and a similar one using
useSWRImmutable
for/ws/rest/v1/concept...
Here is the cache right before the POST is made and the update function is called:
Before:![image](https://github.com/vercel/swr/assets/1031876/4008252f-047e-4976-accc-e54d35fe2220)
After:![image](https://github.com/vercel/swr/assets/1031876/d62fa0f2-d805-4b23-9b66-d2703f4c86b6)
Additional Context
SWR 2.2.1.
App uses a microfrontends framework, so calls to SWR functions do not necessarily happen in the same React tree.