What are Keepalive and Navigator.sendBeacon()? Zero-Loss Unload Telemetry
Surviving page teardown: why standard `fetch()` calls are aborted when a tab closes, how the 64 KiB out-of-process browser queue works, and why `visibilitychange` beats `unload`.
Direct Definition
Navigator.sendBeacon() and fetch(..., { keepalive: true }) are W3C and WHATWG transport primitives that transfer ownership of an outbound HTTP request from the document's renderer process to the browser's core network process, ensuring terminal telemetry packets finish transmitting even after the originating tab is closed or navigated away.
Why Standard `fetch()` Drops Terminal Session Packets
When a user clicks an external link, submits a form that triggers a full-page redirect, or swipes a mobile browser tab closed, the browser engine immediately begins tearing down the document's Renderer Process. Any in-flight asynchronous `fetch()` or `XMLHttpRequest` tied to that document context is abruptly cancelled (`net::ERR_ABORTED`) before the TCP/TLS socket can flush its send buffer.
Years ago, scripts tried to work around this by firing synchronous `XMLHttpRequest` (`xhr.open('POST', url, false)`) inside `unload` or `beforeunload` handlers - halting the UI thread until the server responded. Modern browsers deprecated synchronous XHR on page dismissal, and even worse, registering an `unload` listener disqualifies the document from the browser's Back/Forward Cache (`bfcache`), wrecking repeat navigation speed.
| Transport Mechanism | Survives Tab Teardown? | bfcache Compatible? | Response Body Readable? | Shared Quota Limit |
|---|---|---|---|---|
| Standard async fetch() | No (aborted on teardown) | Yes | Yes | Unbounded |
| Synchronous XHR in unload | Blocked in modern browsers | No (breaks bfcache) | Yes (blocks UI) | N/A |
| navigator.sendBeacon() | Yes (Network Process owned) | Yes (via visibilitychange) | No (fire-and-forget boolean) | 64 KiB (65,536 bytes) |
| fetch({ keepalive: true }) | Yes (Network Process owned) | Yes (via pagehide/hidden) | Yes (while page remains alive) | 64 KiB (65,536 bytes) |
The 64 KiB Quota Quirk & Fallback Dispatch Pattern
Both `navigator.sendBeacon()` and `fetch()` with `{ keepalive: true }` share a strict specification limit: the browser's background network queue only allows 64 KiB (65,536 bytes) of total pending keepalive payload across all active requests for an origin. If a script attempts to flush a bloated 90 KiB payload on exit, `sendBeacon()` returns `false` and `fetch({ keepalive: true })` rejects immediately with a `TypeError`.
Because our RAM Buffer Pipeline caps uncompressed kinetic slices well under 4 KiB and pairs them with CORS Preflight Bypass framing, terminal flushes never approach the 64 KiB ceiling. To handle mobile Safari (`iOS WebKit`) backgrounding - where users swipe up to switch apps without firing `pagehide` - our Zero DOM-Blocking Architecture binds to `visibilitychange` as the primary lifecycle trigger:
function dispatchTerminalSlice(endpointUrl, compactPayloadString) {
const blob = new Blob([compactPayloadString], {
type: 'text/plain;charset=UTF-8'
});
// 1. Attempt primary out-of-process beacon handoff
if (typeof navigator.sendBeacon === 'function') {
const queued = navigator.sendBeacon(endpointUrl, blob);
if (queued) return;
}
// 2. Fallback to WHATWG keepalive fetch if beacon queue returns false
fetch(endpointUrl, {
method: 'POST',
body: blob,
keepalive: true,
credentials: 'omit',
mode: 'cors'
}).catch(() => {
// Silent catch during renderer teardown
});
}
// Listen to visibilitychange (fires reliably on mobile app-switch)
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
const slice = ringBuffer.drainCompactJSON();
if (slice) dispatchTerminalSlice(INGEST_URL, slice);
}
}, { capture: true });
Using `visibilitychange` alongside `pagehide` preserves full `bfcache` eligibility on desktop and mobile browsers while guaranteeing that final exit-velocity vectors reach the edge even when a shopper abruptly locks their phone screen.
Related Glossary Concepts
Capture Every Terminal Session Vector
See how Nexolytix pairs sub-4 KiB ring buffer frames with out-of-process keepalive transport to eliminate 25–40% page-exit data loss.