arthurfiorette / axios-cache-interceptor

📬 Small and efficient cache interceptor for axios. Etag, Cache-Control, TTL, HTTP headers and more!

Home Page:https://axios-cache-interceptor.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bug: staleIfError doesnt work, always ends with Caught an error in the request interceptor

fsobola opened this issue · comments

I would like to use staleIfError functionality, but I'm not able to achieve this feature. When I got response with status net::ERR_CONECTION_REFUSED, in debug mode I always see error: Caught an error in the request interceptor. So its rejected before stale cache settings is read and can be used. I'm using basic configuration (for testing purpose only):

setupCache(axios, { headerInterpreter: headers=>{ return {cache: 30*1*1000, stale: 60*60*1000} })

I'm not using any own interceptors. axios 1.4.0, axios-cache-interceptor 1.2.0.

Hey @fsobola would you mind doing a small reproducible example?

Here is very simple nodejs program, where the error is reproduced. Problem is probably in response, because staleIfError works well with https://jsonplaceholder.typicode.com/users for example. But I'm not able to find difference, I tried to remove or add some headers, but without any success.

const axios = require('axios');
const { setupCache } = require('axios-cache-interceptor/dev');

const test_url='http://test.satthosting.cz/backend/api/openapi/test';
//const test_url='https://jsonplaceholder.typicode.com/users';

setupCache(axios,
  {
    headerInterpreter: headers=>{
           return 1000;
    },
    debug: console.log
 }
);


axios.get(test_url, { id: 'test'});

setTimeout(()=>axios.get('https://invalid.url.com/users', { id: 'test', cache: { staleIfError: 5000}}), 2000);

Hey @fsobola, sorry for this delay, i was having some family problems... I'll test it asap.

Hey @fsobola, your problem was the following: You're making your headerInterpreter to only allow cache to 1 second. The second request you're making is after two seconds, after that time, the cache was already evicted and there's nothing to return and fulfill de requirements of staleIfError.

You have two solutions: You can increase the TTL on header interpreter, headerInterpreter: headers => 3000 or return a second allowed property called stale that specifically helps this use case headerInterpreter: headers => ({ cache: 1000, stale: 50000 }). The previous examples allows requests to be kept up to a second, but stale requests can use this value up to 50 seconds later.

You can learn more at https://axios-cache-interceptor.js.org/config#headerinterpreter

P.S: thanks <3