Continuing the discussion from Element Parsing Listeners:
What would be nice for handling async dependencies would be if we could include an after
attribute on scripts loaded with async
, stating that the script is not to be run until after the script or scripts with the specified id
have been run (and/or discovered/created):
<script src="/vendor/aep.js" async id="await-element-polyfill">
<script src="app-logic.js" async after="await-element-polyfill">
The obvious way to polyfill this (to me) is to use an attribute like data-src
instead of src
on script tags that use after
, but this loses out on having the script be potentially pre-loaded by the UA (without cache-warming/ServiceWorker hacks that still can’t implement pre-compilation).
(Edit: actually, if the semantics of after
are changed to be defined instead of async
, then a polyfill might be able to make this change without requiring changes to the HTML, by tweaking script tags as they’re discovered.)
The alternative (rewriting the script’s content to use an approach like AMD) introduces non-trivial overhead on the behalf of the author (which includes the author’s toolchain), especially in the event that the script can be included with page-defined dependencies (which makes this somewhere between non-trivial and impossible for requiring dependencies from a global CDN).
If “page-defined dependencies” sound far-fetched to you, realize that this is the nature of almost all polyfills - allowing this kind of deferment to be defined by the page allows for async scripts to be written using polyfilled APIs with no changes to the script in the event that the polyfill is replaced/omitted.