News for hackers. Stuff that matters.
Re:LoadJS – A tiny async loader for modern browsers (710 bytes) by robocat on Wednesday December 14, 2016 @04:21AM (#13174762)
> This looks like the perfect case for using Promises...

I think promises are a bad idea if loading JavaScript is on the critical path to rendering your page. Unless you have so much JavaScript that parsing takes a long time and you need to yield to the layout and composition engine...

I wrote my own bundle loader so that:

* cached JavaScript loads fast

* CSS is put into a JavaScript string and loaded as a JavaScript file so (1) success and error handling code is simplified and (2) because error handling for CSS is hard -- e.g mycss.js looks like insertCssStyle('.much {css: here}');

* Small images are compiled into the CSS as data. Fonts are loaded in the CSS as data (avoiding a couple of bugs).

* it starts loading in <head> (avoiding much delay).

* It inserts <script> elements (rather than use XHR and eval) so that stack traces are readable, and cross origin works well, and so the browser can multithread the loading and parsing.

* it requests scripts from a single cross origin cloudflare host (for speed and caching), but re-requests from the page's domain if that isn't working (for reliability).

* loading failures are logged to third party (for checking reliability).

* JavaScript bundles are wrapped in try/catch for logging, and they explicitly call a function too indicate the bundle loaded successfully (and to chain dependencies correctly for async loading).

It is hard to make a single page app load reliably, and to report failures correctly if it fails!

There are no comments on this comment yet.