Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

History length/index are not synchronously initialized in new navigables #9030

Open
domfarolino opened this issue Mar 15, 2023 · 5 comments
Open

Comments

@domfarolino
Copy link
Member

All browser synchronously initialize a new child navigable's window's history.length to whatever the parent's is, but the spec doesn't appear to do this anywhere in the https://html.spec.whatwg.org/multipage/document-sequences.html#create-a-new-child-navigable flow, so I think history.length would incorrectly always return 0 there. As @domenic suggested elsewhere, we should consider running https://html.spec.whatwg.org/multipage/browsing-the-web.html#update-document-for-history-step-application somewhere during new browsing context creation.

The trick is figuring out what to do for top-level ones. If you run the following code in DevTools:

const win = window.open();
console.log(win.history.length);

You'll get 0 in Chrome and Firefox, and 1 in Safari. Safari's behavior seems the most sane here, because if you just open a normal new tab in Chrome and Firefox you get 1, so 0 feels like an anomaly here. After all, history.length's documentation is:

Returns the number of overall session history entries for the current traversable navigable.

So I feel like we should never observe 0, in which case the idea of running https://html.spec.whatwg.org/multipage/browsing-the-web.html#update-document-for-history-step-application for all new BCs (after getting the proper values elsewhere) makes sense.

@domenic
Copy link
Member

domenic commented Mar 16, 2023

Note that we previously got some consensus on 1 instead of 0 in #6491, /cc @rakina. I think we got a good suite of web platform tests for that, too: web-platform-tests/wpt#28541. However re-reviewing them I don't see any assertions on history.length immediately after opening the window, so maybe we should add those.

One route here is to just change the history length to default to 1, which might require updating fewer call sites. (I.e. we'd only need to update in the iframe case, I think.)

@smaug----
Copy link

I think the behavior in Firefox comes from the fact that window.open(); (no arguments) doesn't load anything, it just creates the initial about:blank. window.open("about:blank"); behaves differently, because it loads a document. @hsivonen is making some changes to about:blank handling, but I can't recall now if the changes will affect window.open() too.

@petervanderbeken
Copy link

However re-reviewing them I don't see any assertions on history.length immediately after opening the window, so maybe we should add those.

I think the first test in html/browsers/browsing-the-web/navigating-across-documents/initial-empty-document/window-open-history-length.html does check this?
(We're not sure the other tests in that file are correct, looks like they might not wait for the loads to happen before checking history.length)

@hsivonen
Copy link
Member

hsivonen commented Jul 5, 2023

I think the first test in html/browsers/browsing-the-web/navigating-across-documents/initial-empty-document/window-open-history-length.html does check this?

Chrome reports 0 for these. Gecko with my patch from https://bugzilla.mozilla.org/show_bug.cgi?id=543435 reports 0 as well as a consequence of the high-level change without trying to target this test specifically. This makes me wish to change the test expectation to 0.

"Shippable"-configuration builds with today's iteration of my Gecko patch are available.

Is the mechanism by which Safari says 1 known?

@domfarolino
Copy link
Member Author

Gecko with my patch from https://bugzilla.mozilla.org/show_bug.cgi?id=543435 reports 0 as well as a consequence of the high-level change without trying to target this test specifically.

Hmm, are you saying that Gecko returning 0 here is new? I'm ask because at the time of filing this bug Gecko was returning 0, and we also have reports from #6491 from a while ago about both Chrome and Firefox returning 0, while Safari was returning 1. I'd be surprised if Gecko was returning 1 up until recently.

Given the rough consensus in #6491 from older discussion about this, and the fact that apparently web developers are apparently used to checking history.length against 1 instead of 0 (given this comment), I'm a little hesitant to change the test expectations. The initial about:blank Document is a document in history. The only thing special about it seems to be that it gets replaced more often, but I imagine it should contribute to session history length just like any other document.

Is the mechanism by which Safari says 1 known?

I imagine they're just literally counting the entries right, but I'm not sure. Maybe @cdumez could weigh in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

5 participants