-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
Currently it seems like there are two separate meanings for "unload" in the spec:
- Changing the
Documentthat's currently loaded: Used by "unload a Document" and "unloading document visibility change steps " which called on navigations, history traversal, closing browsing context, and thebeforeunloadevent. - Destroying a
Document: used by theunloadevent, which is only fired if theDocumentis no longer salvageable (per Make beforeunload not affect 'salvageable' & fire unload event only if document is no longer salvageable #5889).
The first definition means that the Document does not have to be destroyed if it's unloaded, allowing cases where the back-forward cache keeps the Document alive after navigation so that the Document can be reused on back navigation, etc.
The unload event, however, does not fire every time we "unload" (using the first definition), and instead only fires whenever the Document turns out to be no longer salvageable, meaning it will get destroyed later. Even using the second definition, though, it's not always fired: notable examples include when we discard a browsing context (per #4611 this is not true in practice though, so this might be a separate issue), and when a Document gets destroyed while it's not the "active document" (e.g. when it's saved in bfcache but later destroyed due to memory pressure/timeout/other reasons)
So I guess the main problem here is just that the unload event has a misleading name, as it won't always fire whenever we're "unloading" as defined by the first definition (aka whenever the term "unload" is used in the spec). Also, we already have the pagehide event, which will always be fired whenever we're unloading and it covers all cases when the unload event is fired (+ more!). This means all unload handlers should be able to be changed into a pagehide handler.
Considering the above points, I wonder if it makes sense to deprecate the unload event (this does not mean the browsers will stop firing the event entirely immediately - ~66% of page loads still uses unload handlers). This will remove any confusion of the term "unload".
If that seems too drastic, I guess we can just make the different meanings very clear? (and maybe MDN, etc. can help clarify cases where the unload event will fire vs not).
cc @annevk @domenic @cdumez @fergald @altimin @smaug---- @mystor