WICG / import-maps

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

Dynamic import-map tag insertion and prefetching #218

Closed moztcampbell closed 4 years ago

moztcampbell commented 4 years ago

Will the ability to insert import-map tags dynamically from out-of-line scripts harm a browser's ability to prefetch and compile module graphs?

Example:

<script src="maybe_dynamic_importmap.js"></script>
<script src="module_root.mjs" type="module"></script>

Without import-maps, a browser is able to look at this page and reasonably expect to begin to fetch the module-graph and things it imports transitively. With this proposal, that prefetching would either have to be avoided, or we would need to refetch and recompile the module graph. The proposal seems to blame the developer in this case which suggests the browser should reasonable pre-fetch and then re-fetch if maps are used.

Pages that do not use an import-map may be inclined to put an empty <script type="importmap"></script> tag to reassure the browser that URI rewriting will not happen.

Only allowing re-writes for non-URL import identifiers would avoid this all, but that was ruled out elsewhere in the proposal.

domenic commented 4 years ago

The answer is no. Once <script src="module_root.mjs" type="module"></script> executes, the import map is locked, and nothing can be dynamically added. So there is no delay waiting to see what maybe_dynamic_importmap.js does.

moztcampbell commented 4 years ago

One would still need to fetch/parse/execute the top-level of maybe_dynamic_importmap.js to determine that the import map is locked, wouldn't they?

I'm trying to consider the case of prefetching and parsing while the DOM is still being constructed.

domenic commented 4 years ago

Sure, but you can't execute <script src="module_root.mjs" type="module"></script> or fetch its dependencies before maybe_dynamic_importmap.js is executed anyway, per the HTML Standard.

moztcampbell commented 4 years ago

Got it. Thanks.

annevk commented 4 years ago

The HTML Standard also does not define <script> can be speculatively parsed and fetched yet everyone does it (in subtly different ways). We could try to forbid that practice for module scripts, but it seems such a decision would benefit from a wider discussion.