System Design · Junior · Question 50 of 75
How do you send analytics when the page closes without losing the request?
- ✓Correct answer: With
navigator.sendBeaconorfetchwithkeepalive: true - 2With a regular
fetchin abeforeunloadhandler - 3With a
setTimeoutof 5 seconds after the tab is closed
Explanation
A regular request during page unload may be cancelled. sendBeacon queues a small POST in the browser and delivers it in the background. It's better to send on visibilitychange (when the page is hidden), since unload fires unreliably.