How to use Next.js + React Query for insane loads times with dynamic content
Published: 2021-09-13 07:02:57

<!-- wp:paragraph -->
<p><a href="https://www.infoq.com/news/2020/11/ux-stale-while-revalidate/">Stale-while-revalidate</a> has some exciting implications for the future of web performance: by serving stale data instead of loading spinners, the user experience can be smoother and snappier. It also opens up the possibility for pages with dynamic content to be built to static files at intervals without the need to be server-side rendered on demand, meaning websites can utilise features of a dynamic website without sacrificing the speed of a static one.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This is feasible thanks to a few new technologies which have been cropping up on the JS horizon: A new build option for Next.js, called <a href="https://vercel.com/docs/next.js/incremental-static-regeneration">Incremental Static Regeneration,</a> and a cache-revalidation package called <a href="https://react-query.tanstack.com/">React Query</a>. Alternatives have appeared, such as Vercel's own <a href="https://swr.vercel.app/">SWR</a> package, but all aim to reach the same goal: To handle query caching, error handling, polling and most importantly, to revalidate data when required. These two technologies work in perfect harmony to create super-fast, up-to-date static websites that will amaze clients and improve SEO. </p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":335,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://npmrundev.files.wordpress.com/2021/09/ezgif.com-video-to-gif.gif?w=600" alt="" class="wp-image-335"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>In this article, I'll be showing you how to get the best of both worlds by setting up ISR to do the main content fetching and React Query to 'fill in the gaps' and provide more recent data.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>How it works</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>ISR works by building static 'snapshots' of a populated site: It will go through each page and make all the requests it needs to, finally building each page to a static HTML file. This isn't much different to a regular static site builder; what ISR does differently is it goes back and checks at regular intervals if the data it served has changed. If it has, ISR will rebuild <em>only the pages which have changed</em>. If you have a site with 100 pages and only 5 pages were changed, it skips over the 95 that didn't change and only rebuilds the 5 that did. This saves a lot of processing power while also allowing for more 'dynamic' sites that still have the speed of static sites. It also means that both the developer and the client don't need to worry about rebuilding the site: Everything is handled automatically.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There's a little more to it than that, but that's the general gist. <a href="https://vercel.com/docs/next.js/incremental-static-regeneration">You can read more about the details of ISR here</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>ISR can do the majority of the heavy lifting: For most posts and pages, content does not need to be up to date to the exact minute. Usually 30 seconds or a minute is enough, or even higher in some cases.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Sometimes, however, you need the data you're displaying to be precise. Perhaps you're building an online shop with stock indicators that need to be exact, or you're building a recruitment website that always needs to display fresh jobs. That's where React Query comes in. Using a combination of ISR for menus and general page data alongside React Query for up-to-date listing information, you can build a frontend that is not only fast but also completely up to date.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>0. Installation</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Start by creating your Next.js project using <code>create-next-app</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>npx create-next-app my-project</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Next, let's add <code>react-query</code> to the mix.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>npm i react-query</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>You will also want some kind of CMS which serves up a REST API - your data will be served as JSON objects. I'm using Wordpress' REST API feature for demonstration.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Now we've got everything installed, it's time to get started.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>1. Set up your Next.js page to use Incremental Static Regeneration</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Set up your page in Next.js under the <code>pages</code> folder.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can use this method for either non-dynamic routes for things like homepages and unique pages, or you can use dynamic routes which allow you to re-use the same template for different pages. I'm hard-coding my page ID here for the sake of the tutorial, but I intend on writing a guide for dynamic routes in Next.js in future.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can style up the page now if you want, or wait until you have your data to worry about displaying it on the page.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// pages/index.js

export default function Home(props) {
  return (
    &lt;div&gt;
      &lt;Head&gt;
        &lt;title&gt;My App&lt;/title&gt;
        &lt;link rel="icon" href="/favicon.ico" /&gt;
      &lt;/Head&gt;

      &lt;main&gt;
        &lt;-- your page content --&gt;
      &lt;/main&gt;
    &lt;/div&gt;
  )
}

