WICG / import-maps

How to control the behavior of JavaScript imports
https://html.spec.whatwg.org/multipage/webappapis.html#import-maps
Other
2.65k stars 69 forks source link

Speculatively parsing with import map if there's a dynmic import before, #281

Closed allstarschh closed 2 years ago

allstarschh commented 2 years ago

Per https://github.com/WICG/import-maps#interaction-with-speculative-parsingfetching, I'd like to ask whether we should preload the import map or not.

If there's a dynamic import call before the import map tag,

According to the spec, the dynamic import call will set acquiring importmaps to false, so the following import map shouldn't be accepted, and if bar.mjs uses the specifier from the import map, that should also fail.

It looks fine if the parser doesn't preload the importmap script tag and module script tag, but if it does preloading, the fetching for bar.mjs seems wasted.

domenic commented 2 years ago

It's really up to the implementing browser? Like all speculative parsing, that's an optimization you can choose to perform, which may be wasted or may be useful.

Although in the case you list, I'm not sure what you mean by "preload the import map", since it is inline.

allstarschh commented 2 years ago

Although in the case you list, I'm not sure what you mean by "preload the import map", since it is inline.

I am trying to preload the inline import map script tag as well, as the speculative parser will try to preload the module script tag. If the parser didn't preload the import map tag, then the module script won't be able to resolve the specifier from the import map.

However, if the parser preloads the import map tag, but it won't know the dymanic import call from the javascript, and 'accepts' the import map tag instead.

I'd like to point out that with import map tag, preloading and non-preloading module scripts (for speculative parsing) could possibly have different bahaviors.

domenic commented 2 years ago

Yep, that's what that section is about! Like <base> or <script>, <script type=importmap> can cause preloading and non-preloading URL resolution to differ.

guybedford commented 2 years ago

It probably still makes sense to incorporate import maps when available into the preloading, assuming they will apply, unless there is an obvious statically detectable violation like a module script before an import map tag.