I use NextJS to generate a list of static pages.
All pages need one big static Javascript object which needs some minutes to initialize (it contains the result of parsing many other files).
const bigData = initializeBigData(); // <- called for every page, instead once
export async function getStaticProps({ params }) {
return { bigData[params.slug] }; // ...
}
Unfortunately I cannot find a way to initialize this data object once. Instead the slow initializeBigData() runs every time when a page was created which makes the build very slow.
I've tried the following things:
npm run build --threads=1All these things change nothing, the long initializeBigData() is called for every single page when building the site.
If possible, I don't want to use a custom server.
How can an object get initialized only once in the build process of a NextJS project?
There is a relevant discussion here https://github.com/vercel/next.js/discussions/15054
But I would just use:
if (!global.font) {
global.font = fs.readFileSync("fonts/Somefont.ttf");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With