Are JS loaders snakeoil?
github.com
github.com
For our application, every page has a defined JSON file with dozens of resources that the page uses. CSS and Javascript.
When the user asks to load a new page, the loader clears the DOM, fetches the JSON (if it hasn't before), and loads and executes the necessary resources.
This reduces the amount of data transferred from the server, since only the necessary files are loaded for each page. They are loaded only once, since the loader keeps track of what it already fetched. On top of that, HTML is only fetched once from the server. After that, it's all DOM manipulation.
Seems complex? Sure, if you only have 100K in JavaScript. If you have 2MB, it's really the only way to keep things sane.
For each job, a proper tool.
"2MB of Javascript" and "sane" read somewhat funny in the same sentence.
However, to download few widgets and +1 buttons on a static blog page, script loaders are an overkill.
This doesn't make any sense for a javascript-based client. The server code by definition is already as lean as it can be, because everything that could be moved to the client already has been.
We can argue about the merits of having a client-heavy vs server-heavy code base, but that's not what this is about.
It necessarily depends on the details of the situation, but it naively seems that it should be possible to not have users download 2MB of JavaScript if they aren't going to use it by rearchitecting the data or doing some server work, instead of sending them different JavaScript to manage that from the client. Perhaps even more naively, if this is done correctly it will reduce burden on both the client and the server instead of just the server (provided the cost of the loader is actually mitigated by the savings).
With that in mind it can make sense regardless of whether you favour server or client bound implementations.
That's fine for a traditional layout, but most client-heavy applications do not load HTML every time a user clicks something. In my case, the application only loads the HTML once, on the initial visit, and from that point, it's all javascript. Re-working things to load HTML on every click would actually make things slower, since it has to fetch the HTML each time. This type of architecture makes a loader necessary.
For example, third party javascript files for social media sharing and liking buttons often take a miserable amount of time to load. Bringing them in asynchronously can dramatically improve the user experience of a site because the user doesn't have to wait for them to load before seeing the real content.
A similar reason is part of why tables are a bad layout strategy for content in HTML. Back in the day, some (all?) browsers would not render a table until everything in it had loaded. So if you put your entire page in a table, and that table had a big image, the image would block the rendering ofthe page.
That is what javascript loaders are about. Non-blocking loading of not-immediately-required resources.
They can just be module loaders or module systems.
I should have said that discussion missed the point(s) of script loaders...
If your site is rendered client side with backbone and templating etc, then there probably aren't many gains to be had there.