First Load JS shared by all is rather heavy in next.js

I would suggest installing @next/bundle-analyzer to get a better idea of what dependencies you’re importing and which ones are contributing to that file size. This can help in identifying any unused or unnecessary libraries that could potentially be removed.

You can also look into using code splitting to reduce the bundle for the initial load of the application. This can be achieve by lazy loading code using dynamic import() and/or next/dynamic.

Furthermore, Next.js also mentions in their documentation other tools you can use to understand how much a dependency can add to your bundle.

(…) you can use the following tools to understand what is included inside each JavaScript bundle:

  • Import Cost – Display the size of the imported package inside VSCode.
  • Package Phobia – Find the cost of adding a new dev dependency to your project.
  • Bundle Phobia – Analyze how much a dependency can increase bundle sizes.
  • Webpack Bundle Analyzer – Visualize size of webpack output files with an interactive, zoomable treemap.
  • bundlejs – An online tool to quickly bundle & minify your projects, while viewing the compressed gzip/brotli bundle size, all running locally on your browser.

Next.js, Going to Production, Reducing JavaScript Size

Leave a Comment