Clear dedupingInterval of an unmounted key
See original GitHub issueBug report
Description
I am developing a simple CRUD application with two views:
App.jsx
<Route path="/" component={User} />
<Route path="/edit" component={Edit} />
User.jsx
const { data } = useSWR('/api/user', fetcher, { dedupingTime: 60000 });
return (
<h1>{data.name}</h1>
)
Edit.jsx
const editName = (name) => {
fetch('/api/user/edit', { method: 'post', name })
mutate('/api/user') // here User.jsx is unmounted
}
return (
<button onClick={editName}>EDIT NAME</button>
)
If the user follows these steps:
- Navigate to ‘/’
- Navigate to ‘/edit’
- Click on edit name button
- Come back to ‘/’ after 1 second
/api/user data contains an old cached value.
I’ve tried to manually delete the cache value instead of calling mutate, but the issue remains. CONCURRENT_PROMISES remains fullfilled with the previous payload.
const editName = (name) => {
fetch('/api/user/edit', { method: 'post', name })
cache.delete('/api/user')
}
Expected Behavior
After calling mutate on Edit.jsx, I expect that a revalidation occurs when User.jsx is mounted, ignoring dedupingInterval value. In other words, dedupingInterval should be cleared if mutate is called on an unmounted key.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:17 (6 by maintainers)
Top Results From Across the Web
useSWR() and mutate() do not behave as expected when ...
I am having an issue with some unexpected behavior with regards to mutate(key). In my data fetching code, I have these hooks/functions:
Read more >Question about useSWR and mutate behavior when ... - Reddit
Greetings, I am having an issue with some unexpected behavior with regards to mutate(key). In my data fetching code, ...
Read more >API - SWR
SWR is a React Hooks library for data fetching. SWR first returns the data from cache (stale), then sends the fetch request (revalidate),...
Read more >How to understand the request deduplication in SWR
The options dedupingInterval. Awesome! Now we have a clear understanding of how the request is being triggered and which onSuccess will be ...
Read more >swr: Versions | Openbase
The mutation above will match all 3 keys and set the values to undefined (clear them), and skip the revalidation at the end....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@shuding Right, but then we can just fallback to a regular
mutate(key)
call in case that a globalfetcher
is not provided, yet still have this behavior by default when there is a configured globalfetcher
.What’s more doing a
mutate(key, fetcher(key))
opts out of some built-in features such asdeduping
, but that one in particular I think is irrelevant, since I can’t think of a single case where one would domutate(key, fetcher(key))
multiple times following a single mutation request to the server.However, when
mutate(key)
is called in a case where not mounted component has calleduseSWR(key)
I would expect the behavior logic to follow something like this:This I think illustrates the bigger issue with my expectations about what a
mutate
call should do when no mounted component is callinguseSWR(key)
at this very moment. Doing amutate(key)
should at the very least remove the entry from cache in order to allow theuseSWR(key)
call to fetch a fresh resource. In this way, even ifuseSWR
is using a locally definedfetcher
, we are still not going to be left with a stale entry. Yes, if we remove an entry from cache, this is probably going to pop up the loading indicator once again the next time a given component is mounted, but isn’t stale (and possibly inconsistent) data display worse than a janky loading indicator?Currently calling
mutate(key)
when no mounted component is callinguseSWR(key)
leaves us with stale data as described in my other issue #751.The
mutate()
has the third paramshouldRevalidate
, But callingmutate('key', undefined, true)
doesn’t clean previous request result. I think it is the root cause of this issue.Although there is no mounted hook, we can simply delete the previous request result. It seems like that the
revalidate()
has happened for specific key.