Replace beforeunload dialog with sendBeacon for session cleanup #19
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The current implementation uses beforeunload with preventDefault() and returnValue, which:
Shows an annoying "Are you sure you want to leave?" dialog to users
Often cancels the AJAX request before it completes (browser kills async requests during unload)
Provides poor UX - users expect to navigate away immediately, not be questioned
Unregisters regardless of user response
Solution
Use navigator.sendBeacon() with beforeunload event (without preventDefault):
Why sendBeacon is better:
No user dialog - Navigation happens immediately without interruption
Guaranteed delivery - Browser queues the request and ensures it completes even after page unload
Built for this use case - Specifically designed for sending analytics/cleanup data during page transitions
Non-blocking - Doesn't delay page unload
More reliable - Survives page termination, unlike regular AJAX requests
Technical details:
Sends a POST request - server endpoint updated accordingly
Works reliably across modern browsers (Chrome, Firefox, Safari, Edge)
Fires only on actual navigation/close, not on tab switching