🔄 Fix a Beacon Request Silently Dropped on Page Unload
navigator.sendBeacon() exists specifically to reliably deliver a small payload during page unload, when a normal fetch or XHR call risks being cancelled mid-flight as the browser tears the page down — but the browser still enforces a payload size limit on beacons (typically 64KB), and a beacon call that exceeds it doesn’t throw or warn, it simply returns false and the request is never sent at all, which is easy to miss since nothing in the console flags it.
🔎 The Problem
window.addEventListener('pagehide', () => {
const payload = JSON.stringify(collectFullSessionAnalytics());
// payload is ~180KB - over the browser's beacon size limit
const sent = navigator.sendBeacon('/analytics', payload);
console.log(sent); // false - but no error, no exception, nothing
});
// Analytics endpoint never receives this request, ever
✅ Fix: Check the Return Value and Keep Payloads Small
- Always check
sendBeacon()‘s boolean return value and have a fallback path (queue it for the next page load vialocalStorage, say) for when it returnsfalse, instead of assuming the call either worked or threw. - Trim the payload before unload rather than sending a full session dump — send the essential fields during
pagehideand let a regular, non-time-critical request deliver the bulk analytics data earlier, while the page is still fully alive. - Test specifically with a payload sized near and over the limit during development, since a beacon call built and tested with small sample data can look completely correct right up until real payloads cross the threshold in production.
A beacon that’s too big for the trip doesn’t fail loudly — it just doesn’t go, and sendBeacon’s own return value is the only place that gets recorded.