export const getStaticProps = async () =&gt; {...}</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>The area you need to pay attention to when setting up ISR is the <code>getStaticProps</code> function below the main body. This function is run on the server side during its rendering process, and can be used to prefetch any queries required for that page. In the example below, we use this function to prefetch page data:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// pages/index.js

export const getStaticProps = async ({ params }) =&gt; {
  const pageId = 1; // pageId could be dynamic

  // fetch page data using id
  const pageData = await getPageById(pageId);

  return {
    props: {
      pageData
    }
  };
};</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>The request function, <code>getPageById</code>, can be anything you like so long as it returns the data you're trying to fetch. I used Axios:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// util/queries/pages.js

export const getPageById = async (id) =&gt; {
  const res = await axios
    .get("http://localhost/wp-json/wp/v2/pages/" + id)
    .catch((err) =&gt; err);

  return res;
};</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Setting up Incremental Static Regeneration from here is simple: All you need to do is add a <code>revalidate</code> property to the return object. This value is the number of seconds before Next.js will wait before attempting to revalidate and rebuild the page, once a user requests it again.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If someone visits the page before that number of seconds is up, they will be served the old, cached version of the page. However if someone visits the page after that amount of time, they will be served the old version but a new version of the site will regenerate in the background and update the user when it's ready. That new version is then cached and the cycle repeats.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I've used 60 seconds for the sake of testing, but the time period you use will depend on the app requirements. If it's just a blog, you can probably get away with a longer time period.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// pages/index.js - getStaticProps()

return {
  props: {
    pageData
  },
  revalidate: 60
};</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>To test if ISR is working, you can put some dates on the page. One is returned from <code>getStaticProps</code> at build time, and the other is returned when the page is loaded. I've used <a href="https://momentjs.com/">moment</a> to format the dates to time. Your page will look something like this:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// index.js

export default function Home({ data, updatedAt }) {
  return (
    &lt;div&gt;
      &lt;Head&gt;
        &lt;title&gt;My App&lt;/title&gt;
        &lt;link rel="icon" href="/favicon.ico" /&gt;
      &lt;/Head&gt;

      &lt;main className="p-12"&gt;
        &lt;p className="text-48"&gt;
          Rendered: {moment(new Date(updatedAt)).format("h:mm:ssa")}
          &lt;br /&gt;
          Loaded: {moment(new Date(Date.now())).format("h:mm:ssa")}
        &lt;/p&gt;
      &lt;/main&gt;
    &lt;/div&gt;
  );
}

export const getStaticProps = async ({ params }) =&gt; {
  const pageId = 1;

  // fetch page data using id
  const pageData = await getPageById(pageId);

  return {
    props: {
      pageData,
      updatedAt: Date.now(),
    },
    revalidate: 60,
  };
};</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Run <code>npm run build</code> followed by <code>npm run start</code> to see the result - ISR won't be enabled in dev mode. If the timestamps don't match up, ISR is enabled. You should be able to continually refresh the page for the duration of the <code>revalidate</code> timer before the 'rendered' timestamp updates.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":315,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://npmrundev.files.wordpress.com/2021/08/screenshot-from-2021-08-09-17-20-49-1.png?w=598" alt="" class="wp-image-315"/></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>2. Add React Query context to _app.js</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Now ISR is set up and we've tested it works, it's time to get React Query working. We'll need to create a QueryClient to handle our queries, then add some context to provide our app with a central point of control. The QueryClientProvider allows us to access the same QueryClient using Hooks.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>First we'll need to import some things from React Query:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// pages/_app.js

import { useRef } from "react";
import { QueryClient, QueryClientProvider } from "react-query";</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Next we create a reference to attach to the QueryClientProvider and add logic to create a new QueryClient if one doesn't exist.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// pages/_app.js

const queryClientRef = useRef();
  if (!queryClientRef.current) {
    queryClientRef.current = new QueryClient();
  }</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Finally, we can add our QueryClientProvider and attach our QueryClient using the reference we just made.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// pages/_app.js

