As the HTTP2 protocol (originally started out from the SPDY protocol) is now supported[2] among all major browsers in all recent (and lots of older versions[3]) I thought a lot about our/my typical approach on how to build/serve assets of a website.
Due to the restrictions of HTTP1.x we used to concatenate CSS and JS files to improve loading performance of websites. There are several other techniques like domain sharding for instance, which doesn't really touch my typical type of work, but is now going to get obsolete by HTTP2.
There are quite a few articles out there who share the same opinion:
We’ll be able to cut the following activities out of our regular routines:
Concatenating CSS and JavaScript. Organizing your assets during development according to the sections of your application they are used will make much more sense, because HTTP requests are cheap on HTTP/2.[1]
HTTP/2 and You by Adam Henson, 18 March 2016
This is everything-you-thought-you-knew-is-wrong kind of stuff. In an HTTP/2 world, there are few benefits to concatenating a bunch of JS files together, and in many cases the practice will be actively harmful.[2]
Building for HTTP/2 by Rebecca Murphey, 25 November 2015
All that means not only are the old HTTP1 techniques not needed, they'll actually make things slower. You may be loading assets that are not required for the page being viewed (concatenation and spriting are likely to do this), and sharding invokes DNS lookups which slow things down, despite HTTP2 meaning you don't need to shard in the first place.
The long and short of it is; when you build a front-end to a website, and you know it's going to be served over HTTP2 - you need to ensure you're not using legacy HTTP1 performance techniques that are going to harm the site under HTTP2.[3]
HTTP2 for front-end web developers by Matt Wilcox, 03 March 2015
However, concatenating files is no longer a best practice in HTTP/2. While concatenation can still improve compression ratios, it forces expensive cache invalidations. Even if only a single line of CSS is changed, browsers are forced to reload all of your CSS declarations.[4]
HTTP/2 For Web Developers by Ryan Hodson, 10 December 2015
So how do we get rid of the concatenation? This is what I have in mind and would like to be discussed.
I've set up a simple method called detectComponentsFromMarkup() in the App class which is looking for DOM nodes with a specific class pattern. The pattern is pretty simple.
<div class="@$component-name">My shiny component.</div>
So you have the name of the component and two special characters. Where @
means that the component is requiring CSS and $
means it is requiring JavaScript.
I do not insist on this pattern or implementation rather on the approach. I don't care if it's all done by data-*
attributes or with a class pattern or anything else.
In this case our app would inject the CSS of this component into the document and would tell the App to require/import the JavaScript parts of the component. It's also setting up the correct CSS class selector so that the injected CSS will have any effect. So far so good…
There is still quite some effort to put into this as I don't know by now how to handle the "injection" of the components JavaScript code. There are quite a few factors like ES6 modules, import/export syntax, to transpile or not, and dynamically loading using SystemJS (or similar libraries) that make it a bit tricky. I'm still investigating, but maybe one of you guys already has a good idea to throw in?