Performance Engineering ⚡
Performance is a multi-course meal—optimize the appetizer (first paint), main dish (interactivity), and dessert (server throughput).
Core Web Vitals 🍽️
- LCP (Largest Contentful Paint): optimize hero images, use
next/imagewith proper sizing, preconnect critical origins. - CLS (Cumulative Layout Shift): reserve space for media (
width/height), load fonts withnext/font. - INP (Interaction to Next Paint): debounce expensive handlers, defer non-critical JS, use Web Workers for heavy tasks.
Bundle Size Control
- Inspect
.next/analyze(usenext build --analyze). - Remove dead code, leverage
modulePathIgnorePatterns.
Code Splitting & Tree Shaking
- Use
dynamic(() => import('./Chart'), { ssr: false })for client-only heavy components. - Ensure packages ship ES modules for tree shaking; avoid glob imports.
Hydration Cost
- Keep server components server-only; minimize client component boundaries.
- Use
use clientsparingly.
Server Rendering Perf 🍲
- Streaming SSR: wrap sections in
Suspenseto stream as data arrives. - Partial Rendering: split large pages into smaller server components; avoid fetching everything at once.
- Avoid Waterfalls: parallelize fetches with
Promise.allor fetch dedupe. - DB Optimization: batch queries, use
.select()to fetch only needed columns, implement caching (Redis) for hot data. - Connection Pooling: use Prisma Data Proxy, pgBouncer, or built-in poolers when on serverless to avoid max connections.
Next.js-specific Tools 🧰
next/scriptstrategies (afterInteractive,lazyOnload). Inline critical scripts withstrategy="beforeInteractive"only if necessary.next/imagefine-tuning: setsizesattr, adjustquality, markpriorityfor hero images.dynamic()+React.lazy: lazily load seldom-used components.- Prefetch control: automatically enabled on
<Link>, but disable viaprefetch={false}if too aggressive. - Instrumentation hooks:
instrumentation.tsfor custom tracing.
Analogy: Performance tuning is running a Michelin kitchen—prep ahead (code splitting), cook efficiently (parallel fetches), and plate beautifully (web vitals) so diners (users) get a delightful experience.
Last updated on