question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Does Axios support 'keepalive' functionality as the native fetch() does (for browser unload)?

See original GitHub issue

Describe the issue

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. Asked previously but auto closed by the bot here: https://github.com/axios/axios/issues/4252

Example Code

window.onunload = function() {
  fetch('/analytics', {
    method: 'POST',
    body: "statistics",
    keepalive: true
  });
};

Expected behavior, if applicable

Closing the browser page does not keep the request from finishing (though the response cannot be handled, the request completes). Navigator.sendBeacon() is the old way of doing this. fetch’s keepalive property is the new way. Any support for either of these within axios? For example, to save your changes right before the browser closes so you do not lose work or send analytics.

Environment

  • Axios Version [0.24.0]
  • Browser [any]
  • OS: [any]

Additional context/Screenshots

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

More context here: https://javascript.info/fetch-api

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
ssidorchikcommented, Aug 23, 2022

👋 @jasonsaayman I see how AbortController can help to cancel requests. But in the above use case the opposite behavior is expected to let the request being completed on page close. Could you reopen this ticket?

4reactions
jasonsaaymancommented, Jun 26, 2022

Please check out the AbortController as this should handle this use case, should it not work please open a new issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Does Axios support 'keepalive' functionality same as the ...
Yes you can do this with axios if you create your own axios instance. const instance = axios.create({ baseURL: ...
Read more >
Improving API Performance with HTTP Keepalive
Axios is a promise-native library. Enablement of keepalive happens at the core https library and it controlled by the keepAlive flag. import { ......
Read more >
Axios vs. fetch(): Which is best for making HTTP requests?
Axios is not always an ideal solution ; depending on your needs, there are sometimes better options for making HTTP requests. The Fetch...
Read more >
Navigator.sendBeacon() - Web APIs | MDN
The navigator.sendBeacon() method asynchronously sends an HTTP POST request containing a small amount of data to a web server.
Read more >
Determining if an HTTP request was sent as beacon/keepalive
The keepalive option of the Fetch API, which allows a Fetch request to outlive the page. So, back to our integration test: our...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found