Description
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 for POST
) or fetch
with a keepalive
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 the keepalive
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 support keepalive
requests on beforeunload
, instead of having to do lots of code branching.