Optimization
Priority: P1 (HIGH)
Core optimization primitives provided by Next.js. Monitor First, Optimize Later.
Monitoring (Core Web Vitals)
Before applying optimizations, identify bottlenecks using:
- LCP (Largest Contentful Paint): Initial load speed. Target < 2.5s.
- CLS (Cumulative Layout Shift): Visual stability. Target < 0.1.
- INP (Interaction to Next Paint): Responsiveness. Target < 200ms.
- Tools: Chrome DevTools "Performance" tab,
next/speed-insights, orReact Profiler.
Implementation Guidelines
Images: Always use
next/imageto prevent CLS (Cumulative Layout Shift). Setpriorityfor above-the-fold images to improve LCP (Largest Contentful Paint). Usesizes(e.g.,(max-width: 768px) 100vw, 33vw) andplaceholder="blur"for better UX.Fonts: use
next/font(Google or Local) to optimize for Zero Layout Shift. This automatically host fonts locally and addsfont-display: swap.Scripts: Use
next/scriptwith appropriate strategies:beforeInteractive(critical),afterInteractive(default/analytics), orlazyOnload(lower priority/social widgets/chat).Metadata: Define
static metadataor usegenerateMetadata(async) for dynamic routes to improve SEO and social sharing. This replaces the legacyHeadcomponent.Bundle: Analyze bundle size with
@next/bundle-analyzer. Prune heavy libraries; use ESM-tree-shakable dependencies.Components: Use
dynamicimports (Next.js version ofReact.lazy) withSuspensefor large components that are not needed during initial render.Next.js 15+ Integration: Enable
ppr: true(Partial Prerendering) innext.config.jsto combine static shell with dynamic islands.Strategy: Self-host Google Fonts or local files via
next/font.Optimization: Zero layout shift, no network requests for font files at runtime. Apply classes to
<body>or specific elements.
Metadata (SEO)
Static: Export
metadataobject fromlayout.tsxorpage.tsx.export const metadata: Metadata = { title: 'Dashboard', description: '...', };Dynamic: Export
generateMetadata({ params })function.export async function generateMetadata({ params }) { const product = await getProduct(params.id); return { title: product.name }; }Open Graph: Use
openGraphkey for social cards.
Scripts (next/script)
- Loading Strategy: Control when 3rd party scripts load.
strategy="afterInteractive"(Default): Google Analytics.strategy="lazyOnload": Chat widgets, low priority.
Anti-Patterns
- No
<img>tag: Usenext/imageto prevent CLS and enable automatic optimization. - No Google Fonts CDN link: Use
next/fontto self-host and eliminate layout shift. - No metadata in
_document.tsx: Useexport const metadataorgenerateMetadata(). - No 3rd-party scripts in
<head>: Usenext/scriptwith appropriatestrategy.