Swr: Prevent using cache on mount

Created on 13 May 2020  路  3Comments  路  Source: vercel/swr

Hi,

I have a page containing posts data (using SWR).
The user can click on "edit post" and it will redirect him to another edit page.
After editing success, the user is getting redirect back to the posts page.
Than, for a second, it sees the old data (before the edit), and after auto revalidation on mount, the new data arrives and the user can see the real data.
How can I prevent it from using the cache on mount in order not to show the user false data?
thanks

Most helpful comment

What you need to do is use mutate to update the cache before the page navigation

All 3 comments

What you need to do is use mutate to update the cache before the page navigation

@sergiodxa how do we use mutate? and how do we use it with "useSWRPages" ?

@rafinskipg you can import mutate from swr as

import { mutate } from 'swr'

Then you can call it as mutate(key, newDate), it also support a function as mutate(key, oldData => newDate), additionally you can pass a false as third value to avoid mutate to revalidate the data and only update it (by default mutate after updating the cached data it will trigger a new revalidation to ensure it's consistent with the backend)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DoT214 picture DoT214  路  4Comments

AjaxSolutions picture AjaxSolutions  路  5Comments

bcomnes picture bcomnes  路  3Comments

polc picture polc  路  3Comments

frdwhite24 picture frdwhite24  路  4Comments