Skillshub nextjs-optimization
Image, Font, Script, and Metadata optimization strategies. Use when optimizing Next.js images, fonts, scripts, or page metadata for performance. (triggers: **/layout.tsx, **/page.tsx, next/image, next/font, metadata, generateMetadata)
git clone https://github.com/ComeOnOliver/skillshub
T=$(mktemp -d) && git clone --depth=1 https://github.com/ComeOnOliver/skillshub "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/HoangNguyen0403/agent-skills-standard/nextjs-optimization" ~/.claude/skills/comeonoliver-skillshub-nextjs-optimization && rm -rf "$T"
skills/HoangNguyen0403/agent-skills-standard/nextjs-optimization/SKILL.mdOptimization
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,
, ornext/speed-insights
.React Profiler
Implementation Guidelines
-
Images: Always use
to prevent CLS (Cumulative Layout Shift). Setnext/image
for above-the-fold images to improve LCP (Largest Contentful Paint). Usepriority
(e.g.,sizes
) and(max-width: 768px) 100vw, 33vw
for better UX.placeholder="blur" -
Fonts: use
(Google or Local) to optimize for Zero Layout Shift. This automatically host fonts locally and addsnext/font
.font-display: swap -
Scripts: Use
with appropriate strategies:next/script
(critical),beforeInteractive
(default/analytics), orafterInteractive
(lower priority/social widgets/chat).lazyOnload -
Metadata: Define
or usestatic metadata
(async) for dynamic routes to improve SEO and social sharing. This replaces the legacygenerateMetadata
component.Head -
Bundle: Analyze bundle size with
. Prune heavy libraries; use ESM-tree-shakable dependencies.@next/bundle-analyzer -
Components: Use
imports (Next.js version ofdynamic
) withReact.lazy
for large components that are not needed during initial render.Suspense -
Next.js 15+ Integration: Enable
(Partial Prerendering) inppr: true
to combine static shell with dynamic islands.next.config.js -
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
or specific elements.<body>
Metadata (SEO)
-
Static: Export
object frommetadata
orlayout.tsx
.page.tsxexport const metadata: Metadata = { title: 'Dashboard', description: '...', }; -
Dynamic: Export
function.generateMetadata({ params })export async function generateMetadata({ params }) { const product = await getProduct(params.id); return { title: product.name }; } -
Open Graph: Use
key for social cards.openGraph
Scripts (next/script
)
next/script- Loading Strategy: Control when 3rd party scripts load.
(Default): Google Analytics.strategy="afterInteractive"
: Chat widgets, low priority.strategy="lazyOnload"
Anti-Patterns
- No
tag: Use<img>
to prevent CLS and enable automatic optimization.next/image - No Google Fonts CDN link: Use
to self-host and eliminate layout shift.next/font - No metadata in
: Use_document.tsx
orexport const metadata
.generateMetadata() - No 3rd-party scripts in
: Use<head>
with appropriatenext/script
.strategy