mdn / content

The content behind MDN Web Docs
https://developer.mozilla.org
Other
9.14k stars 22.46k forks source link

Description of pathname property value when path has no segments is misleading #35574

Closed jamietre closed 1 month ago

jamietre commented 1 month ago

MDN URL

https://developer.mozilla.org/en-US/docs/Web/API/URL/pathname

What specific section or headline is this issue about?

First paragraph of page (under "URL: pathname property" header)

What information was incorrect, unhelpful, or incomplete?

If the URL has no path segments, the value of its pathname property will be the empty string." is incorrect, it can be either "" or "/". For the vast majority of end-user use cases involving schemes like http and https it will be "/", so this is especially confusing.

What did you expect to see?

If the URL has no path segments, the value of its pathname property will be the string "/" if the protocol scheme is special (ftp, file, http, https, ws, wss), or "" if the protocol scheme is non-special

Special scheme reference: https://url.spec.whatwg.org/#special-scheme

Reference: whatwg test data for URL: https://github.com/web-platform-tests/wpt/blob/master/url/resources/urltestdata.json

note that tests involving "special" schemes always return "/" for a pathname when there's no path, and "" only for "non-special" schemes

Full spec: https://url.spec.whatwg.org/

Do you have any supporting links, references, or citations?

Tested in both browsers and nodejs, instance of URL.pathname is never "" and always "/" when path is empty for special schems.

Do you have anything more you want to share?

in browser console or node repl, to demonstrate

new URL("http://foo.bar").pathname

MDN metadata

Page report details * Folder: `en-us/web/api/url/pathname` * MDN URL: https://developer.mozilla.org/en-US/docs/Web/API/URL/pathname * GitHub URL: https://github.com/mdn/content/blob/main/files/en-us/web/api/url/pathname/index.md * Last commit: https://github.com/mdn/content/commit/4de6f76bbfd76229db78ffb7d52cf6b4cb9f31f8 * Document last modified: 2024-03-06T06:23:15.000Z
Josh-Cena commented 1 month ago

Isn't this immediately clarified in the next paragraph?

URLs such as https and http URLs that have hierarchical schemes (which the URL standard calls "special schemes") always have at least one (invisible) path segment: the empty string. Thus the pathname value for such "special scheme" URLs can never be the empty string, but will instead always have a least one / character.

Thus, the condition "If the URL has no path segments" would never apply to HTTP(S) because HTTP URLs cannot have "no path segments", and it's only talking about other schemes. That still makes the first sentence correct.

jamietre commented 1 month ago

That's true. Yet, I read this on a small screen and apparently didn't get to the 2nd paragraph before being very confused by my observed behavior differing from what seems like an absolute statement in the first paragraph. So I guess not too much to see here :)

Since this did trip up at least one person, and most people have probably never heard of an invisible segments or special schema, it might be a little more clear to be less absolute in the first paragraph, and ensure people keep reading by saying something like "the value of pathname when a input URL has no path segments depends on the schema." or something to that effect.

I agree technically it's not wrong though.

Josh-Cena commented 1 month ago

I feel like there's something we could do, like converting the second paragraph to a note, but I'm not sure exactly how, or if we want to do anything at all.

hamishwillee commented 1 month ago

I've taken a shot at this in https://github.com/mdn/content/pull/35586

I think the problem here partially is that this is a lot of text, some of which is not actually all that relevant. What was there was not wrong, but I think this will reduce the chance that people don't read on to the relevant information.