title: NextJS: Is there a way to invoke something only on serverside, only on a fresh page load?
body: <p>Our team initially created an <code>_app.tsx</code> with <code>getInitialProps</code>, and included something along the lines of</p>
  <pre><code>if (!req) return {};
  </code></pre>
  <p>as the top line. The idea was to make <code>getInitialProps</code> run only on the server side, only on initial page load. We thought it was a safe assumption as the usual advice (often repeated in stackoverflow answers) is that <code>getInitialProps</code> runs client-side on page transitions.</p>
  <p>We later discovered that <code>getInitialProps</code> actually runs server-side on page transitions, if you are transitioning to a page that has <code>getServerSideProps</code>.</p>
  <p>So that doesn't serve our purposes - we have several network queries that are made in <code>getInitialProps</code> that are being made on most of our page transitions (since most of our pages have <code>getServerSideProps</code>), when our intention was to only run those queries on a fresh page (app) load.</p>
  <p>We could instead use <code>useEffect</code> only on component mount, but we want to run those queries serverside instead of client side, to protect against visual flashes.</p>
  <p>I'm learning how to set up a context, with a setState inside, but initial testing shows that its initial setState runs on both clientside and serverside:</p>
  <pre><code>...
  
  const getString = (): string =&gt; {
    console.log('about to set string!');
    return 'string';
  };
  
  const PagePropsProvider: React.FC&lt;IProps&gt; = ({ pageProps, children }) =&gt; {
    const [sample, setSample] = useState&lt;string&gt;(getString());
  
    return &lt;PagePropsContext.Provider value={pageProps}&gt;{children}&lt;/PagePropsContext.Provider&gt;;
  };
  ...
  
  </code></pre>
  <p>What I'd like, for something like an auth context, is for <code>_app.tsx</code>'s <code>getInitialProps</code> to request the initial user object server side on page load, supply it to the context from the props, and not have the app re-request the initial user object unnecessarily on every page transition. I could do that if I could prevent <code>getInitialProps</code> from running on page transitions. Is there a way to prevent <code>getInitialProps</code> from running on those page transitions? Or a more idiomatic way to do what we're aiming for? (Ideally we'd prefer to avoid <code>getInitialProps</code> entirely, but the same issue remains with <code>getServerSideProps</code>, how to make it invoke only on a fresh page load.)</p>