DBW: use navigator.sendBeacon in beforeunload/pagehide handlers
See original GitHub issueHow to check:
- look for
beforeunload
/pagehide
handlers (can reuse https://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-core/gather/gatherers/dobetterweb/document-event-listeners.js) - check for sync xhrs or
fetch
calls in the handler
because user agents typically ignore asynchronous XMLHttpRequests made in an unload handler.
To solve this problem, analytics and diagnostics code have historically made a synchronous XMLHttpRequest call in an unload or beforeunload event handler to submit the data. The synchronous XMLHttpRequest blocks the process of unloading the document, which in turn causes the next navigation appear to be slower. There is nothing the next page can do to avoid this perception of poor page load performance, and the result is that the user perceives that the new web page is slow to load, even though the true issue is with the previous page.
https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:11 (8 by maintainers)
Top Results From Across the Web
Navigator.sendBeacon() - Web APIs | MDN
The navigator.sendBeacon() method asynchronously sends an HTTP POST request containing a small amount of data to a web server.
Read more >Closing Chrome window not sending data with sendBeacon in ...
Currently I am using a sendBeacon within a unload event handler. FireFox: Refresh: Works; Back button: Works; Close window: Works. Chrome:.
Read more >Using the Beacon API - Request Metrics
unload handler and try to send an AJAX request, but this was slow and unreliable. Enter the Beacon API to give us a...
Read more >页面隐藏与卸载前, Pagehide vs visibilitychange, 页面生命周期 ...
了解更多HTTP 请求beforeunload:sendBeacon 与img.src HTTP 请求beforeunload:sendBeacon 与img.src,DBW:在beforeunload/pagehide 处理程序中使用navigator.
Read more >Leverage useBeacon And beforeunload In Google Analytics
Then, use the sendBeacon() API to send a User Timing hit to Google Analytics without having to worry about the unload process cutting...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
“uses-sendbeacon” sgtm.
For the network stuff, it looks like something clever like this could work. I didn’t realize you could dispatching
beforeunload
yourself works!You could wrap the observer and dispatching part in a function that gets injected into the page. Example. You might need to play around with delaying dispatching the events.
This would’t tell you that the XHR was sync (unless you also grep the console output for the warning chrome throws when sync xhrs are used), but I don’t think it would hurt to always suggest
navigator.sendBeacon
for all requests made inbeforeunload
andpagehide
handlers.