Does Axios support 'keepalive' functionality as the native fetch() does (for browser unload)?
See original GitHub issueDescribe 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:
- Created 2 years ago
- Reactions:4
- Comments:7 (1 by maintainers)
👋 @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?Please check out the
AbortController
as this should handle this use case, should it not work please open a new issue.