question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Clear dedupingInterval of an unmounted key

See original GitHub issue

Bug 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:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
muqgcommented, Sep 26, 2021

@shuding Right, but then we can just fallback to a regular mutate(key) call in case that a global fetcher is not provided, yet still have this behavior by default when there is a configured global fetcher.

What’s more doing a mutate(key, fetcher(key)) opts out of some built-in features such as deduping, but that one in particular I think is irrelevant, since I can’t think of a single case where one would do mutate(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 called useSWR(key) I would expect the behavior logic to follow something like this: image

This I think illustrates the bigger issue with my expectations about what a mutate call should do when no mounted component is calling useSWR(key) at this very moment. Doing a mutate(key) should at the very least remove the entry from cache in order to allow the useSWR(key) call to fetch a fresh resource. In this way, even if useSWR is using a locally defined fetcher, 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 calling useSWR(key) leaves us with stale data as described in my other issue #751.

2reactions
MoonBallcommented, Apr 27, 2021

The mutate() has the third param shouldRevalidate, But calling mutate('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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found