Web APIs (navigator, geolocation, history)
What's the primary purpose of the navigator.sendBeacon() method?
The navigator.sendBeacon() method enables reliable data sending during page unload: 1) It asynchronously transmits small amounts of data to a server, even as the page is being unloaded, 2) Unlike XMLHttpRequest or fetch during unload events, requests made with sendBeacon() will reliably complete, 3) This is particularly valuable for analytics data that tracks how long users spent on pages or when they left, 4) It uses POST requests with minimal overhead and doesn't wait for or process the server response, 5) The browser prioritizes user navigation while still ensuring the data gets sent when possible, 6) It accepts various data formats including strings, FormData, Blob, and ArrayBuffer, 7) This API solves the common problem of analytics data being lost when users navigate away from pages, 8) It's designed specifically for non-critical, best-effort data transmission that shouldn't delay navigation.