-
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
Improving immediate mutate + later revalidation #157
Comments
I also find this very odd. Like you say the whole idea seems to be to mutate directly, then revalidate at a later time to make sure everything wen't like it should. If I have a slow API and use mutate like in the documentation samples, I still get a big delay in my UI unless doing it like @Daiz. Any reason the samples look like they do? |
This is a nice-to-have feature! I personally like the second API more (the 3rd argument can be a function or a promise): // wait until the Promise from patchData resolves before triggering revalidate
mutate(path, { ...data, patch }, patchData(patch))
// async function
mutate(path, { ...data, patch }, async () => { return patchedData })
// no revalidation
mutate(path, { ...data, patch }, patchData(patch), false) |
Looks great @shuding ! But I have to ask, what is the reason for you guys mentioning this in the Readme:
...and then only showing examples of first awaiting a post then mutating? I also wonder why would you even need mutate at all if not doing like suggested above? In a case where you still wait for a Thank you your awesome work! |
@johot yeah the example in the readme isn't good enough. It should first do a local mutation before sending the post request.
However the
If one simply call
|
@shuding The two suggestion aren't mutually exclusive, and would serve different purposes depending on how your server API works. First scenario:
|
@shuding Thank you for the explanation, now ofc it makes sense to me that you would still need to call mutate yourself since swr needs an extra roundtrip to the server which would cause further delays. @Daiz @shuding mutate(path, { ...data, patch }, patchData(patch)); and mutate(path, { ...data, patch }, patchData(patch), false); if you don't need to revalidate. Maybe skip the "First scenario" API @Daiz as it might be harder to understand what is going on and relying to much on how the backend works? |
@johot |
@shuding What's the progress on this subject? I'm very interested for this new API of |
Working on #1745 to support this case. 👍 |
- Prisma migration for `Integration`: adds `is_enabled` and makes `config` optional - Use Nextjs `useSWR`'s `mutate` to fetch and make updates - Get creating, updating, deleting Integrations working - Update openapi spec for creating/updating integrations SWR related reading for using mutating local and remote state: - vercel/swr#157 - vercel/swr#1745 - https://github.com/vercel/swr-site/pull/202/files Other: - Remove extra /dashboard page, use root instead for dashboard - Use `Card` for `MetricCard` rather than `Grid` - Rename `Active` tab to `Installed` - Rename `Application 1` to `Your application` - others
The documentation for
mutate
talks about local mutation for faster feedback, but the documented way to use it only mutates after a promise has resolved - in other words, not immediately.As a result, I've found myself writing code in this kind of pattern:
Technically, if
patchData
also returns the full new data, you could also do it like this:Which is actually also present in the documentation. But it is kinda janky to need two subsequent mutate calls for this...
One backwards-compatible way to deal with this could be to extend
mutate
to accept options as the third parameter, and introduceinitialData
much like present inuseSWR
. Then you could do something like this:In case the patchData function (or equivalent) doesn't return the full data, the third argument could also be augmented to accept a
Promise
, eg.I feel both these changes would make using mutate a lot more ergonomic overall.
In addition, I haven't checked the code to see how easy this would be to implemented, but another nice possibility would be to keep the local data (ie. non-promise second argument if third arg is a promise, anything in
initialData
) provided tomutate
in some sort of "unverified cache", such that in case the provided promises throw, SWR could automatically roll the data back to "last known good value" (ie. the last real API response for that path).The text was updated successfully, but these errors were encountered: