Next.js Optimization Expert
1. Overview
This skill focuses on maximizing the performance and discoverability of Next.js applications. It covers the correct implementation of the Metadata API for SEO, image and font optimization using next/image and next/font, and strategies for reducing client-side bundle size (e.g., lazy loading, package analysis).
2. Prerequisites & Context
- •Required Tools:
- •
run_command: To build the project and run analysis tools (e.g.,@next/bundle-analyzer). - •
view_file: To inspect code for unoptimized patterns.
- •
- •Environment: Next.js App Router (Production build preferred for analysis).
- •Input:
- •Specific performance bottleneck (if known).
- •Target URL or route to optimize.
3. Workflow
- •Analyze Metadata: Check
layout.tsxandpage.tsxfor propermetadataexports (title, description, Open Graph). - •Audit Assets: Scan for
<img>tags that should be<Image />and standard font imports that should usenext/font. - •Review Client Boundaries: Identify Client Components (
'use client') that are unnecessarily large or could be split. - •Implement Optimizations: Apply fixes (convert tags, add lazy loading, improve metadata).
- •Build & Verify: Run a build to ensure no errors and check bundle size reports.
4. Detailed Instructions & Rules
Critical Rules
- • Rule 1: Always use the Metadata API (static or
generateMetadata) for SEO. Never manually add<meta>tags in the head unless unavoidable. - • Rule 2: Always use
next/imagefor images. It handles lazy loading, sizing, and format conversion automatically. - • Rule 3: Always use
next/font(Google Fonts or local) to prevent Layout Shift (CLS) and optimize loading. - • Rule 4: Minimize
"use client". Move it down the tree as far as possible (Leaf Component pattern). - • Rule 5: Use
dynamic()imports for heavy client components that are not critical for the initial paint.
Optimization Checklist
- • Title & Description on every page.
- • Open Graph images and tags.
- •
next/imagewithsizesprop. - •
next/fontvariable fonts. - •
generateStaticParamsfor dynamic routes (SSG).