Faster Rendering on the Web
Tags: webperf, css, html
Published: 2016-06-14 17:55:49

<p>I had always considered external CSS files a best practice in web development. It does, however, seem fairly&nbsp;counter-intuitive. Why would we fetch the HTML for a page, then wait around for another round trip to fetch the styles, deal with all the handshakes and latency involved in the process, and then eventually render a page. Latency is always a constraint, but it is especially&nbsp;brutal under inconsistent network conditions.&nbsp;Immediacy is the killer feature of the Internet and the World Wide Web,&nbsp;and by imposing a few constraints on our process, we can make it&nbsp;happen way more often.</p>
<p>Performance advocates like <a href="https://twitter.com/igrigorik" target="_blank">Ilya Grigorik</a>, an engineer at Google, have given a lot of talks advocating alternate practices like inlining critical CSS in order to drastically speed up rendering time. A lot of organizations that make good use of these ideas, but it seems to me that many&nbsp;individual developers have not paid much attention to these ideas. It can also be paralyzing to think that new specs like <a href="https://http2.github.io/" target="_blank">HTTP/2</a> will upend some of our current optimization efforts.&nbsp;I think, however, that new&nbsp;specs will be easier to adopt by&nbsp;focusing in now on the general notion that render-blocking latency is an inhibiting factor&nbsp;that can be avoided. HTTP/2&nbsp;features like <a href="https://blog.cloudflare.com/announcing-support-for-http-2-server-push-2/" target="_blank">server push</a>&nbsp;will be an option you may want to consider working towards right now, but browser and server support may&nbsp;put you uncomfortably on the bleeding edge and I think big gains can be made without committing to this process.</p>
<p>We use external CSS files in order to let&nbsp;browsers cache our assets and to simplifying our workflow. I think there are a lot of cases where caching the stylesheets is beneficial, but&nbsp;these benefits are lost on the user in many legitimate cases. Users&nbsp;will need to pay a big upfront cost when they first visit your page and any time you change your CSS files. These are in no way&nbsp;edge cases.&nbsp;If you want to ensure a great user experience every time, you should attempt to make each&nbsp;page render feel immediate.</p>
<p>Addressing the workflow issue: I take the approach of writing&nbsp;all the CSS to external files, then programmatically include critical CSS in a <code>&lt;style&gt;</code> tag in the head. This will be done different ways depending on your server-side environment, but ensures I do not repeat myself and take on a programming flow that can be more easily explained when it comes to collaborating. Non-critical CSS (perhaps thought of as, "below the fold" content) should be &ldquo;asynchronized&rdquo; so that they do not block the page rendering.</p>
<p>This asynchronization of CSS is such an &ldquo;anti-pattern&rdquo; that you cannot even achieve it without a blatant hack. But it is a hack that does not add substantial complexity or debt, so it is worthwhile. There are a few alternate ways to achieve the asynchronization, but my&nbsp;approach is to add a bogus media attribute to the stylesheet link to any random string, such as &ldquo;butthead&rdquo;. You can use any string other than the functional keywords such as "all", "print", "screen", etc.</p>
<pre class="language-markup"><code>&lt;link 
  rel="stylesheet" 
  media="bogus" 
  href="styles.css" 
  onload="if(media!='all')media='all'"
/&gt;</code></pre>
<p>For any page where time to render will have a critical impact on user experience, we should eliminate the latency tax as much as we can. There are various projects and specifications working to address some of these concerns. Google&rsquo;s <a href="https://www.ampproject.org/" target="_blank">Accelerated Mobile Pages&nbsp;project</a>&nbsp;enforces the constraint of inline CSS explicitly and it will be interesting to see what influences&nbsp;it will have&nbsp;in the future of the web. HTTP/2, and all future specs will continue to change&nbsp;a lot of &ldquo;best practices&rdquo; as well. There are also advances is "offline-first" approaches&nbsp;that are worth looking into. All of this can be taken advantage of when it is appropriate to do so, but don't feel like you need to learn all of this in order to make performance a priority, especially if it will complicate things. The best optimizations add little&nbsp;complex overhead. Focusing on a few basic common-sense principles around latency will help you achieve a lot of gains now and put you in the mindset to make the right decisions going forward.</p>
<p>Eliminating asset round-trips can be a gateway drug for thinking about performance in general. If you find this stuff interesting, there is a lot you can learn. The rabbit hole goes deep, though, and we do not want to &ldquo;over-optimize&rdquo; or &ldquo;optimize prematurely&rdquo; and&nbsp;introduce the kind of overhead that makes communication with other developers&nbsp;difficult. By adopting a few high level constraints into your projects, these optimizations will become an empowering part of your workflow and not a complication.</p>
<h2>One last thing</h2>
<p>The&nbsp;application landscape is fracturing and the native vs web vs bots vs whatever debate will rage on.&nbsp;This might make you less concerned with specific web optimization strategies, but I see it as an opportunity to focus the "web" on the web's purpose, which is, as I mentioned before, immediacy. We have expectations of immediacy on the web. Most developers will build a website or two along the way no matter what their specialization, and learning to deliver on the platform's primary ambition&nbsp;is a fulfilling exercise for any developer.&nbsp;</p>