This guide explains how to set up and use the automated bundle size optimization protocols.
The @next/bundle-analyzer package has already been added to package.json. Install it:
npm installConfirm the bundle analyzer is installed:
npm ls @next/bundle-analyzernpm run buildThis will:
- Build the Next.js project
- Analyze all generated bundles
- Check against size limits in
.bundle-limits.json - Generate a detailed report in
.bundle-report.json - Display results in the terminal
ANALYZE=true npm run buildThis generates interactive HTML visualizations showing:
- Which packages take up the most space
- Unused/duplicate dependencies
- Opportunities for optimization
The visualization opens automatically in your browser at .next/analyze/
npm run build:strictUse this in production CI/CD pipelines. The build will fail if any bundle exceeds size limits.
Edit .bundle-limits.json to change thresholds:
{
"maxMainBundle": 250, // Main JS chunk limit (gzipped, KB)
"maxPageBundle": 100, // Per-page chunk limit (gzipped, KB)
"maxTotalGzipped": 500, // Total all bundles (gzipped, KB)
"maxIndividualGzipped": 150 // Hard limit per bundle (gzipped, KB)
}Guidelines:
- Keep limits strict during development to catch bloat early
- 250KB main bundle is a good baseline for SPA/Next.js projects
- 500KB total is a reasonable overall limit
- Adjust only after measuring actual project needs
To track bundle size changes over time:
npm run build
cp .bundle-report.json .bundle-report.baseline.jsonThen after making changes:
npm run build
# Compare totalGzipped in the new report vs baseline
cat .bundle-report.json | jq '.totalGzipped'
cat .bundle-report.baseline.json | jq '.totalGzipped'The repository includes .github/workflows/bundle-check.yml which automatically:
✅ Runs on every push to main and develop branches
✅ Runs on all pull requests
✅ Blocks merges if bundle sizes exceed limits
✅ Posts bundle report as a PR comment
✅ Updates the job summary
No additional setup required! Just push to GitHub and the checks run automatically.
To warn about bundle size changes before committing:
# Install the pre-commit hook
cp scripts/pre-commit-bundle-check.js .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
# Create baseline
npm run build
cp .bundle-report.json .bundle-report.baseline.jsonNow, each commit will analyze bundle changes and warn if the bundle grew significantly.
============================================================
📦 Bundle Size Analysis Report
============================================================
📋 Configuration:
• Max main bundle: 250KB (gzipped)
• Max page bundle: 100KB (gzipped)
• Max individual: 150KB (gzipped)
• Max total: 500KB (gzipped)
📊 Bundle Breakdown:
✅ main-abc123.js
└─ 180.45KB raw | 45.2KB gzipped
✅ pages-def456.js
└─ 92.30KB raw | 28.1KB gzipped
📈 Total (gzipped): 75.5KB
✅ All bundles are within size limits!
What to look for:
⚠️ warnings indicate violations- ✅ means bundle is OK
- Total (gzipped) is what users actually download
- Raw size is before compression
{
"timestamp": "2026-06-01T10:30:00.000Z",
"limits": { ... },
"bundles": [
{
"name": "main-abc123.js",
"size": 180.45,
"gzipped": 45.2,
"exceeds": []
}
],
"totalGzipped": 75.5,
"violations": [],
"passed": true
}Find what's bloating it:
ANALYZE=true npm run buildThen open .next/analyze/ in your browser to see the breakdown.
Common causes:
- Large heavy dependencies
- Missing code splitting
- Duplicate/unused code
- Non-tree-shakeable imports
Clear the cache and rebuild:
rm -rf .next node_modules
npm install
npm run buildIncrease Node memory:
NODE_OPTIONS="--max-old-space-size=4096" npm run buildCheck that .github/workflows/bundle-check.yml exists and is properly formatted (YAML).
// Use dynamic imports for page-level components
import dynamic from 'next/dynamic';
const Dashboard = dynamic(() => import('./Dashboard'), {
loading: () => <LoadingSpinner />
});// ✅ Good - only imports what's used
import { Button, Input } from '@ui-library/components';
// ❌ Avoid - imports entire library
import * as UI from '@ui-library/components';# See what's in your bundle
ANALYZE=true npm run buildThen check:
- Are all dependencies actually used?
- Are there lighter alternatives?
- Can heavy libraries be lazy-loaded?
Already configured in next.config.ts:
- SWC minification (faster, smaller)
- Automatic compression
- Font optimization
- Package imports optimization
- Run your first build:
npm run build - Review the report to understand current bundle size
- Run interactive analysis:
ANALYZE=true npm run build - Identify optimization opportunities from the visualization
- Implement code splitting for large features
- Set up baseline:
cp .bundle-report.json .bundle-report.baseline.json - Monitor over time as you add features
For more details, see BUNDLE_OPTIMIZATION.md.