Next.js Core Web Vitals: fixing TTFB, LCP and CLS

Next.js Core Web Vitals fixes from a photo marketplace: ISR for a 1.9 s cold TTFB, the LCP image in server HTML, a 19 MB background and a 0.293 homepage CLS.

By

Most Next.js Core Web Vitals problems come down to three things: the server takes too long to send HTML, the largest image arrives late, and images push the layout around as they load. I spent several weeks this summer on all three for a photo marketplace built on the Next.js App Router. Some fixes were one-line config changes, and one was a 19 MB background image. This post walks through what I measured, what I changed for TTFB, LCP and CLS, and a caching bug that turned a temporary error into a permanent 404.

What are good Core Web Vitals scores?

Google's Web Vitals guide sets the targets: Largest Contentful Paint within 2.5 seconds, Interaction to Next Paint of 200 milliseconds or less, and Cumulative Layout Shift of 0.1 or less. They are measured at the 75th percentile of page loads, for mobile and desktop separately. Time to First Byte is not one of the three, but a slow TTFB delays everything after it, LCP included.

TTFB: serve image pages as static HTML with ISR

Every photo on the site has its own page. Those pages were rendered dynamically on every request with no-store data fetches, and a cold request took about 1.9 s to first byte.

Nothing on those pages changes per visitor, so there was no reason to render them per request. I added generateStaticParams and a one-hour revalidate, so the pages ship as cached static HTML with near-zero TTFB and refresh hourly. New slugs that were not known at build time still render on demand and are cached after the first visit. Simplified:

// app/photos/[slug]/page.tsx
export const revalidate = 3600;

export async function generateStaticParams() {
  const photos = await fetchPublishedPhotos();
  return photos.map((photo) => ({ slug: photo.slug }));
}

An hour is too long to wait after an admin edits a photo's title or archives it. So saving or archiving now calls a server action that purges the cached HTML for the page's new and old slug straight away. In the App Router, revalidatePath is the function for that. The Next.js ISR guide covers both the time-based and the on-demand form.

LCP: put the hero image in the server HTML

On a photo page, the largest element is the photo. The page fetched the photo's details on the server, but the client component that rendered the image fetched them again after hydration. So the LCP image only started loading after hydration plus a client fetch, which is about the slowest path an image can take.

The fix passes the server-fetched detail into the client component as an initial value. The <img> is now in the server-rendered HTML, and the browser can start downloading it as soon as it parses the page. Around that I added:

  • a preload link and fetchpriority="high" for the main image, and a preconnect to the image host;
  • lazy loading for the photographer's avatar, which never needs to compete with the photo;
  • removal of an unused useSearchParams call, which was a client-side rendering bailout waiting to happen.

The 19 MB login background

The auth pages had their own LCP problem. All 6 of them used a login photo straight from public/ as a CSS background: 19 MB at 7990x5334. CSS backgrounds get none of the help next/image gives, so every visitor downloaded the full file.

It is now a 2000px next/image at 301 KB, compressed, served as WebP, preloaded and marked priority. In Next.js 16 that prop is called preload; the Image component reference lists the change.

The homepage artwork got the same treatment, recompressed from 2.0 MB to 522 KB total. I also deleted 23 MB of unreferenced images from public/. Before deleting, I checked each one against the category and blog content coming from the API as well as the code, because an image referenced only from CMS content will not show up in a code search. I covered resizing on upload in image processing pipelines with Sharp and S3.

CLS: reserve the space before the image loads

The homepage CLS was 0.293, almost three times the 0.1 target. Most of it came from one place: the content rows used h-auto images, so each image took up no height until it loaded, then pushed everything below it down. That row alone was a 0.21 shift.

The fix gives every image a box before it loads. Each tile is an aspect-ratio container, and the image inside uses next/image with fill and a sizes hint so the browser picks the right file. Two smaller changes went with it:

  • The logo got an explicit height. A header that changes height on load shifts the whole page, the same problem I wrote about in measuring a fixed header's real height.
  • Grid tiles got fixed aspect ratios that match their loading skeletons. When the skeleton is replaced by the real tile, nothing moves.

One shift came from dimensions that lied. An intro image was declared as 1200x800 while the file was 810x607, a different ratio, so the reserved box was the wrong shape and the layout adjusted when the real file arrived. Declared dimensions are only useful if they match the file.

A fixed hero height that cropped the photo

A related fix was about how much of the image people saw. The hero was a fixed 550px tall with object-cover. On a 1920px screen, that showed only 71% of the photo. The hero height now scales with the viewport, so wide screens see the composition the photographer intended while the box is still sized before the image loads.

The cached 404 that never expired

ISR adds a failure mode worth knowing about. The deploy synced the build to the server with rsync, excluding .next/cache, but nothing ever cleared the server's own copy of that directory.

So when the API failed for a while and an image page called notFound(), that 404 was cached. With the stale cache surviving every deploy, the page stayed a cached 404 permanently, even after the API recovered. The deploy now drops the cache directory before restarting the app.

If you self-host Next.js with ISR, decide on purpose what happens to .next/cache on deploy, and be careful about caching error states. A page that returns notFound() because a dependency is down is different from a page whose photo was deleted.

Measure before and after each change

Every fix above started from a number: 1.9 s TTFB, 0.293 CLS, a 0.21 shifter, 19 MB, 71% of a photo. Lighthouse and PageSpeed Insights point at the right element most of the time, and the Performance panel in Chrome DevTools shows which element shifted and by how much.

My default is to fix one thing, measure again, and keep the before and after numbers in the commit message. That makes it easy to see which change moved which metric, and it stops the "we optimised images" kind of change that nobody can verify later.

Where I'd start

If your Next.js Core Web Vitals report is red, I'd work in this order:

  1. TTFB. Find pages rendered per request that could be static, and add ISR with an on-demand purge for edits.
  2. LCP. Make sure the largest image is in the server HTML, then preload it and give it high fetch priority.
  3. Heavy files. Look for images served raw from public/ or as CSS backgrounds, and move them to next/image.
  4. CLS. Give every image a box with the right aspect ratio before it loads, and check declared dimensions against real files.
  5. Deploys. Decide what happens to .next/cache, so a temporary error cannot become permanent.

Share previews are another place Next.js images go wrong; see why your pages share with no image. Performance work like this is part of the services I offer.

Want this kind of work on your product?

Get in touch