You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
popstate/hashchange dispatching doesn't match what browsers do. #1792
interopImplementations are not interoperable with each othertopic: eventsEvent handlers, their content and IDL attributes, and the events HTML firestopic: historySession history, the History interface, pushState(), popstate and scroll restoration
When doing fragment navigation Gecko and Blink dispatch popstate sync but hashchange async. Per current spec both events should fire async using same task.
Could you loop some people from webkit / chrome / edge to see if they have some comments? I think if major browsers are all behave the same, we should update the spec accordingly.
The proposal is to align popstate/hashchange with Chrome and Safari and fire the first directly and the second from a task. Anyone any comments on that?
Fun fact: Chrome does not synchronously fire popstate for fragment navigations. It waits until "document close", i.e. load event fire time. You can see this by opening your console on https://boom-bath.glitch.me/popstate-location-href.html and observing different results in Firefox vs. Chrome.
Chrome is trying to introduce a HTML parser that yields to the event loop more often before the load event, and doing so causes the ordering of these events to flip:
Without yielding (current Chrome), we queue hashchange, fire to the load event, fire popstate, then yield and the queued hashchange fires. So: load, popstate, hashchange.
With yielding, we queue hashchange, yield, fire hashchange, fire the load event, fire popstate. So: hashchange, load, popstate.
I'm not sure what the right solution is here, but I don't like how this sort of stuff makes the yield points observable. I suspect the blame lies with queuing hashchange since that makes it run at the next yield point. Maybe we can hold both hashchange and popstate until the load event, and either queue or synchronously fire hashchange then.
I have written some tests at web-platform-tests/wpt#32392. I have not yet found a Safari computer to run them against.
The problem cases really seem to be about changing location.hash. Recall that pushState/replaceState don't fire popstate. Traversal seems to work fine since generally everything is async; the popstate-then-hashchange order seems to be preserved with no difficulties.
Notably Chrome's behavior differs between Canary and stable. And per the above it might depend on parser yield points and thus not be deterministic. I think this gives us more freedom to match Firefox-like behavior.
However I think Firefox might still do task-queuing, which I worry about because it could cause the non-determinism Chrome is currently exhibiting.
So this might be one proposal:
Fire popstate sync
This is like Firefox, AFAICT
Chrome usually fires it sync but will delay it until after the load event if the load event hasn't fired yet. In that case if there are multiple state changes only one will generate an event in Chrome. (Seems bad.)
Before the load event fires, if the hash changes: queue up hashchange events and fire them right before load
This requires a separate queue of hash changes as you can change the hash multiple times before the load event fires
Both Chrome and Firefox appear to fire hashchange before the load event (but not synchronously)
I will investigate Safari behavior next week and see if this plan might work out.
A less-ambituous plan is to avoid the queue of hash changes and allow that level of parser-based nondeterminism. That would still preserve the popstate-then-hashchange order, as well as the every-change-generates-both-events invariant. It would mean hashchange could be either before or after the load event though.
I do worry that firing popstate sync (even if the change happens before load) might be a compat risk for Chrome. So if @smaug---- can confirm that Firefox in all fragment navigation cases fires popstate sync, and that this hasn't been a source of compat bugs, that would help alleviate my concerns.
What causes Chrome fires popstate after load? Does it happen always or only if .hash was modified just before load?
I don't understand what "separate queue". One just queues a task to fire hashchange.
And yes, in FF, location.hash is synchronous, so popstate is sync.
The only relevant bug report about this seems to be for discussing whether to follow the model spec (at least at some point) had to fire popstate async.
@natechapin and I are planning to try aligning Blink and the spec with Gecko here. Since you have not experienced any compat issues we are encouraged that we try that without compat problems.
@natechapin's analysis of the compat risks for Blink changing this are that it reduces to the following scenarios:
If you pushState() before the load event, and you somehow depend on history.state not being equal to e.state inside the popstate event handler.
The long tail of absurd ordering dependencies that a website might take on between popstate/hashchange/load.
Both of these are not too risky because (a) Firefox has gotten away with it for many years without problems; (b) ordering is already unreliable and has even changed recently in Chromium.
I will work on a spec PR now where we can see in more detail. And @natechapin and I will collaborate on web platform tests.
Now for a history lesson!! Why is Chromium delaying popstate until after load? Well, apparently the spec used to ask us to do so! See this old working draft.
interopImplementations are not interoperable with each othertopic: eventsEvent handlers, their content and IDL attributes, and the events HTML firestopic: historySession history, the History interface, pushState(), popstate and scroll restoration
Activity
freesamael commentedon Sep 7, 2017
@annevk I'm willing to push this forward. I found that Webkit raises compatibility concerns and decided not to implement popstate async:
https://bugs.webkit.org/show_bug.cgi?id=153686#c2
Chrome seems to keep the same behavior as Webkit at this part - sync popstate & async hashchange:
https://cs.chromium.org/chromium/src/third_party/WebKit/Source/core/frame/LocalDOMWindow.cpp?type=cs&sq=package:chromium&l=405-415
Could you loop some people from webkit / chrome / edge to see if they have some comments? I think if major browsers are all behave the same, we should update the spec accordingly.
annevk commentedon Sep 7, 2017
The proposal is to align popstate/hashchange with Chrome and Safari and fire the first directly and the second from a task. Anyone any comments on that?
cc @cdumez @majido @foolip @hober @travisleithead
foolip commentedon Sep 7, 2017
@tkent-google, can you figure out who the Blink contact for this would be?
tkent-google commentedon Sep 11, 2017
I don't think so. Maybe japhet@chromium or kinuko@chromium knows it.
foolip commentedon Sep 19, 2017
@kinu, do you know? (I can't find japhet on GitHub.)
kinu commentedon Sep 20, 2017
@natechapin you probably know more about this?
domenic commentedon Nov 19, 2021
Fun fact: Chrome does not synchronously fire popstate for fragment navigations. It waits until "document close", i.e.
loadevent fire time. You can see this by opening your console on https://boom-bath.glitch.me/popstate-location-href.html and observing different results in Firefox vs. Chrome.Combined with the fact that we queue hashchange events (per the original post in this issue), this bit us recently in https://bugs.chromium.org/p/chromium/issues/detail?id=1254926. The basic situation is
Chrome is trying to introduce a HTML parser that yields to the event loop more often before the
loadevent, and doing so causes the ordering of these events to flip:I'm not sure what the right solution is here, but I don't like how this sort of stuff makes the yield points observable. I suspect the blame lies with queuing hashchange since that makes it run at the next yield point. Maybe we can hold both hashchange and popstate until the load event, and either queue or synchronously fire hashchange then.
domenic commentedon Jan 14, 2022
I have written some tests at web-platform-tests/wpt#32392. I have not yet found a Safari computer to run them against.
The problem cases really seem to be about changing
location.hash. Recall that pushState/replaceState don't firepopstate. Traversal seems to work fine since generally everything is async; the popstate-then-hashchange order seems to be preserved with no difficulties.Notably Chrome's behavior differs between Canary and stable. And per the above it might depend on parser yield points and thus not be deterministic. I think this gives us more freedom to match Firefox-like behavior.
However I think Firefox might still do task-queuing, which I worry about because it could cause the non-determinism Chrome is currently exhibiting.
So this might be one proposal:
I will investigate Safari behavior next week and see if this plan might work out.
A less-ambituous plan is to avoid the queue of hash changes and allow that level of parser-based nondeterminism. That would still preserve the popstate-then-hashchange order, as well as the every-change-generates-both-events invariant. It would mean hashchange could be either before or after the load event though.
I do worry that firing popstate sync (even if the change happens before load) might be a compat risk for Chrome. So if @smaug---- can confirm that Firefox in all fragment navigation cases fires popstate sync, and that this hasn't been a source of compat bugs, that would help alleviate my concerns.
smaug---- commentedon Feb 3, 2022
Firefox queues hashchange, but not popstate, as mentioned initially
https://searchfox.org/mozilla-central/rev/8b46752d1e583b2f817c451f93ba515fb865554d/docshell/base/nsDocShell.cpp#9154,9164
What causes Chrome fires popstate after load? Does it happen always or only if .hash was modified just before load?
I don't understand what "separate queue". One just queues a task to fire hashchange.
And yes, in FF, location.hash is synchronous, so popstate is sync.
The only relevant bug report about this seems to be for discussing whether to follow the model spec (at least at some point) had to fire popstate async.
domenic commentedon Apr 12, 2022
@natechapin and I are planning to try aligning Blink and the spec with Gecko here. Since you have not experienced any compat issues we are encouraged that we try that without compat problems.
@natechapin's analysis of the compat risks for Blink changing this are that it reduces to the following scenarios:
pushState()before theloadevent, and you somehow depend onhistory.statenot being equal toe.stateinside thepopstateevent handler.Both of these are not too risky because (a) Firefox has gotten away with it for many years without problems; (b) ordering is already unreliable and has even changed recently in Chromium.
I will work on a spec PR now where we can see in more detail. And @natechapin and I will collaborate on web platform tests.
Now for a history lesson!! Why is Chromium delaying popstate until after load? Well, apparently the spec used to ask us to do so! See this old working draft.
This changed in 4e57ced#diff-42692259bbea922a55e8e0f8a8b7860df9b59e29b3f955bcfd4d0a3ca852cc01L60686 , as a result of discussions in https://www.w3.org/Bugs/Public/show_bug.cgi?id=11468 and https://www.w3.org/Bugs/Public/show_bug.cgi?id=12277 . The change in turn is based on something Firefox very intentionally led the way on, and even blogged about: https://hacks.mozilla.org/2011/03/history-api-changes-in-firefox-4/ . As far as we can tell, other browsers implemented items 1 and 2 in that blog post, but not 3. And the spec got updated in a weird way that didn't really match any browser.
4 remaining items
Make popstate always fire synchronously
Always fire popstate and restore persisted state sync
Make popstate always fire synchronously
Make popstate always fire synchronously
Make popstate always fire synchronously
Bug 1765893 [wpt PR 33746] - Make popstate always fire synchronously,…
Bug 1765893 [wpt PR 33746] - Make popstate always fire synchronously,…
Always fire popstate and restore persisted state sync
PopState event should be fired synchronously, even before the load event
Merge 254519@main - PopState event should be fired synchronously, eve…
Bug 1765893 [wpt PR 33746] - Make popstate always fire synchronously,…