minimal-analytics icon indicating copy to clipboard operation
minimal-analytics copied to clipboard

Check if `navigator.sendBeacon` is defined before calling it

Open PascalKoe opened this issue 11 months ago • 5 comments

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?

PascalKoe avatar Mar 03 '25 12:03 PascalKoe

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 👍

jahilldev avatar Mar 03 '25 13:03 jahilldev

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).

You can use the keepalive option for Request to get the same behavior for fetch in this respect.

alcore avatar Apr 03 '25 03:04 alcore

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).

You can use the keepalive option for Request to get the same behavior for fetch in this respect.

Yes, true, but mileage may vary between browser vendors!

jahilldev avatar Apr 03 '25 05:04 jahilldev

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).

alcore avatar Apr 03 '25 06:04 alcore

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).

Feel free to open a PR, all contributions welcome

jahilldev avatar Apr 03 '25 07:04 jahilldev