title: Journal-Hydration
url: https://adactio.com/journal/16404
hash_url: 911b72ae5d6e140268adf8591aae7df3

<p>As you may have noticed, <a href="https://adactio.com/tags/progressive%20enhancement">I’m a fan of progressive enhancement</a>.</p>

<p>It’s not cool. It’s often at odds with “modern” web development, so I end up looking like an old man yelling at a cloud to get off my lawn. Or something.</p>

<p>At its heart though, progressive enhancement seems fairly uncontroversial and inoffensive to me. It’s an approach. A mindset. Here’s how I describe it in <a href="https://resilientwebdesign.com/chapter6/#Identify%20core%20functionality"><cite>Resilient Web Design</cite></a>:</p>

<ol>
<li>Identify core functionality.</li>
<li>Make that functionality available using the simplest possible technology.</li>
<li>Enhance!</li>
</ol>

<p>Progressive enhancement makes use of <a href="https://adactio.com/journal/14327">the principle of least power</a>:</p>

<blockquote>
  <p>Choose the least powerful language suitable for a given purpose.</p>
</blockquote>

<p>That’s step two of the three-step process. But the third step is vital.</p>

<p>I think a lot of the hostility towards progressive enhancement comes from a misunderstanding of that three-step process, perhaps thinking that it stops at step two. I’m sure that some have intrepreted progressive enhancement as <em>preventing</em> developers from using the latest and greatest technology. Nothing could be further from the truth!</p>

<blockquote>
  <p>Taking a layered approach to building on the web gives you permission to try cutting‐edge JavaScript APIs, regardless of how many or how few browsers currently implement them.</p>
</blockquote>

<p>The most common misunderstanding of progressive enhancement is that it’s inherently about JavaScript. That’s not true. You can apply progressive enhancement at every step of front-end development: HTML, CSS, and JavaScript.</p>

<p>But because of JavaScript’s strict error-handling model (at least compared to HTML and CSS), it’s in <a href="https://adamsilver.io/articles/javascript-isnt-always-available-and-its-not-the-users-fault/">the JavaScript layer</a> that the lack of a progressive enhancement mindset is most often felt.</p>

<p>That’s why I was saddened by the rise of frameworks and mindsets that <em>assume</em> the availability of JavaScript. Single page apps generally follow this assumption. Everything is delivered via JavaScript: content, markup, styles, and behaviour.</p>

<p>This leads to a terrible situation for performance. The user is left staring at a blank screen, waiting for something—anything!—to appear. Browsers are optimised to stream HTML as soon as they can. Delivering your content via JavaScript rather than HTML means you’re not taking advantage of that optimisation. Your users suffer.</p>

<p>But I was very heartened when I saw the pendulum start to swing back the other way a bit…</p>

<p>Let’s say you’re using a JavaScript framework like React. But the reason you’re using it isn’t because you’re doing anything particularly complex in the browser involving state management. You might be using React because you really like the way it encourages modularity and componentisation.</p>

<p>A few years ago, making a single page app was pretty much the only way you could use React. For you as a developer to experience the benefits of modularity and componentisation, users had to pay the price in the payload (and fragility) of client-side JavaScript.</p>

<p>That’s no longer the case. Now that we can run JavaScript on the server, it’s possible to build in a modular, componentised way and still use progressive enhancement.</p>

<p>When I first heard about <a href="https://www.gatsbyjs.org/">Gatsby</a> and <a href="https://nextjs.org/">Next.js</a>, I thought that was the selling point. Run React on the server; send pre-generated HTML down the wire to the user; then enhance with client-side JavaScript.</p>

<p>But that’s not exactly how it works. The pre-generated HTML isn’t functional. It still needs a bucketload of JavaScript before it can do anything. The actual process is: Run React on the server; send pre-generated HTML down the wire to the user; then send everything again but this time in JavaScript, bundled with the entire React library.</p>

<p>This leads to a situation for users that’s almost worse than before. Instead of staring at a blank screen, now they get HTML lickety-split—excellent! But if they try to interact with what’s on screen, they’ll find that nothing is working yet. Even worse, once the JavaScript is delivered, and is being parsed, they probably can’t even scroll—their device is too busy interpreting all that JavaScript. Your users suffer.</p>

<p>All your content is sent twice. First HTML is sent from the server. These days this is called “server-side rendering”, even though for decades the technical term was “serving a web page” (I’m pretty sure the rendering part happens in a browser). Then a JavaScript library—plus all your bespoke JavaScript—is loaded. Then all your content is loaded <em>again</em> as JSON.</p>

