Is it even possible to persist paused mutations that are resumed and then fail?
My mutations are already persisted locally when offline. However, when back online if the mutation fails (also with retries) and I refresh the page, it is all lost. Is there a way to still keep the failed mutation and treat it as a paused one after a page refresh? I don't want to build something custom if react-query supports it and I'm unaware of it. Thanks.
12 Replies
flat-fuchsia•3y ago
are you using the PersistQueryClientProvider ?
flat-fuchsiaOP•3y ago
Yes I am. Im noticing that the mutations are persisted just fine, but when resumed, if they fail (say because the user session is expired), they’re removed from the storage and are lost
I could implement some custom code and persist the failed mutations separately, but I think there could be an option to tell react-query whether to remove or to keep mutations in the persisted cache.. there’s not even an api to manually add a mutation to the cache..
flat-fuchsia•3y ago
only paused mutations are persisted. if a mutation finishes and fails, it's not persisted
you can configure it to persist failed mutations, too
flat-fuchsiaOP•3y ago
Really?? What’s the option for that?
It’d be exactly what I need
flat-fuchsia•3y ago
it takes hydrate / dehydrate options, please look it up
but you can't "continue" failed mutations, so not sure what good that would do
flat-fuchsiaOP•3y ago
Mmm I see.. why isn’t there any API to manage the cache manually?
flat-fuchsia•3y ago
what do you mean?
flat-fuchsiaOP•3y ago
I'll just tell you my use case, so it's prob easier for you to understand:
My users will use the app mostly offline, so they will perform several mutations.
While they're offline, their refresh token might expire, which means they will have to login again when back online.
When this happens, all resumed mutations fail of course, as the token is expired, but I need to retry them all after a new token is granted, as I can't afford to lose any mutation.
One solution that kinda works is to set retries, but if the user for whatever reason decides to refresh the page during the retries the mutations are lost, so not really a solution. I could add some logic
onError and pretty much have some bespoke code that stores locally and retries the failed mutation but it'd be miles easier if I could just make sure the failed mutations are added back to the cache, persisted, and retried on page refresh.
I looked up dehydrate and the option shouldDehydrateMutation, but I'm not entirely sure where I should be dehydrating and later hydrating in my scenarioflat-fuchsia•3y ago
the dehydrate options just tell react query which mutations to write to the disk; all stored mutations will be restored
per default ,we filter to "only paused mutations".
if a mutation fails, it' s no longer paused. How do you handle this situation if the user is always online, but their jwt token expires?
flat-fuchsiaOP•3y ago
when online, the refresh token gets extended for as long as the user is using the app not to abruptly redirect them to the login page... so I don't have to handle that scenario
I need to handle the case when they visit the site again or go from offline to online and during that time the refresh token happened to expire
flat-fuchsia•3y ago
what you could do is just always grab a fresh refreshToken before you
resumePausedMutationsflat-fuchsiaOP•3y ago
omg of course! thanks!