HIGH-SPEED WEB: START WITH THE END IN MIND
Tags: serverless, static, jam, performance
Published: 2020-07-25 16:29:05

<!-- wp:paragraph -->
<p>The speed of your website is <a rel="noreferrer noopener" href="https://www.cloudflare.com/learning/performance/more/website-performance-conversion-rates/#:~:text=Website%20performance%20has%20a%20large,targeted%20action%20on%20that%20webpage." target="_blank">important to users</a>. But in this <a rel="noreferrer noopener" href="https://channels.theinnovationenterprise.com/articles/why-cloud-computing-is-so-popular-and-how-it-transforms-business#:~:text=Cloud%20computing%20is%20gaining%20popularity,in%20computer%20software%20and%20hardware." target="_blank">age of pay-per-compute</a>, reducing processing all along the chain is important to keep owner costs down too. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Fortunately, in today's modern web, both of these seemingly competing requirements actually complement each other; reducing processing costs can be achieved by improving site speed. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To explore how to improve site speed and reduce overall processing, lets start with the end in mind and work backwards.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Starting with the customer, what consititutes a 'good' user experience, in terms of speed?</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Ultimate User Experience?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Imagine you are a first time visitor to your site. The ideal situation would be if an immediate view of your site was instantly available to the device you're using. No javascript processing, no querying for data, no image optimizations, just the browser showing HTML and [inline] CSS (or even one big snapshot image of your content). </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Bear with me, I know some sites need to show dynamic data and some do not, but remember, at this point, you're just an ordinary user. You dont care about what goes on in the browser and on the server. Neither do I care what static vs dynamic is, or what pain it takes to achieve results. You just want a great experience.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>As a user, I want to see the content immediately. By the time I've made sense of the initial page (<a href="https://web.dev/interactive/" target="_blank" rel="noreferrer noopener">0-3.8secs</a>), I want to interact with it.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If the data a customer is viewing is updated server-side while the page is open, these updates should be pushed to me automagically. Getting new data should not rely on me to fetch the data e.g. by hitting some kind of refresh button to call back to the server.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If the customer leaves the site and comes back to it, by the time I have indicated that I wish to view the page (<a href="http://tim.igoe.me.uk/2012/02/10/google-chromes-new-features-good-or-bad/" target="_blank" rel="noreferrer noopener">preemptive rendering</a>?), it should already be fully loaded and no stale data exists on the screen. If any updates have been made to the page since I last saw it, the changes, and only the changes, should have been pushed to my device, using as little bandwidth as possible.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>All sounds great? But are these demands even possible using the latest tools/technologies for web and server?  </p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Server Side Rendering</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Arguably one of <a href="https://www.cloudflare.com/learning/performance/more/website-performance-conversion-rates/#:~:text=Website%20performance%20has%20a%20large,targeted%20action%20on%20that%20webpage." target="_blank" rel="noreferrer noopener">the most important thing</a>s is to show the content of your site in the quickest way possible. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Generally, the steps that a browser takes to display a web page are as follows:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>A request to the server is made for a webpage.</li><li><strong>The server decodes this request and the page, and its resources (files), are downloaded.</strong></li><li><strong>The web browser uses the page resources to build the page.</strong></li><li>The page then is rendered (displayed) to the user.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>The bottle-neck in this process are steps 2 + 3 i.e. downloading the resources and 'building' the page as the resources become available. Rendering a 'built' page, stage 4, is what a browser is really good at. </p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3><strong>Can we improve, or even skip, steps 2 + 3 to give users a better experience?</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If the customer's browser isnt going to do the hard work to pull all the resources together to build the page, who's going to do it then? </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Combining and building the page can be perfomed on the server instead. The subsequent 'built' page can be the resource that is served to the customer.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Server side rendering is an <a href="https://dev.to/sunnysingh/the-benefits-and-origins-of-server-side-rendering-4doh#:~:text=The%20origin%20story,dynamic%20code%20in%20an%20index.&amp;text=Data%20is%20being%20retrieved%20and,list%20all%20on%20the%20server.">'old' method of serving web pages</a>. Before client-side Javascript was as powerful as it is today, SRR was a common way to serve dynamic web pages.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>What's changed is that now you can use Javascript on both the client and server. You can now replace PHP, Java, ASP etc on the server with NodeJs. This certainly helps with code reuse between client and server, but are we actually any further forward? </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The principles are still the same. Client browser makes call to server, server dynamically creates a webpage containing an initial view of the page, server then delivers the page to client. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Server side rendering certainly solves the processing problem for the customer but we've just pushed the problem server-side. Not only have we increased the amount of processing that we, the site owner, has to pay for. We are also not much further in improving the overall speed of the site. Certainly, the customer may see a more complete view of your site sooner. Also, the server may have better access to some of the resources. But the overall amount of processing to build the page stays relatively the same. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If we, as a site owner, are now paying for the processing to 'build' an inital view of the site how can we make this process as efficient as possible?  </p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Static First</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The vast majority of content on the web changes infrequently. Even sites with 'dynamic' content, usually only a small amount of the total content of a page is truly dynamic. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If the same content is going to be viewed by more than one visitor, why generate it more than once? It might not be a big issue if you only have 2 visitors. But even with 11 visitors, you still <em>might</em> be doing 10x more processing than was needed.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If as much of your content can be precompiled i.e. the data has already been fetched, the styles applied and html generated preemptively, it can be delivered to the user quicker. If fact, if the content is already compiled, we can take the server out of the chain completely for this interaction, and allow the browser to access the content directly from a <a rel="noreferrer noopener" href="https://www.networkworld.com/article/3224893/what-is-edge-computing-and-how-it-s-changing-the-network.html" target="_blank">location close to the customer</a>. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The 'static first' strategy is to compile a static view of the page first, serve it from a CDN, delay enabing javascript until the page has loaded, then hydrate any stale dynamic data. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><em><strong>By adopting static first you can potentially reduce your hosting costs to <a rel="noreferrer noopener" href="https://medium.com/@bezdelev/cost-breakdown-for-a-static-website-on-aws-after-18-months-in-production-d97a932d2d25" target="_blank">cents per month</a>, as opposed to dollars, AND provide a blisteringly fast experience for your customers</strong></em></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>But what about pages that are never viewed? To statically generate an entire site, you need to generate and manage updates for <strong>all </strong>potential routes in your website. You might be generating hundreds or thousands of web pages that real users may never visit. However, although 'real' users may not visit these pages, it is likely, and welcome, that at least 1x crawler bot will want to access these pages (unless it is content not for the eyes of search engines). </p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Caching Vs Static Site</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>So if having assets ready and available on the network, close to the user, is preferable. Is this not just <a href="https://en.wikipedia.org/wiki/Web_cache">server caching</a>?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Yes and no. There are a number of different <a href="https://wp-rocket.me/blog/different-types-of-caching/">caching options</a> available for your site. You can cache the individual items that are referenced by your page e.g. images, css files, database queries etc, or you can cache the page itself, or both. A static first strategy will try to cut the cord with the server.  The strategy does not require database query caching and processes as much into a cachable unit i.e. page caching. Caching is generally performed dynamically i.e. the caching happens when one or more users access a particular page. Static site generation is performed pre-emptively i.e. before any users access a particular page. Caching and static site generation both have the aim of making reused assets as available and as close to a user's device as possible; the difference is if this is done entirely pre-emptively or dynamically.  </p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Static First, Only Update Dynamic Data</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>However, frequent updates may be unavoidable depending on the type of site. For dynamic sites, it is not feasible to continually pre-compile all views for all users, especially when the data is changing frequently. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>But remember again, your user does not care. Mostly, they dont understand the difference between static and dynamic sites; they want to see the important content, fast. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can aim to statically compile as much of the page as possible beforehand, but the 'dynamic' parts will involve some sort of processing. As a first time user, I may accept having the page load, seeing a placeholder where the dynamic data should be, and then 'hydrating' the data on page load. On the other hand, the user may, in this instance, prefer a slightly slower page load, if the page loads with the data already fully 'hydrated'. The choice <a rel="noreferrer noopener" href="https://www.toptal.com/front-end/client-side-vs-server-side-pre-rendering" target="_blank">probably depends</a> on what makes your customer happiest. </p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Subsequent Visits &amp; Data caching</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Up until now we've generally been concentrating on the scenario when customers first visit your site. When a customer visits your site again, the situation is slightly different. A returning customer will already have, on their device, many of the resources for the page. If nothing has changed, they may even already have everything to show the page again.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>As a returning user, it makes little sense for me to have to contact the server again, have a new view generated by the server and download a new page. If only a small subsection of the page I already have has changed, this is unnecessary processing.  </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The ideal situation is if the server actually <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API" target="_blank">pushes my browser updates</a>. When this happens my browser doesnt have to continually ask if new data is available. An even better scenario is if the server has already pushed me the data <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API" target="_blank"><strong>before</strong> I open the page again</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Even if you dont consider websockets and/or service workers you still have the opportunity to cache api data on the server. If a piece of data has not changed since the last time your browser (or any other browser) asked the server for it, it introduces unnessecary processing.  Not for the feint hearted, but api caching can be achieved using the <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag" target="_blank">ETag</a> header of a HTTP call. </p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Final Note. Software Development is hard.</h3>
<!-- /wp:heading -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p>There are only two hard things in Computer Science: cache invalidation and naming things.</p><p>-- Phil Karlton</p></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>There are lots of difficult things about software development, but <a rel="noreferrer noopener" href="https://yihui.org/en/2018/06/cache-invalidation/" target="_blank">cache invalidation</a> is the devil.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To reduce processing, all the methods above require caching in some shape or form. Static website generation is just page caching by another name. Putting things in cache is easy, knowing when to update them is incredibly difficult.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If your website page changes url, should you rebuild your entire site  in case other pages reference the changed page? Does this re-introduce more processing than its worth?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you statically compile your stylesheets inline into the page, what if a stylesheet changes? Does every page need compiled again, even if it doesnt make use of the changed style?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If a property in a row of data in your database changes, how do you invalidate your api cache? What if the data is referenced by another item in the cache?  </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you are a masochist and like solving these type of problems, have at it. For the rest of us mere mortals, look to use a tool or service that helps you manage these problems.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here is a non-exhaustive list of some newer tools and services that help in this space:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a rel="noreferrer noopener" href="https://gohugo.io/" target="_blank">Hugo</a> - Static website builder</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a rel="noreferrer noopener" href="https://www.getshifter.io/" target="_blank">Shifter</a> - Compile your WordPress site to static</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://vercel.com/">Vercel</a> - JAM Stack + React Server Side Rendering + Hosting</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://www.netlify.com/">Netlify</a> - JAM Stack + Hosting</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a rel="noreferrer noopener" href="https://www.webiny.com/" target="_blank">Webiny</a> - Almost serverless Static website builder + API Generation</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a rel="noreferrer noopener" href="https://svelte.dev/" target="_blank">Svelte</a> - Javascript framework that precompiles to plain JS so you dont need to deploy and load a framework with your webapp.</p>
<!-- /wp:paragraph -->