Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Axios support 'keepalive' functionality same as the native fetch()?

Can Axios be configured to keep requests alive after a browser page is closed? Native fetch() accepts keepalive flag to enable this functionality, while I haven't found any similar in Axios documentation.

keepalive The keepalive option can be used to allow the request to outlive the page. Fetch with the keepalive flag is a replacement for the Navigator.sendBeacon() API.

https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters

like image 321
Arkadiy Andriyenko Avatar asked Jan 28 '26 15:01

Arkadiy Andriyenko


1 Answers

Yes you can do this with axios if you create your own axios instance.

const instance = axios.create({
  baseURL: 'https://some-domain.com/api/',
  timeout: 1000,
  headers: {'X-Custom-Header': 'foobar'},
  httpAgent: new http.Agent({ keepAlive: true }),
});

Axios docs can guide you a bit more.

like image 115
cpppatrick Avatar answered Jan 31 '26 04:01

cpppatrick



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!