whatwg / html

HTML Standard
https://html.spec.whatwg.org/multipage/
Other
8.15k stars 2.67k forks source link

Browsers synchronously fire `pagehide` & `unload`, while the standard doesn't #10194

Open domfarolino opened 8 months ago

domfarolino commented 8 months ago

What is the issue with the HTML Standard?

This isn't as much an issue with the HTML Standard, as it is a compat discussion about whether we should move it forward in an undesirable way to match what all implementations do, vs get implementations to commit to match the standard.

Per HTML, the iframe removing steps should not synchronously invoke any script in the event loop that triggered the removal. This is not only clear in the algorithms called in the removal process, but made extra clear by the note just below this dfn: https://html.spec.whatwg.org/C#the-iframe-element:html-element-removing-steps

This happens without any unload events firing (the element's content document is destroyed, not unloaded).

Unfortunately, all browsers seem to synchronously fire the pagehide event during same-origin iframe removal, and depending on the status of their unload interventions, that event too: https://wpt.fyi/results/dom/nodes/insertion-removing-steps/insertion-removing-steps-iframe.window.html?label=experimental&label=master&aligned.

I mentioned this in https://github.com/whatwg/dom/issues/808#:~:text=But%20this%20unload%20issue%20seems%20like%20a%20scoped%20HTML%2Dnavigable%2Ddestruction%20bug%2C%20and%20less%20about%20the%20wider%20hooks%20in%20the%20overall%20DOM%20world%20that%20I%20would%20expect%20to%20block%20whatwg/html%235484.%20If%20%40domenic%20agrees%20I%27ll%20file%20something%20on%20HTML when exploring all of the opportunities that spec and implementations have to synchronously fire script in the node insertion & removal process.

Chrome is interested in experimenting (cc: @noamr @mfreed7) with the suppression of these events in at least the frame removal case, but since this carries possibly-significant compatibility risk, and since all browsers are currently aligned, we wanted to start some discussion with other vendors and editors before proceeding. I'll loop in @smaug---- and @rniwa (and @annevk since he's been involved in https://github.com/whatwg/dom/issues/808), but please feel free to loop others in with more context or opinions on this. It'd be unfortunate if we had to keep this instance of synchronous script execution around forever, but I'd love to get more thoughts on how difficult it would be to remove it.

annevk commented 8 months ago

In principle I think not firing events for the iframe element removal case is reasonable as the primary purpose for these events is navigation and history traversal.