0

I'm doing the folloing request over a json file stored on an azure blob.

const getDatas = () => {
    axios
      .get('https://randomname.blob.core.windows.net/public/data_home_page.json')
      .then(({ data }) => {
        setStats(data)}
        )
      .catch((er) => console.log('error'))
  }

I nested this function in a useEffect hook

useEffect(() => {
    getDatas()
  }, [])

It's working well as I can get the data and display it. My issue is that it's never refreshing at page/component render. The only solution is to clear cache.

How can I force the call to get fresh data withouth clearing all the user's cache ? And for a better understanding why does it go this way ?

Doge
  • 93
  • 3
  • 11
  • Does this answer your question? [Using JavaScript Axios/Fetch. Can you disable browser cache?](https://stackoverflow.com/questions/49263559/using-javascript-axios-fetch-can-you-disable-browser-cache) – Justinas Sep 01 '22 at 07:41
  • 1
    Try another API call and see if you have the same problem. For ex: https://jsonplaceholder.typicode.com/todos/1 – Cristian Muscalu Sep 01 '22 at 07:53

1 Answers1

1

If you add the correct headers to your Axios call this should disable the cache.

headers: {
    'Cache-Control': 'no-cache',
    'Pragma': 'no-cache',
    'Expires': '0',
}

Full Axios example

axios.get(`https://randomname.blob.core.windows.net/public/data_home_page.json`, {
    headers: {
        'Cache-Control': 'no-cache',
        'Pragma': 'no-cache',
        'Expires': '0',
    }
    })
    .then(({ data }) => {
    setStats(data);
    })
    .catch((er) => console.log("error"));
};

Example: https://codesandbox.io/s/flamboyant-resonance-dhq2fi?file=/src/App.js

Bonttimo
  • 621
  • 1
  • 9
  • 23