Data not fetched when missing from cache, when revalidateOn* config set to false
See original GitHub issueThere are some requests that I’m happy to fill the cache, and then always return the cached value without revalidating. However, I can’t figure out the correct config to achieve this?
My understanding from #@sergiodxa’s comment here: https://github.com/vercel/swr/issues/450#issuecomment-642280493
was that setting the revalidate config properties to false would enable data to be fetched when it is not present in the cache, and then not fetched again. However, this doesn’t seem to be the case:
https://codesandbox.io/s/still-thunder-wq9df?file=/src/App.js
The following just always shows ‘no data’.
import React from "react";
import useSWR from "swr";
export const App = () => {
const { data, error, isValidating } = useSWR(
"/api/user",
async () => {
console.log("fetching user");
return { name: "Amy " + Math.random() };
},
{
revalidateOnFocus: false,
revalidateOnMount: false,
revalidateOnReconnect: false
}
);
if (error) return <div>failed to load {error}</div>;
if (isValidating) return <div>validating...</div>;
if (!data) return <div>no data...</div>;
return <div>hello {data.name}!</div>;
};
Am I missing something? Thanks!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Advanced topics on caching in Apollo Client
To reset the cache without refetching active queries, use client.clearStore() instead of client.resetStore() . Responding to cache resets. You can register ...
Read more >Stale-while-revalidate Data Fetching with React Hooks: A Guide
We do this by having a separate state for isLoading and setting it according to the state of the hook. That is, when...
Read more >Incremental Static Regeneration - Data Fetching - Next.js
If revalidate is omitted, Next.js will use the default value of false (no revalidation) and only revalidate the page on-demand when revalidate() is...
Read more >Data fetching in React and Next.js with useSWR to impress ...
We all can agree: there are many ways to fetch data in Next.js app. Because of that, people at the parties are not...
Read more >What's the difference between Cache-Control: max-age=0 and ...
...a cache MUST NOT use the response to satisfy a subsequent request without successful revalidation with the origin server. This allows an origin...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You can set
revalidateOnMount
totrue
and callmutate
in an effect.That should trigger a fetch and update the cache, the function to be used to fetch is the fetcher you passed to useSWR.
Thanks folks 😃
@shuding for our use case, we have data that we read where the records are static (think event log type data) - hence we want the caching benefit, but have no need to re-validate.
I’ll go with @sergiodxa 's suggestion for now, thanks.