REACT & NEXT.JS / 9. PERFORMANCE & PRODUCTION
Performance & Production
Image optimization, fonts, bundle analysis — shipping fast apps
EXPLANATION
Next.js has several built-in optimizations but you need to use them correctly. next/image: • Automatic WebP/AVIF conversion • Lazy loading by default • Prevents layout shift (requires width/height or fill) • Resizes on the server — never serve a 4000px image on mobile next/font: • Downloads fonts at build time (no external network request) • Automatically applies font-display: swap • Eliminates layout shift from font loading next/link: • Prefetches pages when link enters viewport • Client-side navigation (no full page reload) Bundle optimization: • Dynamic imports → code splitting. Don't load heavy libraries until needed • Server Components → zero client JS for static content • Analyze with: ANALYZE=true bun run build Performance checklist: • Images: use next/image, always specify sizes • Fonts: use next/font, subset to needed characters • Data: fetch in Server Components where possible • Code: dynamic import heavy client libraries • Cache: revalidate strategies for dynamic data
DIAGRAM
Without next/image: Client → requests 4MB PNG → browser downloads full size → layout shift while loading With next/image: Client → requests image with screen width hint → Next.js resizes/converts to WebP on server → serves optimized version → placeholder prevents layout shift Bundle splitting: Without dynamic import: page.js → [all code including heavy chart library] With dynamic import: page.js → [core code only] chart.js → [loaded only when chart renders]
CODE