This document outlines the automated bundle size analysis and optimization protocols implemented in the Stellarflow Frontend project.
The project now includes:
- Automated bundle analysis using
@next/bundle-analyzer - Strict size thresholds that can block distribution builds
- GitHub Actions integration for CI/CD pipeline checks
- Bundle size reporting with violation detection
Bundle size thresholds are configured in .bundle-limits.json:
{
"maxMainBundle": 250, // Main chunk max size (gzipped, KB)
"maxPageBundle": 100, // Per-page chunk max size (gzipped, KB)
"maxTotalGzipped": 500, // Total all chunks (gzipped, KB)
"maxIndividualGzipped": 150 // Individual bundle hard limit (gzipped, KB)
}Adjust thresholds based on your project requirements:
- Smaller values = stricter optimization requirements
- Run baseline builds first to establish realistic limits
- Update thresholds as the project grows, but avoid exceeding them
npm run buildBuilds the project and analyzes bundle sizes. Violations are reported but do not block the build.
npm run build:strictBuilds the project and fails the build if any thresholds are exceeded. Use this in production deployment pipelines.
ANALYZE=true npm run buildGenerates interactive HTML bundle visualizations in .next/analyze/ directory. Opens in browser to explore what's bloating bundles.
After any build, view the detailed JSON report:
cat .bundle-report.json============================================================
📦 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!
The project includes .github/workflows/bundle-check.yml which:
✅ Runs on every push to main and develop
✅ Runs on all pull requests
✅ Fails the build if bundle sizes exceed limits
✅ Posts bundle report as PR comment
✅ Generates summary in GitHub Actions
PR Comment Example:
📦 Bundle Size Report
| Bundle | Size (KB) | Gzipped (KB) |
|--------|-----------|--------------|
| ✅ main-abc.js | 180.45 | 45.2 |
| ✅ pages-def.js | 92.30 | 28.1 |
Total (gzipped): 75.5KB
Limit: 500KB
✅ All bundles within limits!
Run interactive analysis periodically to identify bloat:
ANALYZE=true npm run buildMonitor .bundle-report.json over time to catch gradual bloat:
# Store baseline
cp .bundle-report.json .bundle-report.baseline.json
# Compare after changes
npm run build
# Check if totalGzipped increased significantlyEnsure pages are code-split properly:
// ✅ Good - dynamic imports for large features
import dynamic from 'next/dynamic';
const HeavyComponent = dynamic(() => import('./HeavyComponent'), {
loading: () => <div>Loading...</div>
});
// ❌ Avoid - forces everything into main bundle
import HeavyComponent from './HeavyComponent';Verify unused code is eliminated:
// ✅ Good - import specific exports
import { Button } from 'react-icons/fa';
// ❌ Avoid - imports entire library
import * as Icons from 'react-icons/fa';Review large dependencies in bundle analysis. Consider:
- Lighter alternatives (e.g.,
date-fnsvsmoment) - Lazy-loading heavy libraries
- Tree-shakeable imports
The project includes these optimizations in next.config.ts:
swcMinify: true // SWC-based minification (faster)
compress: true // Enable compression
productionBrowserSourceMaps: false // Reduce build size
optimizeFonts: true // Optimize font loading
optimizePackageImports: [] // Tree-shake specific packagesCheck what's causing the bloat:
ANALYZE=true npm run buildReview interactive visualization:
- Opens in browser automatically
- Shows which packages consume the most space
- Identify unused/duplicate dependencies
Increase thresholds only if necessary:
Edit .bundle-limits.json:
{
"maxTotalGzipped": 600 // Increased from 500
}Better solution - reduce bundle:
- Remove unused dependencies
- Implement code splitting
- Use lighter alternatives
- Enable proper tree-shaking
Set Node memory limit:
NODE_OPTIONS="--max-old-space-size=4096" npm run buildVerify Next.js cache is cleared:
rm -rf .next
npm run buildWhen you encounter bundle size violations:
- Document the violation: Note which bundle exceeds limits
- Analyze the cause: Use
ANALYZE=true npm run build - Create an issue: Link bundle report and analysis results
- Propose fix: Code splitting, removing dependencies, etc.
- Update limits: Only if the increase is justified and necessary