Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@winkler1
Copy link
Contributor

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

@winkler1 winkler1 mentioned this pull request Nov 16, 2025
@joaomdsg joaomdsg merged commit 2bb5d80 into go-via:main Nov 16, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants