-
Notifications
You must be signed in to change notification settings - Fork 23
Why does visibilitychange fire after pagehide in the unload flow? #39
Comments
I don't recall any particular reason for why this ordering was chosen. My guess is that it simply wasn't a constraint or criteria when we specced this. If we wanted to update this logic, you'd have to update the HTML spec and swap order of steps 5 and 6.
|
My guess is this wouldn't break many (or any) sites or analytics libraries based on the fact that, until recently, not all browsers actually fired My sense is most code has to determine page unload status by listening to a number of different events ( |
Fair enough. I don't have any objections to making this change. That said, this is an update to the HTML spec, so I'll defer to the editors there. |
@domenic could we send a patch to update the ordering in the HTML spec? or do we need a new bug on html repo first? |
Sending a patch to HTML sounds great! It'll need publicly-expressed multi-implementer interest to land; you can gather that on the PR or in this issue. (Also it will need web platform tests.) |
@smaug--- @toddreifsteck - could you comment for implementer interest. |
(Oops, I had the wrong git handle for Olli) |
I think it should be fine, and looking at the Gecko implementation, the change should be easy https://searchfox.org/mozilla-central/rev/00dd116638001772fe354b081353b73f1cad405d/dom/base/nsDocument.cpp#8548,8561 But that code made me look at fullscreen spec, and perhaps we'll need to update that too "Whenever the unloading document cleanup steps run with a document, fully exit fullscreen document." That happens quite late during unload. Basically someone needs to take a look at the whole unload algorithm https://html.spec.whatwg.org/multipage/browsing-the-web.html#unload-a-document and figure out what all needs to be changed and whether changes are actually feasible. |
Would it make sense to make the code change in Chrome (before spec change) and see if anything breaks, although I'm fine with proceeding with spec change based on comments in this thread. |
@philipwalton Per WG call, Could you work with folks to move this issue to HTML5 spec as it tracks the ordering of events. |
Filed whatwg/html#3957. |
Part of #3957. See also discussion in w3c/page-visibility#39. Note that we're already running visibility change steps before firing pageshow, so after this change we have up-to-date visibility when either pagehide or pageshow is fired.
Part of whatwg#3957. See also discussion in w3c/page-visibility#39. Note that we're already running visibility change steps before firing pageshow, so after this change we have up-to-date visibility when either pagehide or pageshow is fired.
Part of whatwg#3957. See also discussion in w3c/page-visibility#39. Note that we're already running visibility change steps before firing pageshow, so after this change we have up-to-date visibility when either pagehide or pageshow is fired.
Part of whatwg#3957. See also discussion in w3c/page-visibility#39. Note that we're already running visibility change steps before firing pageshow, so after this change we have up-to-date visibility when either pagehide or pageshow is fired.
But in the more-than-two-years since this issue was first raised, I think Gecko and Blink have for quite a while now been shipping with support for Given that, I wonder what’s the current level of confidence among the Chrome team and Firefox team that this change isn’t going to break/regress existing sites and libraries?
OK, yes, it does seem likely that sites would be doing that if they needed to make things work across current browsers.
…but I think from the assumption that existing sites are listening to a number of different events, it doesn’t necessarily follow that changing the firing order of But regardless, it’s all just speculation unless there’s some plan to try to evaluate whether in fact the change breaks any existing sites. Given the nature of the change, it seems like there’s still a non-insignificant risk it could actually break some. |
Looking at https://www.chromestatus.com/metrics/feature/timeline/popularity/196, it looks like the usage number hasn't changed much between 2018-2020. Other than that, it's already possible for visibilityState to be hidden and visibilitychange to be fired before pagehide, if a tab is already backgrounded, so sites should already handle this case. And as you mentioned, Safari only very recently added visibilitychange support on navigations. Anyways, it's probably a good idea to measure the impact by how often visibilityState is accessed within a pagehide handler. Do you have other suggestions on what might help here? |
ah OK yeah, that’s good data to know about
That makes sense, yeah
Right. And it’s not actually shipped yet — not even in a Technology Preview release.
I don’t have any other specific suggestions — but if others with more insight than me have ideas that would require somebody spending time — whether it’s manual searching of some kind, or writing some tooling to help with — I’d certainly be willing to put time into it needed to do those kind of things. |
This change reverts 386360c (#3957). Specifically, it reorders two steps in the “unload a document” algorithm such that the spec once again requires UAs to wait to run any “unloading document visibility change steps” from other applicable specifications (in particular, the Page Visibility spec) until *after* firing the pagehide event. In practice, in combination with requirements in the Page Visibility spec, this change has the effect of stating that UAs must wait to fire the visibilitychange event until after the pagehide event has fired. That ordering matches the behavior current implemented in all engines. Otherwise, without this change, the ordering of firing for the events doesn’t match what implementations actually do. See also w3c/page-visibility#39 See also #5949 (comment)
This change reverts 386360c (#3957). Specifically, it reorders two steps in the “unload a document” algorithm such that the spec once again requires UAs to wait to run any “unloading document visibility change steps” from other applicable specifications (in particular, the Page Visibility spec) until *after* firing the pagehide event. In practice, in combination with requirements in the Page Visibility spec, this change has the effect of stating that UAs must wait to fire the visibilitychange event until after the pagehide event has fired. That ordering matches the behavior current implemented in all engines. Otherwise, without this change, the ordering of firing for the events doesn’t match what implementations actually do. See also: * w3c/page-visibility#39 * #5949 (comment)
This change reverts 386360c (whatwg#3957). Specifically, it reorders two steps in the “unload a document” algorithm such that the spec once again requires UAs to wait to run any “unloading document visibility change steps” from other applicable specifications (in particular, the Page Visibility spec) until *after* firing the pagehide event. In practice, in combination with requirements in the Page Visibility spec, this change has the effect of stating that UAs must wait to fire the visibilitychange event until after the pagehide event has fired. That ordering matches the behavior current implemented in all engines. Otherwise, without this change, the ordering of firing for the events doesn’t match what implementations actually do. See also: * w3c/page-visibility#39 * whatwg#5949 (comment)
The current ordering of
pagehide
andvisibilitychange
during a page unload complicates our proposed flow of Lifecycle states for the Lifecycle API.The reason is the ordering of
pagehide
andvisibilitychange
events can be inverted, depending on what the user does. Consider these two examples:The user loads a page in Tab A and then switches to a new tab B. Later, they close tab A without refocusing it. In this case the ordering of events fired on tab A is
visibilitychange
→pagehide
.The user loads a page in Tab A and then navigates to a new page from a link. In this case the ordering of events fired on tab A is
pagehide
→visibilitychange
.Wouldn't it make more sense if
visibilitychange
always fired beforepagehide
?Since pagehide is associated with the FROZEN state (for bfcache supporting browsers), and visiblitychange is associated with the HIDDEN state, the current ordering of events suggests that the lifecycle state of a page can transition from ACTIVE to FROZEN to HIDDEN, which doesn't make sense since we only want to freeze hidden tabs.
Does anyone here remember why this ordering was chosen? Would anyone object to changing the ordering in the spec?
https://html.spec.whatwg.org/multipage/browsing-the-web.html#unloading-documents
(Related dicussion WICG/page-lifecycle#7).
The text was updated successfully, but these errors were encountered: