Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force axios to not cache in GET requests

I use axios in my React-Native app

first , configure the headers

function configHeaders()
{
    // I tested all these below 3 lines , no on worked
    axios.defaults.headers.common["Pragma"] = "no-cache";
    axios.defaults.headers.common["Cache-Control"] = "no-cache";
    axios.defaults.headers.common["Cache-Control"] = "no-store";
}

the data returned from the below request is old data , not the current data in my database

exports.getInfo = async function ()
{
    configHeaders();
    return await cachios.get(URL + '/user/info').then(data => {
        return { success : true , data : data.data.data.user };
    }).catch(err => {
        return { success : false , message : err.response.data.message };
    });
}
like image 458
27mdmo7sn Avatar asked Jan 19 '26 04:01

27mdmo7sn


1 Answers

You can try to add cache buster in the URL - forcing each request to consider as new:

const cacheBuster = (url) => `${url}?cb=${Date.now()}`;
exports.getInfo = async function ()
{
    configHeaders();
    return await cachios.get(cacheBuster(URL + '/user/info')).then(data => {
        return { success : true , data : data.data.data.user };
    }).catch(err => {
        return { success : false , message : err.response.data.message };
    });
}
like image 173
Adidi Avatar answered Jan 21 '26 16:01

Adidi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!