Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to analyze Next Js bundle size and content

I've just upgrated to Next JS 9.0 and when I run a build there's a fantastic new feature which shows you the size of all the compiled pages. They're all around 20-30k except for pages which use Formik which tend to be at least double that size. The main problem is that the app.js file is over 600k and red.

Is there a way to dive further and see on a more granular level what's making up all my bundles?

like image 303
jonhobbs Avatar asked Oct 16 '19 09:10

jonhobbs


2 Answers

You can use below package to analyze main bundle:

https://github.com/vercel/next.js/tree/canary/packages/next-bundle-analyzer

like image 149
Parham Zare Avatar answered Oct 25 '22 02:10

Parham Zare


  1. Install @next/bundle-analyzer and cross-env as dev dependency:

    yarn add -D @next/bundle-analyzer cross-env
    
  2. Create a next.config.js file in the root of your project directory (next to package.json) and paste the code below:

const withBundleAnalyzer = require('@next/bundle-analyzer')({
  enabled: process.env.ANALYZE === 'true',
})

module.exports = withBundleAnalyzer({
  reactStrictMode: true,
})
  1. Go to your package.json file and add the line below to the scripts section:

    "analyze": "cross-env ANALYZE=true next build"
    

Now you can run yarn analyze or npm run analyze to analyze your bundle size.

like image 32
user9408899 Avatar answered Oct 25 '22 02:10

user9408899