-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Emulate the keepalive option by doing a synchronous request #700
Comments
Hi, thank you for the thoughtful feature request. There are pros and cons to us implementing this, and I think you've done a good job outlining them. The technical aspect of this isn't hard (it would likely be only 2 lines of code), and it would certainly be useful is However, synchronous XHRs are just bad. There's a reason why browsers are deprecating/disallowing them now. They block the whole thread, freezing up the browser tab until they are done. What if the site that they are making the request to isn't responsive? We cannot in good consciousness support people using synchronous XHR. My advice: just use |
Thanks for taking the time to answer!
At the very minimum, I would suggest adding a notes under the "Caveats" section of the README with other potential solutions. Ideally, the library would throw when |
Using feature detection is better. It's possible that not all browsers that have initially implemented const browserSupportsKeepalive = 'keepalive' in new Request('')
// later:
if (browserSupportsKeepalive) {
fetch(url, {keepalive: true})
} else {
new XMLHttpRequest
}
We welcome PRs to improve documentation! I would suggest first adding the note about |
Historically, when sending data from browsers to server before the page is closed (e.g. through the
beforeunload
event), XHR synchronous requests have been used.This is now deprecated in Chrome, with likely more browsers to follow. The prescribed solution is either
navigator.sendBeacon
(which only works forPOST
) orfetch
with akeepalive
flag.It's not possible to properly polyfill the
keepalive
flag of course, but sending a synchronous XHR request is a good alternative for older browsers. Would you guys accept a PR that makes synchronous requests when thekeepalive
flag is set to true, with corresponding documentation in the README? This would make it so that people could use a single API call to supportkeepalive
requests onbeforeunload
, instead of having to do lots of code branching.The text was updated successfully, but these errors were encountered: