Skip to content

Imposing no cache for a SWR call #456

Answered by sergiodxa
at-webdev asked this question in General
Discussion options

You must be logged in to vote

It's not possible, the idea of SWR (as the name implies) is to first return you stale data and the revalidate it against the API to get the new data.

However, you can pass an array as key, if your fetch receives the URL as first argument and does nothing with the second argument you could do

const random = React.useRef(Date.now())
const { data } = useSWR([url, random], fetcher)

That will create a new random per mount (not per render) creating effectively a new key in the cache. But I don't recommend you to avoid the cache.

Replies: 8 comments 12 replies

Comment options

You must be logged in to vote
4 replies
@at-webdev
Comment options

@nandorojo
Comment options

@nikita-starostin
Comment options

@Mohamed3on
Comment options

Answer selected by at-webdev
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
5 replies
@shuding
Comment options

@nandorojo
Comment options

@ephys
Comment options

@nandorojo
Comment options

@ephys
Comment options

Comment options

You must be logged in to vote
2 replies
@shuding
Comment options

@yoshiko-pg
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@benevbright
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet