|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Moving CSS Wizardry onto HTTPS and HTTP/2" |
| 4 | +date: 2017-01-25 11:45:58 |
| 5 | +categories: Web Development |
| 6 | +meta: "Improving security and performance on CSS Wizardry" |
| 7 | +--- |
| 8 | + |
| 9 | +Hopefully by now, you should be able to [read this article over |
| 10 | +HTTPS](https://csswizardry.com/2017/01/moving-css-wizardry-onto-https-and-http-2/) |
| 11 | +and HTTP/2. (If that link doesn’t work, please hit your back button and [let me |
| 12 | +know](https://twitter.com/csswizardry)!) I made the switch yesterday, and all |
| 13 | +being well the new DNS settings have propagated through. At some point soon I |
| 14 | +will begin forcing all HTTP traffic to HTTPS. |
| 15 | + |
| 16 | +The site is still built with Jekyll and hosted on GitHub Pages, but it’s now |
| 17 | +fronted by Cloudflare who offer a plethora of [performance-related |
| 18 | +services](https://www.cloudflare.com/performance/). I was only really interested |
| 19 | +in HTTPS because it allows me to use, among other things, HTTP/2[^1], Brotli, |
| 20 | +and Service Worker (I’m not actually using the latter two yet, but now at least |
| 21 | +I can), and when my buddy [James](https://twitter.com/jameskirkby) tipped me off |
| 22 | +as to [just how |
| 23 | +simple](https://blog.cloudflare.com/secure-and-fast-github-pages-with-cloudflare/) |
| 24 | +the process is, I got stuck in and got it done. |
| 25 | + |
| 26 | +As an aside, I had some issues specific to my domain registrar (not GitHub or |
| 27 | +Cloudflare) which my very good friend |
| 28 | +[Steve](https://twitter.com/StephenMelrose) helped out with. He’s a very |
| 29 | +talented software and Ops engineer who managed to solve in _minutes_ a problem |
| 30 | +that had troubled me all day. Thanks, Steve! |
| 31 | + |
| 32 | +## HTTPS |
| 33 | + |
| 34 | +My site is completely static; it doesn’t take any user input, there is no logic |
| 35 | +or scripting, no database, so I didn’t feel like the need for HTTPS _from a |
| 36 | +security point of view_ was all that pressing[^2]. However, HTTPS is a |
| 37 | +prerequisite for a number of other technologies, and having a secure |
| 38 | +site—regardless of its content—is never a bad thing. More on the first bit in a |
| 39 | +moment, but for now I want to talk a little about security. |
| 40 | + |
| 41 | +I imagine that although most users won’t have much of a understanding as to the |
| 42 | +technical implications behind a secure or a not-secure site (I’m hesitant to use |
| 43 | +the word _insecure_), they are becoming aware that there is such thing as |
| 44 | +security. With Google’s intent to [mark certain websites as |
| 45 | +insecure](https://security.googleblog.com/2016/09/moving-towards-more-secure-web.html), |
| 46 | +and the general increase in awareness, users _do_ know that secure websites are |
| 47 | +a thing. The implication, therefore, is that any site that isn’t explicitly |
| 48 | +marked as being secure must be insecure (whether that’s actually the case or |
| 49 | +not). |
| 50 | + |
| 51 | +To this end, I think that HTTPS is as much an exercise in branding and trust as |
| 52 | +it is in security, and that it will steadily become more and more ubiquitous. |
| 53 | +This is only a good thing. |
| 54 | + |
| 55 | +## HTTP/2 |
| 56 | + |
| 57 | +HTTP/2 (or H2, as it’s commonly referred to) is a vast, vast improvement on the |
| 58 | +HTTP/1.1 protocol that we’ve been using for almost two decades. It brings many |
| 59 | +benefits to both developers and users, but many of its best features are centred |
| 60 | +around performance: |
| 61 | + |
| 62 | +* Compressed headers: HTTP/1.1 sends its headers uncompressed, which creates a |
| 63 | + surprising amount of overhead. HTTP/2 reduces that by compressing the response |
| 64 | + headers as well as the response body. |
| 65 | +* Multiplexing: get around head of line blocking and lack of parallelisation by |
| 66 | + sending multiple assets asynchronously over the same TCP connection. |
| 67 | +* Server push: allows developers to send late requested assets preemptively. |
| 68 | + |
| 69 | +A lot of HTTP/2’s additions will moot the domain sharding, concatenating, and |
| 70 | +inlining strategies we came up with as hacks, and will instead allow us to |
| 71 | +deliver faster experiences with simpler architectures: we can optimise assets |
| 72 | +all we like, but there is no denying that HTTP/2 gives developers an astounding |
| 73 | +performance boost right out of the box. |
| 74 | + |
| 75 | +Further, HTTP/2 is required in order for certain other technologies to be |
| 76 | +utilised: |
| 77 | + |
| 78 | +* Brotli, an improved compression algorithm from Google, needs to run over |
| 79 | + HTTP/2 because of third parties (ISPs, proxies, etc.) infamously trying to |
| 80 | + recompress already compressed transfer. By preventing them getting at at all, |
| 81 | + it means that they can’t try running gzip over a new, unknknow content |
| 82 | + encoding (e.g. Brotli). |
| 83 | +* Service Worker absolutely needs to run over HTTP/2, because it’s basically a |
| 84 | + man in the middle. We’re building a proxy that sits in between our users and |
| 85 | + our servers, so the need for security there should be pretty clear. |
| 86 | + |
| 87 | +Currently I’m not making that much use of anything HTTP/2 offers me other than |
| 88 | +multiplexing and header compression (because I didn’t even have to lift a |
| 89 | +finger) for those. My site is already pretty slim, and I’m serving so few assets |
| 90 | +that one could almost argue over-engineering, but one really great example of |
| 91 | +where I will benefit from HTTP/2 multiplexing is this [relatively large list of |
| 92 | +images on the homepage](/#section:clients). |
| 93 | + |
| 94 | +## Browser Support |
| 95 | + |
| 96 | +Support for HTTP/2 is [pretty good](http://caniuse.com/#feat=http2), and always |
| 97 | +improving. Servers capable of serving over HTTP/2 will also still deliver to |
| 98 | +HTTP/1.1 clients, so nothing will break. |
| 99 | + |
| 100 | +However, it might not be time for your company to switch over too. |
| 101 | +Unfortunately, HTTP/2 best practices become bad practices in HTTP/1.1, and |
| 102 | +HTTP/1.1 best practices become bad practices in HTTP/2. Optimising for one will |
| 103 | +be detrimental to the other, and it’s pretty hard to satisfy both camps. |
| 104 | + |
| 105 | +Fortunately for me, looking at the data, over the past 24 months over 85% |
| 106 | +of CSS Wizardry’s traffic has come from a browser that supports HTTP/2. For me |
| 107 | +it’s pretty clear that using HTTP/2 is the right decision. Other companies |
| 108 | +(government, ecommerce, etc.) might not have quite such a clear cut view. |
| 109 | + |
| 110 | +## What Next |
| 111 | + |
| 112 | +Next up, I can look at implementing a simple Service Worker to provide better |
| 113 | +caching strategies, as well as a simple offline page for users on poor or |
| 114 | +non-existent connections. I should also look into splitting up my CSS into more |
| 115 | +granular, individually cacheable chunks. |
| 116 | + |
| 117 | +Unfortunately, given that I am still hosting on GitHub Pages, I am limited in |
| 118 | +how much I can implement. Things like enabling Brotli will have to be done by |
| 119 | +Cloudflare, and utilising Server Push would require access to server-level |
| 120 | +configuration. That stuff will have to wait, but I can probably manage without |
| 121 | +it whilst I’m serving up a flat-file website with such a small footprint in the |
| 122 | +first place. |
| 123 | + |
| 124 | +- - - |
| 125 | + |
| 126 | +With all of this said, despite HTTP/2’s clearly superior approach to optimising |
| 127 | +user experiences, there is still great need for fundamental performance |
| 128 | +knowledge within teams: optimising assets, structuring more elegant delivery, |
| 129 | +and building to support non-HTTP/2 environments are all still very important. |
| 130 | + |
| 131 | +If you’d like any advice or help with any of the above, I am lining up |
| 132 | +performance consultancy work for Q2 onward. [Get in touch.](/contact/) |
| 133 | + |
| 134 | +- - - |
| 135 | + |
| 136 | +[^1]: Interestingly, the HTTP/2 standard doesn’t define HTTPS as being mandatory, but almost all browser vendors have stated that they will only support HTTP/2 over a secure connection, which makes it pretty mandatory in practice. |
| 137 | +[^2]: I know, I know: there’s a lot more benefit to HTTPS than that. |
0 commit comments