Check if `navigator.sendBeacon` is defined before calling it
When browsing the DaisyUI Website that uses minimal-analytics, the navigation to links does not work. In the console of the browser an error is displayed linking to code of minimal-analytics: Uncaught (in promise) TypeError: navigator.sendBeacon is not a function. Source Location.
I guess my company modified the default Firefox installation to disable sending beacons using this API.
Is it possible to check if the API is available before calling it, so in case of corporate browsers like mine we do not run into exception but degrade gracefully?
Hey @PascalKoe,
Yes this should be a fairly simple change, we'd just need to fall back to making a fetch request instead. The only consideration being sendBeacon ensures to request is made, regardless of navigation away from the page that initiates it, whereas fetch does not. I believe this is a fairly reasonable compromise (it being the only one, really).
I'll try and get to this when I have some time, but feel free to open a PR, all contributions welcome 👍
The only consideration being
sendBeaconensures to request is made, regardless of navigation away from the page that initiates it, whereas fetch does not. I believe this is a fairly reasonable compromise (it being the only one, really).
You can use the keepalive option for Request to get the same behavior for fetch in this respect.
The only consideration being
sendBeaconensures to request is made, regardless of navigation away from the page that initiates it, whereas fetch does not. I believe this is a fairly reasonable compromise (it being the only one, really).You can use the
keepaliveoption forRequestto get the same behavior forfetchin this respect.
Yes, true, but mileage may vary between browser vendors!
Yes, true, but mileage may vary between browser vendors!
Not sure I follow. https://caniuse.com/?search=keepalive shows ~97% compat for a measure intended as a fallback to sendBeacon with its 99% compat. What particular other mileage is there to consider?
As for the reliability of properly triggering sends on unloads in the first place, a much more pertinent issue is the reliance on beforeunload for this, which is simply put, outright unreliable (see 1 and 2), as others have already pointed out. It also interferes with the bfcache in some browsers and goes against recommended practices (see MDN for both).
Yes, true, but mileage may vary between browser vendors!
Not sure I follow. https://caniuse.com/?search=keepalive shows ~97% compat for a measure intended as a fallback to sendBeacon with its 99% compat. What particular other mileage is there to consider?
As for the reliability of properly triggering sends on unloads in the first place, a much more pertinent issue is the reliance on
beforeunloadfor this, which is simply put, outright unreliable (see 1 and 2), as others have already pointed out. It also interferes with the bfcache in some browsers and goes against recommended practices (see MDN for both).
Feel free to open a PR, all contributions welcome