← Back to React, page 7

react-0126 · Medium mid

What are Core Web Vitals (LCP, CLS, INP), and what do you do in a React app to improve each one?

LCP measures how quickly the largest visible content appears (target 2.5 s or less), CLS measures unexpected layout movement (0.1 or less), and INP measures responsiveness to interactions (200 ms or less). To improve LCP, render critical content on the server, prioritize the hero image and reduce JavaScript. For CLS, reserve space with image dimensions and skeletons. For INP, avoid long tasks, defer non-urgent updates and keep renders small. Measure with field data first.

Full answer

Core Web Vitals are Google's user-experience metrics, measured on real visits (field data) and in lab tools such as Lighthouse.

Largest Contentful Paint (LCP): time until the biggest element in the viewport (often a hero image or heading) is rendered. Good is 2.5 seconds or less. In React apps the usual problems are an empty HTML shell, large JavaScript bundles delaying rendering, data waterfalls and unoptimized images. Fixes: server rendering or static generation so the HTML contains the content; code splitting and smaller bundles; start critical requests early; compress and size images, serve modern formats, set fetchPriority="high" on the LCP image and never lazy-load it; preload fonts.

Cumulative Layout Shift (CLS): how much visible content moves unexpectedly. Good is 0.1 or less. Causes: images without dimensions, content inserted above existing content (banners, late-loaded ads), web fonts swapping, and fallbacks (skeletons or spinners) with different sizes than the content. Fixes: set width and height or aspect-ratio, reserve space for dynamic content, use skeletons that match the final size, and avoid inserting items above the fold after load.

Interaction to Next Paint (INP): how long the page takes to respond visually after interactions throughout the visit. Good is 200 ms or less. In React this is about main-thread work: expensive renders, long handlers, hydration of large pages. Fixes: keep state local, memoize or use the Compiler where profiling shows waste, startTransition for heavy updates, split long tasks, virtualize long lists, lazy-load rarely used code, and use Web Workers for heavy computation. (See the INP debugging item.)

Process: collect real user data with the web-vitals library or your analytics, find the worst pages, reproduce with Lighthouse and the Performance panel under throttling, fix, and track by release.

Thresholds can change over time, so check the current definitions on web.dev.

The example shows an image and a placeholder that avoid shifts and prioritize the LCP element.

jsx
import { renderToStaticMarkup } from 'react-dom/server';

function Hero({ src }) {
  // The LCP image: explicit size, high priority, never lazy.
  return <img src={src} alt="Students in a class" width="1200" height="600" fetchPriority="high" />;
}

function Thumbnail({ src, title }) {
  // Below the fold: lazy, with reserved space.
  return <img src={src} alt={title} width="300" height="200" loading="lazy" decoding="async" />;
}

function CommentsPlaceholder() {
  // A skeleton with the same height as the final content prevents layout shift.
  return <div style={{ minHeight: 240 }} aria-busy="true" />;
}

console.log(
  renderToStaticMarkup(
    <main>
      <Hero src="/hero.webp" />
      <Thumbnail src="/course.webp" title="Course cover" />
      <CommentsPlaceholder />
    </main>
  )
);

This ran as a script and prints the markup. It shows the attributes but not the metrics. React also emitted a preload link for the high-priority image on its own. Real measurement needs a browser and real users. Output of the run: <link rel="preload" as="image" href="/hero.webp" fetchPriority="high"/><main><img src="/hero.webp" alt="Students in a class" width="1200" height="600" fetchPriority="high"/><img src="/course.webp" alt="Course cover" width="300" height="200" loading="lazy" decoding="async"/><div style="min-height:240px" aria-busy="true"></div></main>

Key points

  • LCP is loading speed, CLS is visual stability, and INP is responsiveness.
  • LCP: put content in the first HTML, prioritize the hero image, and reduce bundle size.
  • CLS: reserve space with dimensions and same-size skeletons.
  • INP: reduce main-thread work with transitions, local state, splitting and virtualization.
  • Measure with real-user data first, then verify fixes with lab tools.

Common mistakes

  • Lazy-loading the hero image that is the LCP element.
  • Leaving out image width and height or an aspect ratio.
  • Showing a skeleton that is smaller than the content.
  • Optimizing from lab scores without field data.
  • Shipping a large bundle for a mostly static page.

Follow-ups

  • How do you collect Core Web Vitals from real users?
  • Why can a spinner cause layout shift?
  • How do server rendering and hydration affect INP?

Tips

  • Give a number for each metric, and say numbers are checked against current guidance.
  • Offer one React-specific fix for each metric.
  • Say you measure with field data before optimizing.