-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
add helper function to clear deduping intervals #720
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! Unfortunately I don't think it's a good idea to expose such an internal and low-level API. May I ask what's the exact use case?
Thanks for the feedback. The use case is suppose I have a list of users fetched and render in a component using Now if I update the user in that component and come back to user list page, I will see the old data since new fetch won't happen before deduping interval timeout. I cannot use So in-order to trigger a new fetch, I needed a function to clear the |
@Arjun-sna Could you use the global |
@pacocoursey I tried using global |
After looking into the code, I found that once the component where |
@Arjun-sna this is not true, did you test? Since SWR shares the cache globally, even if it’s another component, or it’s unmounted, calling |
@shuding You are right. The cache is global. But the When I tried to find the cause of that, I found that the
Here
Please find the codesandbox list to demo that https://codesandbox.io/s/swr-demo-5q3l1 |
@Arjun-sna yes, if all components using that resource are unmounted, I think what you should do is this:
You can use mutate by using the same resource inside the current component, like a "revalidate handler". For example: function List () {
const { data } = useSWR('/api/user')
// render data...
}
function Button () {
const { mutate } = useSWR('/api/user') // here we don't use the data, just use mutate
return <button onClick={() => mutate()}>revalidate user</button>
} So even if |
@shuding Thanks for that workaround. The one issue with that approach though is it doesn't work for APIs with pagination related query params. Like if we want to invalidate a API with same url but different query params. |
Or will it be a better solution if |
This row executes an unnecessary revalidation on Button mounting with the default fetcher implementation. If we don't want to expose a clearDedupingInterval method, dedupingInterval should be cleared if mutate is called on an unmounted key. I opened a similar issue yesterday. |
If my understanding of this PR is correct it's totally needed. For example for SWR & NextJS combination the following code: // Logout Handling
cache.clear()
Router.push("/some-url") DOES NOT work for logout. Cached data is still there and affects rendering. This question was brought up previously btw. The only way to fix logout seems to be a fallback to low-level |
This is still an issue. Global mutate function currently doesn't mutate the values if there's a |
Having the same issue. Global mutate function currently doesn't mutate the values if there's a deduping Interval and global mutate is called in a seperate component. I guess the only solution is to force a page refresh, to force a page re-render, which is not ideal. |
@osseonews the solution is to switch to React-Query (sorry SWR). |
I love this library otherwise, but this is a super strange oversight with how the deduping interval is set. You basically can't log users out without forcing a page refresh. Am I missing something? I may actually have to consider |
@shuding Thanks for following up. If I'm logging a user out, can I clear the cache globally and get the same effect or do I have to call mutate on all known keys? If we have a Next.JS SPA, what's the recommended way to make sure no data leaks across users if someone logs out and logs in as someone else? |
The function added will help in clearing deduping interval before timeout set