|
| 1 | +--- |
| 2 | +title: Optimize bundle size and implement code splitting |
| 3 | +labels: performance, enhancement, optimization |
| 4 | +--- |
| 5 | + |
| 6 | +## Description |
| 7 | + |
| 8 | +Analyze and optimize bundle size using next/bundle-analyzer to improve initial load time and Core Web Vitals scores. |
| 9 | + |
| 10 | +## Background |
| 11 | + |
| 12 | +Currently, no bundle analysis is configured, and all components load upfront. The MiniGame component, in particular, is loaded even when not used, increasing the initial bundle size unnecessarily. |
| 13 | + |
| 14 | +## Requirements |
| 15 | + |
| 16 | +### 1. Bundle Analysis Setup |
| 17 | + |
| 18 | +Install and configure `@next/bundle-analyzer`: |
| 19 | +```bash |
| 20 | +npm install --save-dev @next/bundle-analyzer |
| 21 | +``` |
| 22 | + |
| 23 | +Configure in `next.config.mjs`: |
| 24 | +```javascript |
| 25 | +import bundleAnalyzer from '@next/bundle-analyzer'; |
| 26 | + |
| 27 | +const withBundleAnalyzer = bundleAnalyzer({ |
| 28 | + enabled: process.env.ANALYZE === 'true', |
| 29 | +}); |
| 30 | + |
| 31 | +export default withBundleAnalyzer({ |
| 32 | + // ... existing config |
| 33 | +}); |
| 34 | +``` |
| 35 | + |
| 36 | +Add script to `package.json`: |
| 37 | +```json |
| 38 | +"analyze": "ANALYZE=true npm run build" |
| 39 | +``` |
| 40 | + |
| 41 | +### 2. Dynamic Imports |
| 42 | + |
| 43 | +Implement dynamic imports for components that are: |
| 44 | +- Not needed on initial render |
| 45 | +- Behind user interaction |
| 46 | +- Heavy/large components |
| 47 | + |
| 48 | +**Priority: MiniGame component** |
| 49 | +```tsx |
| 50 | +const MiniGame = dynamic(() => import('../components/MiniGame').then(mod => ({ default: mod.MiniGame })), { |
| 51 | + loading: () => <div>Loading game...</div>, |
| 52 | + ssr: false |
| 53 | +}); |
| 54 | +``` |
| 55 | + |
| 56 | +### 3. Image Optimization |
| 57 | + |
| 58 | +Ensure all images are properly optimized: |
| 59 | +- Use `next/image` consistently (already done for profile images) |
| 60 | +- Optimize SVG files in `/public` |
| 61 | +- Consider using WebP format where appropriate |
| 62 | + |
| 63 | +### 4. Dependency Audit |
| 64 | + |
| 65 | +Review and optimize dependencies: |
| 66 | +- Check for unused dependencies |
| 67 | +- Look for lighter alternatives to heavy libraries |
| 68 | +- Ensure proper tree-shaking |
| 69 | + |
| 70 | +### 5. Route-Based Code Splitting |
| 71 | + |
| 72 | +Verify Next.js automatic code splitting is working: |
| 73 | +- Check each route has its own bundle |
| 74 | +- Verify shared chunks are optimized |
| 75 | + |
| 76 | +## Target Metrics |
| 77 | + |
| 78 | +- **Initial bundle size**: < 200KB (gzipped) |
| 79 | +- **First Contentful Paint (FCP)**: < 1.8s |
| 80 | +- **Largest Contentful Paint (LCP)**: < 2.5s |
| 81 | +- **Time to Interactive (TTI)**: < 3.8s |
| 82 | + |
| 83 | +## Acceptance Criteria |
| 84 | + |
| 85 | +- [ ] Bundle analyzer installed and configured |
| 86 | +- [ ] Analysis script added to package.json |
| 87 | +- [ ] MiniGame component dynamically imported |
| 88 | +- [ ] Bundle analysis report generated and reviewed |
| 89 | +- [ ] Identified and removed unused dependencies (if any) |
| 90 | +- [ ] Documentation added explaining bundle optimization strategy |
| 91 | +- [ ] Performance improvements measured and documented |
| 92 | +- [ ] Core Web Vitals scores improved |
| 93 | + |
| 94 | +## Implementation Steps |
| 95 | + |
| 96 | +1. Install and configure bundle analyzer |
| 97 | +2. Run initial analysis to establish baseline |
| 98 | +3. Implement dynamic import for MiniGame |
| 99 | +4. Re-run analysis to measure improvement |
| 100 | +5. Identify other optimization opportunities |
| 101 | +6. Document findings and results |
| 102 | + |
| 103 | +## Related Files |
| 104 | + |
| 105 | +- `next.config.mjs` - Next.js configuration |
| 106 | +- `package.json` - Dependencies and scripts |
| 107 | +- `app/page.tsx` - Main page (imports MiniGame) |
| 108 | +- `components/MiniGame.tsx` - Component to be dynamically loaded |
| 109 | + |
| 110 | +## Resources |
| 111 | + |
| 112 | +- [Next.js Bundle Analyzer](https://www.npmjs.com/package/@next/bundle-analyzer) |
| 113 | +- [Next.js Dynamic Imports](https://nextjs.org/docs/advanced-features/dynamic-import) |
| 114 | +- [Web Vitals](https://web.dev/vitals/) |
0 commit comments