title: Next.js: Reduce data fetching and share data between pages
body: <p>I'm looking for solutions for better data fetching in a Next.js app. In this question I'm not just looking for <em>a</em> solution, I'm looking for multiple options so we can look at the pros and cons.</p>
  
  <h1>The problem I have</h1>
  
  <p>Right now I have a few pages that all include a  component that displays som static content and a  that have some dynamic content that is fetched from an API. Each page do a <code>fetch()</code> in their <code>getInitialProps()</code> to get their own page data, but also the footer data, <strong>which is the same for all pages</strong>.</p>
  
  <p>This of course works, but there is a lot of duplicated data fetching. The footer data will always be displayed for all pages and always be the same. It will also rarely be changed in the API, so no need for revalidate the data.</p>
  
  <h1>The answers I'm looking for</h1>
  
  <p>I'm not just looking to solve this one problem, I'm looking for an overview to learn some new practice for future projects as well. I like writing "obvious" code, so not looking for too hacky solutions, like writing to the <code>window</code> object etc. Simple solutions with less dependancies are preferred. The goal is a fast site. It's not that important to reduce network usage/API calls.</p>
  
  <h1>What I have thought so far</h1>
  
  <p>This is the possible solutions I've come up with, somewhat sorted from simple/obvious to more complex. </p>
  
  <ol>
  <li>Do a fetch inside the Footer component (client side)</li>
  <li>Do a fetch in getInitialProps (server side &amp; client side) on all /pages</li>
  <li>Do a fetch in _app.js with a HOC and hooking into it's <code>getInitialProps()</code> and add it to props, so data is available for all pages</li>
  <li>Use zeit/swr and data prefetching to cache data</li>
  <li>Use redux to store a global state</li>
  </ol>
  
  <p>All of these "work", but most of them will refetch the data unnecessarily, and/or adds a bit more complexity. Here are the pros/cons as I see it (numbers are the same as above):</p>
  
  <ol>
  <li> Simple! Fetch code is only in one place, it's located where it's used.  Data is fetched after page is loaded, so the content "jumps" in to view. Data is refetched all the time.</li>
  <li> Simple! Data is fetched on the server, so content is available before the page is rendered.  Data is refetched for each page. We have to remember to fetch the same footer data for each page in their <code>getInitialProps()</code>.</li>
  <li> We can do the fetch in one place and add it to all the pages props, so footer data is automatically available for all pages' props.  Might be a bit more complex for some to easily understand what's going on, as it requires a bit more understanding of how Next.js/React works. Still refetches the data for all pages. We now do two <code>fetch()</code> calls after each other (first in _app.js to load footer content, then in each page to get custom content), so it's even slower. </li>
  <li> Somewhat simple. We can use the prefetching to load data to cache even before the JS is loaded. After first page load, we will have fast data fetching. Can have fetch code directly in footer component.  The <code>rel="preload"</code> prefetching technique won't work with all types of fetching (for instance Sanity's client using groq). To not have "jumpy" content where the data is loaded after initial page load, we should provide <code>useSWR()</code> with <code>initialData</code> which still will require us to fetch data in <code>getInitialProps()</code>, but it would be enough to just do this on the server side. Could use the new <code>getServerSideProps()</code>. </li>
  <li> We can load data once(?) and have it available throughout the application. Fast and less/no refetching.  Adds external dependency. More complex as you'll have to learn redux, even to just load one shared data object. </li>
  </ol>
  
  <p>Current solution, using the solution described in bullet point number 2.</p>
  
  <pre><code>const HomePage = (props) =&gt; {
    return (
      &lt;Layout data={props.footer}&gt;
         &lt;Home data={props.page} /&gt;
      &lt;/Layout&gt;
    )
  }
  
  // Not actual query, just sample
  const query = `{
    "page": *[_type == "page"][0], 
    "footer": *[_type == "footer"][0]
  }`
  
  HomePage.getInitialProps = async () =&gt; {
    const data = await client.fetch(query)
  
    return {
      page: data.page
      footer: data.footer
    }
  }
  
  export default HomePage
  </code></pre>
  
  <hr>
  
  <p>Would love some more insight into this. I'm a missing something obvious?</p>