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.