Categories
JavaScript Answers

How to disable browser cache with JavaScript Axios?

Spread the love

Sometimes, we want to disable browser cache with JavaScript Axios.

In this article, we’ll look at how to disable browser cache with JavaScript Axios.

How to disable browser cache with JavaScript Axios?

To disable browser cache with JavaScript Axios, we can set the Cache-control and Pragma request headers to no-cache.

And we set the Expires request header to 0.

For instance, we write:

axios.defaults.headers = {
  'Cache-Control': 'no-cache',
  'Pragma': 'no-cache',
  'Expires': '0',
};

(async () => {
  const {
    data
  } = await axios.get('https://catfact.ninja/fact')
  console.log(data)
})()

We set axios.defaults.headers to:

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

to disable caching the response.

Then we call axios.get to make the GET request to the URL we want.

Conclusion

To disable browser cache with JavaScript Axios, we can set the Cache-control and Pragma request headers to no-cache.

And we set the Expires request header to 0.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.