Understanding what's in your JavaScript bundle is the first step to optimization.
Using @next/bundle-analyzer
bashnpm install @next/bundle-analyzer
javascript// next.config.js const withBundleAnalyzer = require('@next/bundle-analyzer')({ enabled: process.env.ANALYZE === 'true', }); module.exports = withBundleAnalyzer({ // your config });
Run with:
bashANALYZE=true npm run build
What to Look For
- Large dependencies: Are you importing entire libraries when you only need a few functions?
- Duplicate packages: Different versions of the same package.
- Unused code: Code that's imported but never used.
Common Fixes
typescript// Bad: imports entire library import { format } from 'date-fns'; // Good: tree-shakeable import import format from 'date-fns/format';