document-policy icon indicating copy to clipboard operation
document-policy copied to clipboard

Proposal: Disallow running unload handlers

Open rakina opened this issue 2 years ago • 10 comments

If a document specifies Document-Policy: unload=?0, the unload event will not be dispatched for that document.

This is useful because the existence of an unload handler in a page will make it be not eligible for BFCache on some browsers (e.g. Desktop Chrome and Desktop Firefox), impacting its performance. With this header, the page can guarantee that the unload handler won't be a problem for BFCache. (Note that a page will need to ensure its iframes won't run unload handlers too if it wants to be BFCacheable, which it can do through setting Require-Document-Policy: unload=?0)

Additionally the unload event is already very unreliable (it won't be run in various cases on mobile platforms, and also won't run if the page gets BFCached on Android Chrome, Android Firefox, and Safari (+ probably other WebKit-based browsers), and so it's pretty much a footgun and we considered deprecating it. The header allows web-pages to see what happens if unload never runs in their pages (to prepare for migration), have control over their code and even code they can't necessarily change (e.g. third party libraries), and also ensure that they'll never add any usage of unload in the future (in favor of the alternatives like pagehide/visibilitychange).

cc @domenic @annevk @smaug---- @cdumez @clelland

rakina avatar Sep 08 '21 14:09 rakina