Next.js Performance Optimization
Optimize Core Web Vitals (LCP, INP, CLS), reduce bundle sizes, and stream components in Next.js applications.
Overview
Next.js offers architectural performance advantages through React Server Components, automated asset optimization, and streaming. Optimizing for Google's Core Web Vitals—Largest Contentful Paint (LCP), Interaction to Next Paint (INP), and Cumulative Layout Shift (CLS)—ensures lightning-fast user experiences.
1. Dynamic Imports & Lazy Loading
Use next/dynamic to defer loading heavy Client Components until they are needed:
'use client';
import dynamic from 'next/dynamic';
// Lazy load heavy markdown editor or chart component
const HeavyEditor = dynamic(() => import('@/components/heavy-editor'), {
loading: () => <div className="p-4 animate-pulse">Loading Editor...</div>,
ssr: false, // Disable SSR if library relies on browser APIs
});
export function EditorWrapper() {
return <HeavyEditor />;
}2. Streaming with Suspense
Instead of blocking the entire page render on slow database queries, stream component fragments with <Suspense>:
// app/dashboard/page.tsx
import { Suspense } from 'react';
import { RecentOrders, OrdersSkeleton } from '@/components/orders';
import { AnalyticsChart, ChartSkeleton } from '@/components/chart';
export default function Dashboard() {
return (
<main className="grid grid-cols-1 md:grid-cols-2 gap-6 p-6">
<Suspense fallback={<OrdersSkeleton />}>
<RecentOrders />
</Suspense>
<Suspense fallback={<ChartSkeleton />}>
<AnalyticsChart />
</Suspense>
</main>
);
}3. Parallel Data Fetching
Avoid data fetching waterfalls by running independent asynchronous requests in parallel with Promise.all:
// ❌ Bad: Sequential Waterfall
const user = await fetchUser();
const posts = await fetchPosts();
// ✅ Good: Parallel Execution
const [user, posts] = await Promise.all([
fetchUser(),
fetchPosts(),
]);4. Bundle Analyzer
Install @next/bundle-analyzer to inspect the size of client-side JavaScript packages:
pnpm add -D @next/bundle-analyzer// next.config.ts
import type { NextConfig } from 'next';
import withBundleAnalyzer from '@next/bundle-analyzer';
const nextConfig: NextConfig = {
// configuration
};
export default withBundleAnalyzer({
enabled: process.env.ANALYZE === 'true',
})(nextConfig);Run bundle analysis:
ANALYZE=true pnpm buildPerformance Summary Checklist
- LCP: Mark primary hero image with
priorityattribute innext/image. - CLS: Always use
next/fontand specify explicit aspect ratios for images and videos. - INP: Minimize heavy client JavaScript execution on user interactions; offload work to Server Actions or Web Workers.
- Data Flow: Push data fetching down to the specific Server Components that consume it.