When a document goes away, the Fetch Standard terminates its in-flight fetches unless they are keepalive. A plain fetch() started on the way out can be cancelled: in Firefox 159, closing the tab lost it every time. Three APIs mark a request so it survives:
navigator.sendBeacon(url, data)queues aPOSTand returnstrueif it was queued,falseif the data is over quota. No response, no custom headers, always cookies. A string goes astext/plain;charset=UTF-8;URLSearchParams,FormDataand aBlobbring their own type. ABlobtypedapplication/jsonmakes a cross-origin beacon wait for a CORS preflight.fetch(url, { keepalive: true }): the same mechanism with the wholefetch()API (method, headers,credentials). Stream bodies aren't allowed.fetchLater(url, init)registers a request now; the browser sends it when the document is destroyed, or afteractivateAftermilliseconds. Abort it and register again to update the data. Chromium 135+ only; not in Firefox or Safari.
Keepalive requests from one page share 64 KiB of in-flight body. Past it, sendBeacon() returns false and a keepalive fetch() rejects with a TypeError. Firefox 159 enforced this for fetch() but not for beacons: a 16 MiB one went through.
When to send. Flush when visibilitychange reports visibilityState === 'hidden'. The Beacon spec calls it the only event guaranteed to fire on mobile when the user switches apps, after which the OS may kill the page without unload. Add pagehide for navigations. Avoid unload and beforeunload: they can be skipped, unload costs the back/forward cache wherever it runs, and Chrome 155+ doesn't run it by default. The page can come back after "hidden", so a flush must be repeatable: send what's new and clear it.
Better still, batch while the user is active, so the exit send carries only the tail.