Skip to content
Styling & OptimizationChapter 30 / 34Advanced5 min read

Performance & Core Web Vitals

Measure and improve Next.js performance and Core Web Vitals (LCP, INP, CLS) with useReportWebVitals and the bundle analyzer, with practical examples.

On this page

1. What are performance and Core Web Vitals?

MetricWhat it measuresGood score
LCP (Largest Contentful Paint)How long the largest element takes to appearUnder 2.5 seconds
INP (Interaction to Next Paint)How quickly the screen updates after a click or typingUnder 200 ms
CLS (Cumulative Layout Shift)How much the content moves while loadingUnder 0.1

2. Why does it matter?

Users leave slow sites, and page experience is also a signal in Google's ranking. Next.js optimizes a lot by default, but wrong choices can cancel out that benefit.

Optimizing by guessworkOptimizing by measuring
It feels like the site is slowReal users' LCP, INP and CLS data
Everything is a Client ComponentServer Components, less JavaScript
A big library in the whole bundleCatch heavy packages with the analyzer
Images and fonts not optimizednext/image and next/font

3. Where is it used?

ProblemEffectNext.js tool
A large hero imageBad LCPnext/image and preload
Text moving on a font swapBad CLSnext/font
Heavy client JavaScriptBad INPServer Components, next/dynamic
Slow data fetchBad LCPStreaming, Suspense, "use cache"
Third-party scriptsBad INP and LCPnext/script with strategy

4. How does it work?

The loop for improving performance
Collect Web Vitals from real users
Find the worst metric and page
Find the cause with the bundle analyzer or DevTools
Apply a fix (image, font, client JS, caching)
Measure again on a production build

Development mode is slow and its numbers are not real. Always measure after next build and next start, or on production.

5. Code example

Real user metrics: useReportWebVitals

tsx
"use client";

import { useReportWebVitals } from "next/web-vitals";

type ReportCallback = Parameters<typeof useReportWebVitals>[0];

const sendToAnalytics: ReportCallback = (metric) => {
  const body = JSON.stringify({ name: metric.name, value: metric.value, rating: metric.rating });
  navigator.sendBeacon("/api/vitals", body);
};

export function WebVitals() {
  useReportWebVitals(sendToAnalytics);
  return null;
}

Render this component as WebVitals inside the body of the root layout.

Lazy loading a heavy component

tsx
"use client";

import dynamic from "next/dynamic";

const Chart = dynamic(() => import("./chart"), {
  loading: () => <p>Loading chart...</p>,
});

export function Dashboard() {
  return <Chart />;
}

Analyzing the bundle

bash
npx next analyze

The built-in Turbopack analyzer (Next.js 16.1+) shows modules by route and by client or server. The webpack @next/bundle-analyzer plugin also still works:

ts
import type { NextConfig } from "next";
import bundleAnalyzer from "@next/bundle-analyzer";

const withBundleAnalyzer = bundleAnalyzer({ enabled: process.env.ANALYZE === "true" });

const nextConfig: NextConfig = {};

export default withBundleAnalyzer(nextConfig);
bash
ANALYZE=true npx next build --webpack

How does the code work?

ItemMeaning
useReportWebVitalsRuns the callback when each metric (LCP, INP, CLS, FCP, TTFB) is ready
Callback defined outsideThe function reference stays stable, so no duplicate reports
sendBeaconSends the data even while the page is closing
dynamic()The chart code goes in a separate chunk and loads when needed
next analyzeAn interactive treemap of the Turbopack bundle
--webpack@next/bundle-analyzer is a webpack plugin, so you need a webpack build

6. Common mistakes

Mistake: Measuring performance in next dev

Why? Development mode has extra checks and unoptimized code, so the numbers are much worse than production.

Better: Run next build and next start, then check Lighthouse or real user data.

Mistake: Making the whole page use client

Why? The whole component tree and its libraries go into the browser bundle, which hurts both INP and load time.

Better: Add use client only on interactive leaf components and keep the rest as Server Components.

Mistake: Trusting only the Lighthouse score

Why? Lighthouse is a lab test on one machine, real users have slow phones and different networks, and INP is not even measured in the lab.

Better: Also collect field data with useReportWebVitals or Vercel Speed Insights.

Mistake: Doing heavy data transforms on the client

Why? Libraries for markdown parsing or syntax highlighting make the client bundle large even though the output is just HTML.

Better: Do this kind of work in a Server Component and send only the rendered markup to the client.

Interview tip

"Why did INP replace FID?" FID measured only the delay of the first interaction. INP measures something close to the slowest interaction across the whole visit, so it gives a better picture of real responsiveness.

7. What to learn before and after

Final quick revision

PointDetail
LCPLoading, under 2.5s
INPResponsiveness, under 200ms
CLSStability, under 0.1
MeasureuseReportWebVitals from next/web-vitals
Bundle checknext analyze or @next/bundle-analyzer with --webpack
Less JSServer Components and next/dynamic