return (
    &lt;QueryClientProvider client={queryClientRef.current}&gt;
        &lt;Component {...pageProps} /&gt;
    &lt;/QueryClientProvider&gt;
  );</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Here's how the whole <code>_app.js</code> should look:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// _app.js

import { useRef } from "react";
import { QueryClient, QueryClientProvider } from "react-query";

function MyApp({ Component, pageProps }) {
  const queryClientRef = useRef();
  if (!queryClientRef.current) {
    queryClientRef.current = new QueryClient();
  }

  return (
    &lt;QueryClientProvider client={queryClientRef.current}&gt;
      &lt;Component {...pageProps} /&gt;
    &lt;/QueryClientProvider&gt;
  );
}

export default MyApp;</code></pre>
<!-- /wp:code -->

<!-- wp:heading -->
<h2>3. Prefetch page queries</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>We've now got a QueryClient on the app, so let's give it some data to revalidate. This is the exact same process as you'd usually use to populate a page using <code>getStaticProps</code> - essentially, return the data inside the <code>props</code> object as usual. This data will be revalidated according to the <code>revalidate</code> timer we set before.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// pages/index.js

export const getStaticProps = async ({ params }) =&gt; {
  const pageId = 1; // pageId could be dynamic

  // fetch page data using id
  const pageData = await getPageById(pageId);

  return {
    props: {
      pageData, // this could be data from any kind of request
      pageId
    },
    revalidate: 60 * 5, // 5 minutes
  };
};</code></pre>
<!-- /wp:code -->

<!-- wp:heading -->
<h2>4. Set up client-side queries</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This is where React Query comes in: if ISR will revalidate our page every 5 minutes, React Query is going to revalidate it immediately upon page load to make sure everything is 100% up-to-date.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We'll use the useQuery hook, set an ID for the request in the first parameter, then an arrow function that returns our request function, and for the third parameter we'll tell it the data it's going to be revalidating. This means that the <code>data</code> const we created will initially be set to the data provided in getStaticProps, but when it's revalidated this value will be automatically updated to the new result.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>export default function Home({ pageData, pageId }) {
  const { isLoading, isError, data } = useQuery(
    &#91;"page-data"],
    () =&gt; getPageById(pageId),
    {
      initialData: pageData
    }
  );

  return (
    &lt;div&gt;
      &lt;main className="p-12"&gt;
        &lt;h1 className="text-48"&gt;{data?.title}&lt;/h1&gt;
        &lt;p className="text-24"&gt;{data?.body}&lt;/p&gt;
      &lt;/main&gt;
    &lt;/div&gt;
  );
}</code></pre>
<!-- /wp:code -->

<!-- wp:heading -->
<h2>5. Enjoy insane performance</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>That's it! You can do <code>npm run start</code> and see your site working.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To improve user experience, it's worth thinking about how you want to use each technology. After all, using React Query for all data is usually overkill.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For page data that might not change often, perhaps just a few times per day or less, then ISR is probably enough. I tend to use ISR mainly for content coming from a CMS - it doesn't need to be live to the minute, it just needs to appear sometime soon after the client changes content.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For data that is constantly updating - news feeds, weather results, any dynamic content - React Query is going to make sure that your page is always updated for every user, regardless of what time they access the site. You could even go a step further and take advantage of HttpOnly cookies with JWT authentication and use React Query for personalised user data, loading into a static 'frame' that is populated on load.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I'm sure I've only scratched the surface of these technologies: There are so many potential applications for this and so much performance to be squeezed out. I hope to see more developers and organisations adopting stale-while-revalidate approaches in future.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// index.js

export const getPageById = async (id) =&gt; {
  const res = await axios
    .get(
      "https://jsonplaceholder.typicode.com/posts/" +
        (Math.floor(Math.random() * 100) + 1)
    )
    .catch((err) =&gt; err);

  return res.data;
};</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->