Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsubscribe on beforeunload instead on unload has Chrome will stop firing unload events #285

Open
SuperPat45 opened this issue May 11, 2023 · 2 comments

Comments

@SuperPat45
Copy link
Contributor

SuperPat45 commented May 11, 2023

The unload event is used to unsubscribe the connection.

Chrome lighthouse audit say the unload event is a bad practice and prevent Back/forward cache

A browser optimization that enables instant back and forward navigation. It significantly improves the browsing experience for users—especially those with slower networks or devices.

Would it not be better to unsubscribe on beforenuload event instead?
beforenuload still prevent back/forward cache on Firefox, but not on Chrome.
I don't know if there would be any other implications.

@SuperPat45
Copy link
Contributor Author

SuperPat45 commented Aug 15, 2023

According to this article: https://developer.chrome.com/blog/deprecating-unload/

The unload event will be gradually deprecated starting from Chrome 117. The deprecation will occur by gradually changing the default so that unload handlers stop firing on pages unless a page explicitly opts in to re-enable them.

@SuperPat45 SuperPat45 changed the title Unsubscribe on beforeunload instead on unload fore Back/forward cache compatibility Unsubscribe on beforeunload instead on unload has Chrome will stop firing unload events Aug 19, 2023
@Legioth
Copy link

Legioth commented Aug 27, 2024

Note that beforenuload may in some cases be fired even if the user doesn't end up leaving the page.

One such case if when you click a link that leads to downloading a file (but the link doesn't have a download attribute). In that case, the browser will fire beforenuload when the link is clicked based on the assumption that the link will lead to a new page. Then when the browser receives the response and sees that it has a Content-Disposition: attachment header, it realizes that the page will not be unloaded after all and then it will just keep it running even though beforenuload has already been fired. (Or that's at least how it worked 5 years ago when I last tested it.)

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

No branches or pull requests

2 participants