跪拜 Guibai
← All articles
Frontend · JavaScript · Developer

Chrome's unload Death: Page-Unload Reporting Methods That Survive

By kyriewen ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Analytics and tracking SDKs that still rely on unload or synchronous XHR are silently losing all page-close data on Chrome, with no error surfaced. Switching to visibilitychange plus sendBeacon is the only way to keep that data flowing; the shared 64KB keepalive budget also needs a hard cap to avoid dropped requests.

Summary

Chrome 155, released October 16, disables the unload event by default, and synchronous XHR has been banned during page unload since Chrome 80. On a local Chrome 158 instance, running each of five page-close reporting patterns three times, only three delivered: fetch with keepalive, navigator.sendBeacon, and fetch without keepalive (though the last is not guaranteed). Unload handlers never fire, and synchronous XHR returns 0/3; both old patterns silently drop analytics data.

Under a simulated weak network (400ms latency, 32KB/s upload), image GET (`new Image().src`) also fails completely, because the browser cancels the request at close time due to low priority. sendBeacon and fetch keepalive both share a 64KB budget per page unload: two 40KB keepalive requests in flight cause the second to throw a TypeError. The article also highlights duplicate reporting: both pagehide and visibilitychange fire on tab close, so attaching to both without a sentinel flag records every leave twice.

The recommended pattern is to listen only to visibilitychange and check visibilityState === 'hidden', then send via sendBeacon, falling back to fetch keepalive for payloads over 64KB or custom headers. A dedup flag prevents double sends. Developers should search their codebases for unload, synchronous XHR, and new Image() tracking URLs, and verify with a local server.

Takeaways
— Chrome 155 (October 16) disables unload handlers by default; in testing on Chrome 158, unload never fires during tab close or navigation.
— Synchronous XHR requests during page unload fail 0/3, even though they succeed when the page is alive.
— fetch without keepalive delivered 3/3 on loopback but is not guaranteed by spec; fetch + keepalive and sendBeacon were stable at 3/3.
— Under weak network (400ms latency, 32KB/s upload), image GET delivered 0/2 in all three rounds; sendBeacon, fetch, and fetch+keepalive all delivered 2/2.
— sendBeacon and fetch keepalive share a 64KB limit across in-flight requests; two 40KB keepalive requests cause the second to throw TypeError.
— sendBeacon over 64KB returns false silently; fetch keepalive over 64KB throws.
— Both pagehide and visibilitychange fire on a single tab close, so attaching to both without deduplication results in duplicate records.
— visibilitychange also fires on tab switching; reporting on hidden without dedup splits one session into multiple segments.
Conclusions

The browser's lifecycle model now punishes legacy assumptions: unload is gone, sync XHR is blocked, and image GET is deprioritized. Only sendBeacon and keepalive are designed for the modern lifecycle.

The shared 64KB budget is a hidden constraint: many SDKs send multiple payloads per unload, so they hit the second-request failure even when each payload is well under the limit.

Dual-event firing means any robust implementation must treat reporting as a state machine, not a one-shot call. The visibilitychange-only approach trades bfcache detection for simplicity, a reasonable engineering trade for most analytics.

Concepts & terms
sendBeacon
A browser API that queues a small HTTP POST request during page unload without blocking the page, with a 64KB body limit and a boolean return indicating whether the request was queued.
fetch keepalive
A flag on fetch that allows the request to continue after the page is gone, sharing the same 64KB in-flight budget as sendBeacon and throwing a TypeError when exceeded.
pagehide
An event fired when a page is being unloaded or moved to the back-forward cache; unlike unload, it still triggers in modern Chrome and can be used for reporting.
From the discussion
Featured comments
星云PLUS团队

Add two edge cases beyond actual testing. First, sendBeacon returning true only means the browser accepted the payload, not that it reached the server - 70KB returning false is explicit, but when the network layer drops it, it still returns true, so deduplication cannot rely solely on the client's reportOnce; it's better to include a client-generated event_id (stored in sessionStorage, so it still hits after bfcache restore), and the server builds a unique index on event_id as a backstop. Second, visibilitychange also triggers once on bfcache restore; if sent is not reset, the leave after returning from back/forward cache will never be reported; the cost of resetting is to use id for deduplication instead of a boolean flag, and these two points are related.

kyriewen

A flash of inspiration.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