question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When revalidateOnMount = false, requests are never made

See original GitHub issue

I configured globally revalidateOnMount = false and no requests are made. Doing a request for the first time when there is no cache yet should not be considered re-validation since there is no “re…” in that action. At least a request should be made on first mount.

In my project I want my requests to only be sent when there is no cache, in other words: after the first call to useSwr() I want all the the following useSwr() to use the cache during all the life time of the application, or until I call mutate(). This is the correct behavior in applications when data is not required to be up to date. I can’t figure out a global configuration where I can set this behavior, is it possible?

This is my global configuration:

{
   refreshInterval: 0,
   errorRetryCount: 0,
   shouldRetryOnError: false,
   revalidateOnMount: false,
   revalidateOnFocus: false,
   revalidateOnReconnect: false,
   compare: (a, b) => a === b
}

I’m very surprised about having to search for this or open an issue because of this, maybe I’m doing something that nobody should do or something like that. Also I can’t figure out what is the use case for the current revalidateOnMount = false, who would ever need a useSwr() that does nothing?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:15
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
fabbcommented, Oct 28, 2021

What is the difference between revalidateOnMount and revalidateIfStale? I don’t really get it from the docs.

6reactions
shudingcommented, Apr 13, 2021

Yeah I’ve been thinking about this too, that’s why I opened #992 specifically for this case: don’t fetch if cached.

I will try to add it in 1.0 soon, it would be great to hear more community feedback for it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I fetch data only once when using swr? - Stack Overflow
{ revalidateOnFocus: false, revalidateOnMount:false, revalidateOnReconnect: ... Once the data is cached, they will never request it again.
Read more >
API - SWR
SWR is a React Hooks library for data fetching. SWR first returns the data from cache (stale), then sends the fetch request (revalidate),...
Read more >
NextJs data fetching tutorial using SWR and getStaticProps
This is a next.js tutorial on how to fetch data data for a statically generated page using swr data fetching hook.
Read more >
vswr - npm
Efficient HTTP requests are only done when needed. ... Keep in mind the component will still attempt to re-validate unless revalidateOnMount is set...
Read more >
[Solved]-Can I fetch data only once when using swr?-Reactjs
const useFoo = () => useSWR(key, fetcher, { revalidateOnFocus: false, revalidateOnMount: false, revalidateOnReconnect: false, refreshWhenOffline: false, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found