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.

Data not fetched when missing from cache, when revalidateOn* config set to false

See original GitHub issue

There 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
sergiodxacommented, Jun 12, 2020

You can set revalidateOnMount to true and call mutate in an effect.

import React from "react";
import useSWR from "swr";

export const App = () => {
  const { data, error, isValidating, mutate } = useSWR(
    "/api/user",
    async () => {
      console.log("fetching user");
      return { name: "Amy " + Math.random() };
    },
    {
      revalidateOnFocus: false,
      revalidateOnMount: false,
      revalidateOnReconnect: false
    }
  );

  React.useEffect(() => {
    if (!data) mutate()
  }, [data, mutate]);

  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>;
};

That should trigger a fetch and update the cache, the function to be used to fetch is the fetcher you passed to useSWR.

1reaction
jamescrowleycommented, Jun 14, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

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