<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Edmund O'Connell</title>
    <description>The latest articles on DEV Community by Edmund O'Connell (@eddyoc).</description>
    <link>https://dev.to/eddyoc</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1237612%2F9005331a-31f1-4d54-9b38-968fe8a99406.jpeg</url>
      <title>DEV Community: Edmund O'Connell</title>
      <link>https://dev.to/eddyoc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eddyoc"/>
    <language>en</language>
    <item>
      <title>Optimising Performance with Next.js [Part 1]</title>
      <dc:creator>Edmund O'Connell</dc:creator>
      <pubDate>Sat, 30 Dec 2023 12:52:19 +0000</pubDate>
      <link>https://dev.to/eddyoc/optimising-performance-with-nextjs-part-1-5d7f</link>
      <guid>https://dev.to/eddyoc/optimising-performance-with-nextjs-part-1-5d7f</guid>
      <description>&lt;h1&gt;
  
  
  Initial Server Response Time
&lt;/h1&gt;

&lt;p&gt;Next.js offers the a vast toolkit of mechanisms with which to optimise the performance of a website written in React. If SEO is important to your site, optimising it's performance will be critical to boosting it's SERP rankings, not to mention your bounce and &lt;a href="https://www.linkedin.com/pulse/page-load-times-how-affects-bounce-rates-chuck-brockman/"&gt;conversation rates&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Analysing performance for your website is most easily achieved using &lt;a href="https://chromewebstore.google.com/detail/lighthouse/blipmdconlkpinefehnmjammfjpmpbjk"&gt;Lighthouse&lt;/a&gt; in Chrome. Lighthouse can also be included as part of your &lt;a href="https://nextjs.org/learn-pages-router/seo/improve/lighthouse"&gt;Next.js setup&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;When using Lighthouse in Chrome remember to run in &lt;a href="https://travis.media/run-lighthouse-chrome-extension-incognito/"&gt;Incognito mode&lt;/a&gt; as any installed extensions can and likely will negatively impact your scores. &lt;/p&gt;

&lt;p&gt;There are several ways to improve the performance score achieved in Lighthouse. In this article I shall explain the techniques employed by our team to reduce one of the most critical performance measures - the Initial Server Response Time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CV93_ED3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/awbg4xkn4sbe0y18ytp8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CV93_ED3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/awbg4xkn4sbe0y18ytp8.png" alt="Image description" width="800" height="197"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Server-Side Rendering (SSR) vs Static Site Generation (SSG)
&lt;/h2&gt;

&lt;p&gt;Next.js offers two distinct pre-rendering methods to optimise the performance of web applications by minimising the amount of JavaScript sent to the client. These methods are Server-Side Rendering (SSR) and Static Site Generation (SSG).&lt;/p&gt;

&lt;p&gt;In the initial development phase of a Next.js website, it is a common practice to employ &lt;code&gt;getServerSideProps&lt;/code&gt; for constructing the web pages. This method typically constitutes our standard approach. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;getServerSideProps&lt;/code&gt; is explicitly designed to execute on the server side, ensuring that database operations are executed in the server-side code. This facilitates the fetching of data and the rendering of the page at request time, adhering to standard server-side just in time rendering principles.&lt;/p&gt;

&lt;h2&gt;
  
  
  The need for speed
&lt;/h2&gt;

&lt;p&gt;The issue with this approach is speed. Even a snappy read request against an indexed collection in a low latency data repository is likely to consume 150-200ms. This will likely serve as the biggest bottleneck to your initial server response time and will cost your performance big. In our case the database round trip was fully ⅓rd of the page load time.&lt;/p&gt;

&lt;p&gt;The key to eliminating this latency and obtaining lightning fast page load speeds is to move all your pages over to static page generation. In Static Generation, the HTML is generated at build time. The database operations run at build time and bake in the data into pre-cached pages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Incremental Static Regeneration
&lt;/h2&gt;

&lt;p&gt;In practical terms this means that your website is static. Changes to the data in your repository will no longer be automatically reflected on your website. This can be an issue for websites with changing dynamic data and could simply be unsuitable where the database updates need to be propagated instantly. &lt;/p&gt;

&lt;p&gt;Fortunately Next.js provides us with a way of reconstituting these pages at periodic intervals - Incremental Static Regeneration (ISR). Next.js allows us to update static pages after build time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getStaticProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getStaticPropsHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;viewport&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mobile&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;isSsrMobile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;revalidate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10800&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;revalidate&lt;/code&gt; attribute value in the code block here is in seconds. In this example where a request is received and the cached page is older than 10800 seconds (3 hours) then it will serve the cached page. However in the background it will regenerate this page which will then be served for future requests. &lt;/p&gt;

&lt;p&gt;The value of this attribute can be reduced down to as little a one second. ISR is effectively "free" on Vercel. It doesn't hit your quotas for serverless or edge invocations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;source&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/(.*?)&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Cache-Control&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`public max-age=10800 s-maxage=10800, stale-while-revalidate=299`&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember to update your &lt;code&gt;Cache-Control&lt;/code&gt; value in &lt;code&gt;headers()&lt;/code&gt; section in &lt;code&gt;next.config.js&lt;/code&gt; to reflect your chosen cache time.&lt;/p&gt;

&lt;h2&gt;
  
  
  The tradeoff
&lt;/h2&gt;

&lt;p&gt;The tradeoff involved in moving to &lt;code&gt;getStaticProps&lt;/code&gt; will depend on your website. For us this meant a massive increase in build times. Our website &lt;a href="https://singmalls.app"&gt;singmalls.app&lt;/a&gt; is a directory of shopping mall directories and has well over 30,000 pages of merchant screens. Previously merchant pages were run from a single dynamic page using getServerSideProps. Now each page needed to be generated at build time. &lt;/p&gt;

&lt;p&gt;Since each page involves database fetching this increased our build time to well over an hour which proved to be a real issue as Vercel, which only allows a hard maximum of 45 minutes of build time. Anything in excess of this time will result in a deployment failure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Navigating Vercel's Build Time per Deployment Limit
&lt;/h2&gt;

&lt;p&gt;Vercel's 45 minute Build Time per Deployment limit extends across all their plans from Hobby, Pro or Enterprise so simply upgrading your plan will not help. &lt;/p&gt;

&lt;p&gt;This limit then will affect any website using &lt;code&gt;getStaticProps&lt;/code&gt; which has a large number of pages which are database driven.&lt;/p&gt;

&lt;p&gt;To circumvent this issue we created a node.js script which runs at build time which queries the database in parallel batches. This script builds a series of json files which reside locally on the build server.&lt;/p&gt;

&lt;p&gt;By querying these json files at build time rather than a remote database we were able to massively reduce the time required to build each page and took us under Vercel's 45 minute build time limit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Moving from &lt;code&gt;getServerSideProps&lt;/code&gt; to &lt;code&gt;getStaticProps&lt;/code&gt; was by far the biggest win in allowing us to improve our Initial Server Response Time metric to the high 90s. By shifting the database queries to build time and not page request time you shift the pain to your build process and away from the end user who benefits in a much improved zippy user experience.&lt;/p&gt;

&lt;p&gt;Your SEO will noticeably improve as result as itis well documented that Google rewards web pages with &lt;a href="https://www.searchenginejournal.com/ranking-factors/page-speed/"&gt;fast load times&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>react</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
