Next.jsMedium
What are the performance metrics Next.js focuses on (Core Web Vitals)?
Next.js targets Google's Core Web Vitals: Largest Contentful Paint (LCP) for loading, Interaction to Next Paint (INP) for responsiveness, and Cumulative Layout Shift (CLS) for visual stability. It also reports supporting metrics like Time to First Byte (TTFB) and First Contentful Paint (FCP), which you can collect with useReportWebVitals.
The three Core Web Vitals (good thresholds)
- LCP ≤ 2.5s: when the largest visible element (hero image, heading) renders.
- INP ≤ 200ms: the latency of user interactions across the visit. It replaced First Input Delay (FID) in March 2024.
- CLS ≤ 0.1: how much visible content shifts unexpectedly.
Measured at the 75th percentile of real users.
How Next.js features map to them
- LCP: prerendered static shells served from a CDN, streaming,
next/imagewithpreloadon the hero image,next/fontpreloading, and route prefetching for instant navigations. - INP: Server Components (less JavaScript to hydrate), selective hydration, code splitting,
next/scriptdeferral, the React Compiler to cut re-renders, and transitions for non-urgent updates. - CLS: image dimensions, metric-matched font fallbacks, and skeleton fallbacks in
loading.tsxand<Suspense>that match the final layout. - TTFB: caching with
"use cache", Partial Prerendering and CDN delivery.
Measuring
- Lab: Lighthouse, Chrome DevTools Performance panel.
- Field:
useReportWebVitalsfromnext/web-vitalsto send metrics to your analytics, Vercel Speed Insights, or the Chrome UX Report. experimental.webVitalsAttributionadds attribution data that helps find the cause of poor scores.
Interview tip
Name the metric, the threshold, and one concrete Next.js technique per metric.
Code example
tsx
1// app/web-vitals.tsx2"use client";34import { useReportWebVitals } from "next/web-vitals";56export function WebVitals() {7 useReportWebVitals((metric) => {8 // metric.name: "LCP" | "INP" | "CLS" | "FCP" | "TTFB"9 const body = JSON.stringify({10 name: metric.name,11 value: metric.value,12 rating: metric.rating, // "good" | "needs-improvement" | "poor"13 id: metric.id,14 page: window.location.pathname,15 });16 // sendBeacon survives page unloads17 if (!navigator.sendBeacon?.("/api/vitals", body)) {18 fetch("/api/vitals", { method: "POST", body, keepalive: true });19 }20 });21 return null;22}2324// app/layout.tsx25// <body><WebVitals />{children}</body>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.