Closed domenic closed 8 years ago
Some use-cases for this include:
My first take at the lazy-loading scenario:
<nav>
<a href="page1.html" data-bundle="page-one">Page One</a>
<a href="page2.html" data-bundle="page-two">Page Two</a>
<a href="page3.html" data-bundle="page-three">Page Three</a>
</nav>
document.querySelector('nav').addEventListener('click', e => {
e.preventDefault();
await import(`./bundles/${e.target.dataset.bundle}.js`);
});
this example is very similar to the fetch-text example on https://github.com/mdn/fetch-examples/. What's the advantage of using import()
for this sort of thing?
Could import()
be used to help integrate JS modules/web components? Something like:
import('https://elements.cdn/specialElementClass.js')
.then(... customElements.define("special-element", module); ...
Well, in this example we're fetching a JS module with exports, not just text.
Could import() be used to help integrate JS modules/web components? Something like:
It could, but this doesn't really demonstrate any advantages of doing this dynamically, instead of just using <script type="module">
yes, but why have a module just to load an HTML page/fragment? I suppose it depends on what loadPageInto
in the Readme example actually does. If it's something complicated rather than a simple copy/clone of HTML, then I suppose the module is more like a component which defines behaviour as well as content - which brings me nicely back to my other question. :-)
Examples of dynamic loading and run-time conditions:
General principle: only load what the user needs when they need it.
if queryparam (in browser) or cli option (in Node) import('a') else import('b')
This isn't really an example though. What use-case are you thinking of?
if browser doesn't support x, import('polyfillforx')
Yeah, polyfills are a good one.
Polyfills are only reasonable if you plan to not use the feature until the polyfill is done loading, which I haven't seen too much of...
Typically polyfills need to be synchronous, and to block other code, for the exact reason @domenic mentions.
if browser doesn't support x, import('polyfillforx').then(import('codewhichusesx') ?
Yes. I've never seen code like that in the wild using existing script-loading techniques, so I am not really excited about including it, as it will seem like a weak example.
yeah, you're probably right
on the runtime params, my main use case is a map (as in geography) app where what is loaded/displayed is entirely runtime determined, for example by querystring params or by users selecting from lists; the default is a blank canvas. That's not a simple example though. Perhaps a small example would be that if the browser supports WebGL, then import the code that uses that, otherwise import code that uses Canvas.
just to clarify that I've understood correctly: in the Readme example, ./section-modules/
is relative to the importing script, right? So if this were loaded with <script src="https://cdn.com/importingScript.js">
, then ./section-modules/
would be https:/cdn.com/section-modules/
?
Correct.
Ideally polyfills need to be synchronous, but actually we don't have the feature which could conditionally load polyfills synchronous. So I think if (!supportFeatureXXX) import('polyfillForXXX')
is still a valid use case.
@hax , the polyfills do not need to be synchronous as long as they are listed as dependencies within the module or module alias( artificial module with only purpose of filling the polyfills returning the original content).
To clarify, it's not that polyfills need to be synchronous, it's that they need to be able to guarantee execution before any other code runs.
The current readme doesn't really help people understand what this is actually about at a glance; they have to read the whole thing.
I tried this briefly but my examples became a bit too large. Probably if I try again on a fresh day it'll be fine. But contributions from others would certainly be appreciated too.