Why navigator.sendBeacon beats fetch for analytics on page exit
When a browser tab is closed, fetch requests fired inside a beforeunload handler are often cancelled mid-flight because the browser tears down the page context before the network round-trip completes. navigator.sendBeacon was purpose-built for this scenario, queuing the request and guaranteeing delivery even after the page unloads. Unlike fetch with keepalive, sendBeacon carries no payload-reliability caveats and works consistently across modern browsers. On mobile devices, beforeunload itself is unreliable, as iOS Safari may not fire it when users switch apps or lock their phones. Developers are advised to use the visibilitychange event combined with sendBeacon to reliably flush batched analytics whenever a page enters a hidden state.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in