Bundling vs sending serialized dependency graph

Marius Gundersen gundersen at gmail.com
Fri Aug 22 03:54:03 PDT 2014

> One way to do this would be to predeclare the modules, as in:
>    <script type=module src=a.js id=a needs=c load-policy=when-needed>
>    </script>
>    <script type=module src=b.js id=b needs=c load-policy=when-needed>
>    </script>
>    <script type=module src=c.js id=c load-policy=when-needed>
>    </script>

All of these scripts would need to be empedded in every page, which would
significantly increase the size of a document and the complexity in
creating a document. Not everyone is making a Single Page Application, and
needing to maintain many lists of modules used on many pages and
downloading that list on every page load seems wasteful.

Pre-fetching dependencies seems like it could most easily be implemented
using ServiceWorkers[1]. A simple build tool, given a set of modules, can
generate a JSON file with each module id (URI) and its dependencies (list
of URIs). This JSON file would contain the entire dependency forrest from
all roots to all leaves, and could be made available on the webserver for
the ServiceWorker to download. The ServiceWorker now has the entire
dependency graph, and can take the correct action when any one of the
modules in the graph is requested. For example, when module A, which
depends on B, is requested, it can start a request for B as well. Both of
these modules will then be added to the cache, so by the time the Loader
discovers that A depends on B, it will already have been downloaded from
the server. The next time module A is requested (which can be another
session, or in a few minutes), it would know that module B is in the cache,
and nothing needs to be done.

There are other ways implement this, without having to send the entire
dependency graph as a JSON file to the client (for example using bloom
filters and a slightly smarter server), and I'm sure the community will
find the best pre-fetching strategy with the tools already available to

Marius Gundersen

[1]: https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker_API
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140822/e78ea629/attachment.html>

More information about the es-discuss mailing list