<p>So you’ve got a facade of an interface that you can’t actually interact with until a deluge of JavaScript has been loaded, parsed and executed. The term used for this stage of the process is “hydration”, which makes it sound more like a relaxing treatment from Gwyneth Paltrow than the horrible user experience it is.</p>

<p>The idea is that subsequent navigations—which will happen with Ajax—should be snappy. But the price has already been paid by then. The initial loading experience is jagged and frustrating.</p>

<p>Don’t get me wrong: server-side rendering is <em>great</em> …if what you’re sending from the server is functional. It’s the combination of hollow HTML sent from the server, followed by a huge browser-freezing dump of JavaScript that is an anti-pattern.</p>

<p>This use of server-side rendering followed by hydration <em>feels</em> like progressive enhancement, because it separates out the delivery of markup and scripts. But it’s missing the mindset.</p>

<p>The layered approach of progressive enhancement echoes the separation of concerns in the front-end stack: HTML, CSS, and JavaScript—each layer expressing more power. But while these concepts are related, they’re not interchangable. Separating out the layers of your tech stack isn’t necessarily progressive enhancement. If you have some HTML that relies on JavaScript to be useful, then there’s no benefit in separating that HTML into a separate payload. The HTML that you initially send down the wire needs to be functional (at least at a basic level) before the JavaScript arrives.</p>

<p>I was a little disappointed to see Kyle Simpson—who I admire greatly—conflate separation of concerns with progressive enhancement in <a href="https://www.youtube.com/watch?v=HVSkcnIJEC0">his talk from JSCamp 2019</a>:</p>

<blockquote>
  <p>This content is here. I can see it, and it’s even styled. But I can’t click on the damn button because nothing has loaded in the JavaScript layer yet.</p>
  
  <p>Anybody experienced that where you’ve been on a web page and it’s not really fully functional yet? I can see something but I can’t actually make any usage of it yet.</p>
  
  <p>These are all things that cropped out of our thought process that said: “Let’s build the web in layers. Let’s deliver it progressively in layers. Because that’s morally right. We call this progressive enhancement. And let’s not worry too much about all these potential user experience flaws that may happen.”</p>
</blockquote>

<p>That’s a spot-on description of server-side rendering and hydration, but it’s a gross mischaracterisation of progressive enhancement.</p>

<p>That button that requires JavaScript to work? That should’ve been generated with JavaScript. (For example, if you’re building a complex web app, consider sending a read-only view down the wire in HTML—then add any interactive interface elements with JavaScript in the browser.)</p>

<p>If people are equating progressive enhancement with thoughtless server-side rendering and hydration, then I can see why they’d be hostile towards it.</p>

<p>Users would be better served with <a href="https://vimeo.com/190834530#t=7m6s">unprogressive non-enhancement</a>:</p>

<blockquote>
  <p>You take some structured content, which follows the vertical flow of the document in a way that everyone understands.</p>
  
  <p>Which people traverse easily by either dragging their scroll bar with their mouse, or operating the keyboard using the up and down keys, or using the spacebar.</p>
  
  <p>Or if they’re using a touch device, simply flicking backwards and forwards in that easy way that we’ve all become used to. What you do is you take that, and you fucking well leave it alone.</p>
</blockquote>

<p>Alas, that’s not what tools like Gatsby offer. The latest post on their blog is called <a href="https://www.gatsbyjs.org/blog/2020-01-30-why-gatsby-is-better-with-javascript/"><cite>Why Gatsby is better with JavaScript</cite></a>:</p>

<blockquote>
  <p>But what about sites or pages where there is no client-side interactivity? Even for those pages, Gatsby offers performance benefits by including JavaScript.</p>
</blockquote>

<p>I beg to differ.</p>

<p>(By the way, that same blog post also initially tried to equate the performance hit of client-side JavaScript with the performance hit of images. <a href="https://hankchizljaw.com/wrote/honesty-is-the-best-policy/">Andy explains why that’s disingenuous</a>.)</p>

<p>Hope is on the horizon for React in the form of partial hydration. I sincerely hope that it will become the default way of balancing server-side rendering with just-in-time client-side interaction.</p>

<p>The situation we have now is the worst of both worlds: server-side rendering followed by a tsunami of hydration. It has a whiff of progressive enhancement to it (because there’s a cosmetic separation of concerns) but it has none of the user benefits.</p>
