[whatwg] Script preloading
Yoav Weiss
yoav at yoav.ws
Wed Jul 10 16:10:26 PDT 2013
I'm supportive of this proposal, and I think it'd help declarative script
loading.
I've recently contemplated the slightly related issue of adding the "media"
attribute to <script>, for declarative loading of scripts that are only
relevant to some viewports [1]
While it may complicate certain things (e.g. execution when media
conditions change, dependencies), I believe it's worth while to give it
some thought, as it'd enable preloaders to fetch these scripts as soon as
possible, in case they are needed.
Regarding Kyle's list of use-cases, I think it can be summed up to
something like:
"Authors often need to load 3rd party scripts into their pages (e.g. ads,
widgets, analytics). These scripts are out of their control, and often do
not allow the hosting page to determine when they are executed.
In many cases, either for performance (they trigger download of their own
resources) or for UX purposes (they display something on the screen),
authors need to control when these scripts are executed."
IMO, the proposed "whenneeded" attribute can answer that need.
rel=subresource can also answer it (assuming that browsers will optimize on
it: download the resource, detect its type and parse it (as low priority)
if needed, and keep it in memory regardless of its caching headers), but
IMO "whenneeded" is cleaner.
For the #11 use-case, where you want to delay the execution of some scripts
until an entire group of scripts have finished downloading, but have yet to
run, it could be done with:
<script id="A" src="A.js" dependencies="B C" async whenneeded></script>
<script id="B" src="B.js" async whenneeded></script>
<script id="C" src="C.js" async whenneeded></script>
<script>
var a=document.getElementById("A"),
b=document.getElementById("B"),
c=document.getElementById("C");
a.markNeeded().then(function(){b.markNeeded()}).then({function(){c.markNeeded()});
</script>
That would work, as long as a dependency when the script we're dependent on
is defined as "whenneeded" becomes dependency on its download, rather than
its execution. Does that make sense?
[1]
http://lists.w3.org/Archives/Public/public-whatwg-archive/2013May/0112.html
More information about the whatwg
mailing list