title: NextJS: Initialize an object once
body: <p>I use NextJS to generate a list of static pages.</p>
  <p>All pages need one big static Javascript object which needs some minutes to initialize (it contains the result of parsing many other files).</p>
  <pre class="lang-js prettyprint-override"><code>
  const bigData = initializeBigData();   // &lt;- called for every page, instead once
  
  export async function getStaticProps({ params }) {
    return { bigData[params.slug] };  // ...
  }
  </code></pre>
  <p>Unfortunately I cannot find a way to initialize this data object once. Instead the slow <code>initializeBigData()</code> runs every time when a page was created which makes the build very slow.</p>
  <p>I've tried the following things:</p>
  <ul>
  <li>Use a module as singleton</li>
  <li>Use an ES6 class singleton</li>
  <li>Use the npm package <a href="https://github.com/isaacs/once#readme" rel="noreferrer">once</a></li>
  <li>Reduce the build threads to 1: <code>npm run build --threads=1</code></li>
  </ul>
  <p>All these things change nothing, the long <code>initializeBigData()</code> is called for every single page when building the site.</p>
  <p>If possible, I don't want to use a custom server.</p>
  <p>How can an object get initialized only once in the build process of a NextJS project?</p>