331: Next.js + Apollo + Server Side Rendering (SSR)

CodePen Radio

Episode | Podcast

Date: Wed, 01 Sep 2021 19:49:56 +0000

<p>Our goal here was to explore server-side rendering (SSR) in <a href="https://nextjs.org/" rel="noreferrer noopener" target="_blank">Next.js</a> using data from <a href="https://www.apollographql.com/" rel="noreferrer noopener" target="_blank">Apollo GraphQL</a>, for faster client-rendering and SEO benefits.</p> <p>There are a variety of approaches, but Shaw has set his sights on a very developer-ergonomic version here where you can leave queries on individual components and mark them as SSR-or-not.</p> <p>There are two "official" approaches:</p> <ol><li><a href="https://www.apollographql.com/blog/apollo-client/next-js/next-js-getting-started/#:~:text=Using%20Apollo%20Client%20for%20server%20side%20rendered%20page%20data">Apollo's documentation</a></li><li><a href="https://github.com/vercel/next.js/tree/master/examples/api-routes-apollo-server-and-client">Next.js' example</a></li></ol> <p>These are sorta-kinda-OK, except... </p> <ul><li>They have to be configured per-page</li><li>They are mostly limited to queries at the top page level</li><li>You'd likely need to duplicate queries with slightly differently handling from client to server</li><li>May or may not populate the client cache so that the client can have live queries without having to query for the same data. For example, ay you have data that you want to change on the client side (pagination, fetching new results). If it's fetched and rendered server-side, you have to fetch again client side or send over the cache from the server so the queries on the client-side can be ready to update with new data.</li></ul> <p>These limitations are workable in some situations, but we want to avoid duplicating code and also have server-side rendered queries that aren't top-level on the page.</p> <p>A probably-better approach is to use <a href="https://www.apollographql.com/docs/react/performance/server-side-rendering/#executing-queries-with-getdatafromtree">the getDataFromTree method</a>, which walks down the tree and executes the queries to fill up the ApolloClient cache. We got this from <a href="https://gist.github.com/Tylerian/16d48e5850b407ba9e3654e17d334c1e" rel="noreferrer noopener" target="_blank">a two-year old Gist from Tylerian</a><em> </em>showing a way to integrate getDataFromTree into Next.js. Tylerian's gist had some extra complications that might've been due to older Next.js limitations, but the overall process was sound:</p> <ol><li>Create a shared ApolloClient instance</li><li>Render the page using getDataFromTree() to fill the cache with data</li><li>Render the page again with that data using Next's Document.getInitialProps()</li><li>Extract the cache to deliver with the page and hydrate the client-side Apollo cache</li></ol> <p>The benefits:</p> <ul><li>Quick to set up</li><li>No duplicate queries</li><li>Nothing special per page to handle server-side rendering or client-side queries.</li><li>Cache hydration to keep client active without re-querying data</li><li>Easy to enable / disable server-side rendering for individual queries</li></ul> <p><a href="https://github.com/shshaw/next-apollo-ssr">Here's a repo with Shaw's findings.</a></p>