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

import.meta.url does not include fragments #5162

Closed
Constellation opened this issue Dec 17, 2019 · 7 comments
Closed

import.meta.url does not include fragments #5162

Constellation opened this issue Dec 17, 2019 · 7 comments

Comments

@Constellation
Copy link
Member

While upgrading WebKit module loader, @youennf noticed that we are using response's url when creating module-script[1].

Let module script be the result of creating a module script given source text, module map settings object, response's url, and options.

However, response's url does not include fragments[2,3]. This means, import.meta.url does not include fragments, and this behavior does not agree with what WPT is testing[4].

Shouldn't we include fragments in import.meta.url? Or should we do that by fixing the current HTML spec? I'm thinking that we should expose fragments because each module w/ URL+different fragments is distinct. But this means we cannot use response's url for import.meta.url in the HTML spec though.

/cc @domenic

1: https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-single-module-script
2: https://fetch.spec.whatwg.org/#ref-for-response-url
3: https://fetch.spec.whatwg.org/#response-url
4: https://github.com/web-platform-tests/wpt/blob/master/html/semantics/scripting-1/the-script-element/module/import-meta/import-meta-url.html#L54-L57

@wanderview
Copy link
Member

I think there might be a bug in the fetch spec at [3] above since @annevk landed a change to explicitly start allowing fragments:

whatwg/fetch@53de36a

@domenic
Copy link
Member

domenic commented Dec 17, 2019

Yeah, the intention is to include the fragment. There is even a test for it. https://wpt.fyi/results/html/semantics/scripting-1/the-script-element/module/import-meta/import-meta-url.html?label=master&label=experimental&aligned&q=script

This was previously discussed in #3622.

I will assign to @annevk to update [3] since it seems like the processing model is generally correct and includes the fragment, it's just that the text in [3] is not updated.

@Constellation
Copy link
Member Author

Thanks!

since it seems like the processing model is generally correct and includes the fragment, it's just that the text in [3] is not updated.

This makes sense :)

@annevk
Copy link
Member

annevk commented Jan 3, 2020

@wanderview that commit is part of an open PR at whatwg/fetch#696 and an open issue at whatwg/fetch#505. I'm not really sure why that did not move forward. It might have happened because I moved countries, nobody else cared, and I forgot about it. (There's various test PRs linked that do not have review.)

@annevk annevk removed their assignment Jan 3, 2020
@dcleao
Copy link

dcleao commented Mar 2, 2020

@Constellation, I wonder why you say:

I'm thinking that we should expose fragments because each module w/ URL+different fragments is distinct.

If the network layer ignores fragments to fetch module files, then how can modules with different fragments be distinct? Is this a way to enable instantiating a module many times and the fragment serves as arguments to the module?

@dcleao
Copy link

dcleao commented Mar 6, 2020

Ok, so light has come to my mind...

I checked and classical scripts can use console.log(new URL(document.currentScript.src).hash) to know the fragment which was used to instantiate the script. In the network tab, however, I only see one network access for the script, even if I write several <script> tags with different fragments (at least for when loading scripts in a web server; for local html and JS files, the script was fetched every time...).

Now, for module scripts, this capability is way more powerful, as these can export values. If no additional network fetches are really made, then... this is almost like AMD loader plugins...

// consumer.js
import bundle from "./i18n.js#path=a/b/c");
// i18n.js
const args = new URL(import.meta.url).hash;
const path = ...;
const locale = ...;
const bundle = await fetch(...);

export bundle;

I wonder if this was intended...

Now, what puzzles me is why the example in https://html.spec.whatwg.org/multipage/webappapis.html#module-map says:

the following code will create three separate entries in the module map, since it results in three different URLs:

import "https://example.com/module.mjs";
import "https://example.com/module.mjs#map-buster";
import "https://example.com/module.mjs?debug=true";

That is, URL queries and fragments can be varied to create distinct entries in the module map; they are not ignored. Thus, three separate fetches and three separate module evaluations will be performed.

Why 3 separate fetches and not 2? The first two imports are the same "web resource", no? Isn't this example incorrect, in the spec?

Now, if only import maps allowed bare names with fragments, this would really be useful:

// consumer.js
import bundle from "@my/i18n#path=a/b/c");

For reference:

cdumez added a commit to cdumez/web-platform-tests that referenced this issue Feb 4, 2022
Update workers/modules/shared-worker-import-meta.html to expect import.meta.url to include
the URL fragment. This matches the behavior of both WebKit and Blink.
See whatwg/html#5162 for more information.
@annevk
Copy link
Member

annevk commented Feb 7, 2022

The Fetch PR landed and fragments are generally preserved now.

@annevk annevk closed this as completed Feb 7, 2022
annevk pushed a commit to web-platform-tests/wpt that referenced this issue Feb 7, 2022
Update workers/modules/shared-worker-import-meta.html to expect import.meta.url to include
the URL fragment. This matches the behavior of both WebKit and Blink.
See whatwg/html#5162 for more information.
mattwoodrow pushed a commit to mattwoodrow/wpt that referenced this issue Feb 15, 2022
Update workers/modules/shared-worker-import-meta.html to expect import.meta.url to include
the URL fragment. This matches the behavior of both WebKit and Blink.
See whatwg/html#5162 for more information.
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this issue Feb 26, 2022
…ragment, a=testonly

Automatic update from web-platform-tests
import.meta.url should include the URL fragment

Update workers/modules/shared-worker-import-meta.html to expect import.meta.url to include
the URL fragment. This matches the behavior of both WebKit and Blink.
See whatwg/html#5162 for more information.
--

wpt-commits: 1c1e48b75ebf98fd3db4af8a330fcb6109942ce5
wpt-pr: 32704
DanielRyanSmith pushed a commit to DanielRyanSmith/wpt that referenced this issue Feb 28, 2022
Update workers/modules/shared-worker-import-meta.html to expect import.meta.url to include
the URL fragment. This matches the behavior of both WebKit and Blink.
See whatwg/html#5162 for more information.
jamienicol pushed a commit to jamienicol/gecko that referenced this issue Mar 1, 2022
…ragment, a=testonly

Automatic update from web-platform-tests
import.meta.url should include the URL fragment

Update workers/modules/shared-worker-import-meta.html to expect import.meta.url to include
the URL fragment. This matches the behavior of both WebKit and Blink.
See whatwg/html#5162 for more information.
--

wpt-commits: 1c1e48b75ebf98fd3db4af8a330fcb6109942ce5
wpt-pr: 32704
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this issue Mar 8, 2022
…ragment, a=testonly

Automatic update from web-platform-tests
import.meta.url should include the URL fragment

Update workers/modules/shared-worker-import-meta.html to expect import.meta.url to include
the URL fragment. This matches the behavior of both WebKit and Blink.
See whatwg/html#5162 for more information.
--

wpt-commits: 1c1e48b75ebf98fd3db4af8a330fcb6109942ce5
wpt-pr: 32704
jamienicol pushed a commit to jamienicol/gecko that referenced this issue Mar 8, 2022
…ragment, a=testonly

Automatic update from web-platform-tests
import.meta.url should include the URL fragment

Update workers/modules/shared-worker-import-meta.html to expect import.meta.url to include
the URL fragment. This matches the behavior of both WebKit and Blink.
See whatwg/html#5162 for more information.
--

wpt-commits: 1c1e48b75ebf98fd3db4af8a330fcb6109942ce5
wpt-pr: 32704
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