-
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
Reloading initialData
after mutation
#451
Comments
You can try deleting the cached key of the list of users after you update the user import { cache } from "swr"
cache.delete("/api/users") Then when you go back to the list, since there is no data currently cached it should use the initialData you provided. |
@sergiodxa Wow, worked like a charm. Thanks so much! Is this API documented somewhere? |
@nfantone it's not fully documented on readme yet, but you can take a look on the file https://github.com/vercel/swr/blob/master/src/cache.ts |
@huozhi Awesome. Thanks for sharing! In the same tenor, I'm still wondering why, if clearing the cache works, the other solutions didn't cut it for me. The more I learn about this, the more inclined I am to say this is really a bug. Shouldn't |
I completely agree with @nfantone I am struggling to get my head round how to update the cache and when to after mutating data. :) |
@nfantone RE: 2. in your list, the issue seems to be documented in #751 If you call |
I'm having some trouble understanding the proper pattern when trying to update/revalidate data from one view/page that's being mutated somewhere else. I'm using
swr
in a Next.js project.The scenario I'm facing is a fairly basic one:
/users
: A main page with a list of users. Fetches data using a/api/users
key./users/[username]
: A details page where you can edit attributes of a single user. Fetches data using a/api/users/[username]
key.If you edit a user, changes to its attributes should be reflected back on the list when going back - which it did, until I introduced SSR via
initialData
./users
page has agetServerSideProps
fetching all users on the list. Those are plugged intouseSWR
as{ initialData }
. This effectively avoids having to fetch users client side on first render, but makes it so it doesn't refresh the list after editing a user entry: it always returns the originalinitialData
value.Things I've tried:
Adding
revalidateOnMount: true
to theuseSWR
call. This solves the problem, but entirely defeats the purpose of passinginitialData
in as the exact same set of users is unnecessarily fetched twice: once SS fromgetServerSideProps
, once CS from the hook.Calling
mutate('/api/users')
from the edit page after successfully submitting the edit form. This would look like the natural solution to my issue. According to the docs, it should "broadcast a revalidation message globally to all SWRs with the same key" - but in my case, I found it doesn't seem to do anything at all.Same as above, but calling
mutate('/api/users', async users => ...)
. However, theusers
array from the callback is alwaysundefined
for me. I would have expected the last set of fetched users from the list to be passed. Does this work when navigating between SSR'd pages?Same as above, but explicitly passing a new list of users with
mutate('/api/users', fetch('/api/users'))
. This works fine, but suffers from the same double fetching quirk as 1. Not to mention the incursion of a clear violation of SoC patterns.So what's the expected way to go about this?
initialData
changes after a mutation and the server returns the correct value. However, it seems as though,swr
ignores new data after being set the first around and doesn't "reload" or "reinitialize" it. While this might be intentional (mimic'ing things likeuseState
), how do I refresh my stale data without fetching the same more than once?The text was updated successfully, but these errors were encountered: