Early accessCodeGecko is opening to a first group of teams.Join the waitlist

Core Web Vitals in Next.js: the usual causes and how to fix them

Next.js is fast by default, and its defaults are easy to undo. Here are the usual reasons a Next.js site fails LCP, INP and CLS, and the Next.js way to fix each one.

Nour9 min read ·
Next.js

Fast by default, slow by accident.

Next.js gives you a head start on speed. Pages can be rendered on the server, images are resized for you, fonts are hosted with your site, and code is split by route. So it’s a surprise when Search Console says your Next.js site is failing Core Web Vitals.

It happens because those defaults are easy to undo without noticing. One component marked “use client” pulls a library into every page, one call to cookies() turns a fast static page into a slow dynamic one, one chat widget loads before everything else. Here are the usual causes for each metric, and the Next.js way to fix them.

The short answer

Most Next.js sites that fail do so for a handful of reasons. A slow LCP usually means the main image isn’t marked as a priority, or the page is rendered on every request when it could be cached. A slow INP usually means too much JavaScript runs in the browser while the page hydrates. A high CLS usually means images without a size, a font swap, or a loading state that’s a different height from the content that replaces it. Fix those, then check the result on real visits at the 75th percentile, phones first.

Next.js makes the fast path easy. It doesn’t stop you from leaving it.

Measure it first

Before changing code, find out which metric fails, on which pages, and on which devices. PageSpeed Insights shows Google’s real-visit numbers for one page at a time, and Search Console groups the failing pages. Those numbers cover the last 28 days.

For your own numbers, Next.js has a built-in hook, useReportWebVitals from next/web-vitals. Put it in a small client component in your root layout, and it calls you back with LCP, INP, CLS, FCP and TTFB for each visit, ready to send to your analytics. Two Next.js details to keep in mind when you read the numbers:

  • LCP only comes from the first page load. When someone clicks a Link and the app swaps the page in place, there’s no new LCP. Google judges LCP on the page people land on.
  • INP and CLS keep counting after that. A slow click or a layout shift on the second or third page of a visit still counts toward the page where the visit started.

Then test with dev mode off. next dev skips most optimizations and adds extra checks, so its numbers are much worse than production. Run next build and next start, or test a preview deploy.

LCP: the main image

On most pages the largest element is an image: a hero, a product photo, a cover. The Image component from next/image lazy-loads images by default, which is right for images further down and wrong for the main one. A lazy main image starts downloading late, and LCP waits for it.

  1. 01Mark the main image as a priority. Set priority on the Image above the fold (newer Next.js versions call it preload). Next.js then loads it early, at high priority, and adds a preload hint to the page. Only do it for one or two images per page.
  2. 02Set sizes on responsive images. An image with fill or a width that changes with the screen needs a sizes value, like sizes="(max-width: 760px) 100vw, 50vw". Without it, the browser can pick a file much bigger than the phone needs.
  3. 03Don’t hide the main content behind client code. If the hero only shows after a client component fetches data or an animation finishes, LCP waits for the JavaScript. Render it on the server and fetch its data there.
  4. 04Check background images. A hero set as a CSS background isn’t found until the CSS is loaded and read, so it starts late. An Image with fill and object-fit: cover is found right away.

Our full guide, How to improve LCP, breaks LCP into its four parts so you can tell which one is slow.

LCP: a slow server answer

Nothing can show before the server answers, so a slow answer (a high TTFB) pushes LCP back by the same amount. In Next.js, the biggest factor is whether a page is built ahead of time or rendered on every request.

  • Know which of your pages are dynamic. Reading cookies() or headers(), using searchParams, or fetching without caching makes a route render on every request. The output of next build marks each route as static or dynamic.
  • Cache what doesn’t change per visitor. A blog post, a product page or a docs page rarely needs to be built per request. Render it ahead of time and refresh it every few minutes or when the content changes.
  • Stream the slow parts. If a page needs one slow piece, like recommendations, wrap it in Suspense. The rest of the page arrives straight away and the slow piece follows.
  • Keep the server close to the data. A function in Europe calling a database in the US pays the trip on every query. Run your functions in the same region as your database.

INP: too much client code

INP measures how long the page takes to respond to a click, tap or key press. In Next.js, slow INP nearly always comes from JavaScript running in the browser: the framework hydrating the page, big client components re-rendering, or scripts you added.

  1. 01Push “use client” down the tree. Every file marked “use client”, and everything it imports, ships to the browser. Keep pages and layouts as Server Components and mark only the small interactive parts, like a button or a menu, as client components.
  2. 02Load heavy components later. Charts, editors, maps and video players can be loaded with next/dynamic when they’re needed, so they don’t compete with the first clicks.
  3. 03Mark slow updates as not urgent. Wrap updates that can wait in startTransition, or use useDeferredValue for a filtered list. React then keeps typing and clicks responsive while the slow part renders.
  4. 04Split the page with Suspense. Suspense boundaries let React hydrate parts of the page separately, and it hydrates the part someone clicks first. One big tree has to hydrate all at once.
  5. 05Check what’s in your bundle. A date library, an icon set imported whole or a big package used for one function can add hundreds of kilobytes. A bundle analyzer shows what each route ships.

More on how INP works and what makes it slow in What is a good INP score?

CLS: images, fonts and loading states

Next.js prevents some layout shift for you, but not all of it. The usual causes:

  • Images without a size. next/image asks for width and height, or fill inside a box with a set size, and that keeps the space. A plain img tag, or an image from your CMS rendered as HTML, doesn’t.
  • Fonts loaded the old way. next/font hosts fonts with your site and adjusts the fallback font to take the same space, so the swap doesn’t move text. A font loaded with a link tag or a CSS import doesn’t get that.
  • A loading state of the wrong size. A loading.tsx file or a Suspense fallback shows while the page streams in. If the skeleton is shorter than the real content, everything below jumps when it arrives. Make skeletons the same height as what they stand in for.
  • Content that depends on the browser. A banner that only appears after checking localStorage, or a different layout after reading the window width, renders one way on the server and another after hydration. Keep space for it from the start, or decide on the server.

Every other cause, and how to find the element that moves, is in How to fix CLS.

Third-party scripts

Analytics, tag managers, chat widgets and A/B testing tools run on the same main thread as your app. Next.js has a Script component from next/script that controls when they load:

  • afterInteractive is the default: the script loads after the page becomes interactive. Fine for most analytics.
  • lazyOnload waits until the browser is idle. Use it for chat widgets, feedback tools and anything nobody needs in the first seconds.
  • beforeInteractive loads before your own code. Use it only when the page truly can’t work without the script, because it delays everything else.

Scripts added through a tag manager skip all of this, since the tag manager decides when they run. It’s worth reviewing what’s in there once in a while. We measured what these scripts usually cost in How much do third-party scripts slow down your site?

Check every release

Next.js teams tend to deploy often, sometimes several times a day. That’s great for shipping and hard on speed: any one of those deploys can bring a bigger bundle or turn a static route dynamic, and Google’s 28-day numbers won’t tell you which one.

The fix is to measure each release against the one before it, on real visits, page by page. Our guide to Core Web Vitals monitoring shows how to set that up yourself.

That’s what CodeGecko does. Connect GitHub and add one script tag, and each production deploy is registered with the commits that shipped. CodeGecko collects LCP, INP and CLS from real visits, counts app-style page changes as their own visits, and tells you after each release whether any page got faster or slower on phones or desktops. It works on any host, Vercel included. The docs have every rule, and the demo shows a release that made a page slower.

  • Measure with dev mode off, on real visits, phones first.
  • Mark the main image as a priority and set sizes on responsive images.
  • Cache pages that don’t change per visitor, and stream the slow parts.
  • Keep “use client” small and load heavy components later.
  • Use next/font, sized images and skeletons that match the content.
  • Load third-party scripts with lazyOnload where you can.
  • Compare each release with the one before it.

Common questions

Is Next.js good for Core Web Vitals? Yes. Server rendering, image handling and font hosting all help. But the defaults are easy to undo, so a Next.js site still needs checking.

Why is my Next.js site slow in development? Dev mode compiles pages on demand and skips optimizations. Always measure a production build.

Does the App Router help Core Web Vitals? It can. Server Components send less JavaScript to the browser, which helps INP, and streaming helps the page start sooner. Both only help if most of your code stays on the server.

Do page changes inside the app count for Core Web Vitals? They don’t get their own LCP, but slow clicks and layout shifts during them count toward the page where the visit started.

Does useReportWebVitals send data anywhere? No. It only gives you the values. You decide where to send them.

NourBuilding CodeGecko

Senior frontend engineer, night-shift gamer, weekend photographer, and the human who taught a gecko to watch your deploys.

Get in early.

We’re letting teams in a few at a time. Join the waitlist and we’ll email you once, when your spot opens.

Go deeper

Keep reading