Concept Explanation
Google uses a set of metrics called Core Web Vitals to decide if your website is healthy. They exist because "is it fast?" is not one question. A page can paint in half a second and still feel broken if the button ignores your click, or if the text slides out from under your thumb as an ad loads.
So the three vitals measure three different kinds of pain:
| Metric | Question it answers | Good | Needs work |
|---|---|---|---|
| LCP (Largest Contentful Paint) | Did the main content appear? | under 2.5s | over 4.0s |
| INP (Interaction to Next Paint) | Does it respond when I tap? | under 200ms | over 500ms |
| CLS (Cumulative Layout Shift) | Does it stay still? | under 0.1 | over 0.25 |
INP replaced FID in March 2024. FID only measured the delay before the first interaction started processing, which flattered pages that were quick to accept a click and then froze for a second. INP measures the full latency of the worst interaction across the whole visit, from tap to the next painted frame. It is a much harder metric to game, and a lot of sites that scored green on FID score amber on INP.
One more distinction that trips people up. Lab data comes from Lighthouse on your machine: repeatable, useful for debugging, and not what Google ranks on. Field data comes from real Chrome users and is what appears in the Chrome User Experience Report and Search Console. Optimise the lab number, verify the field number.
Syntax or Core Structure
Measuring the real thing
import { onLCP, onINP, onCLS } from 'web-vitals';
function report({ name, value, rating, id }) {
navigator.sendBeacon('/analytics', JSON.stringify({ name, value, rating, id }));
}
onLCP(report);
onINP(report);
onCLS(report);
Reserving space to kill layout shift
<!-- width and height let the browser reserve the box before the bytes arrive -->
<img src="/hero.jpg" width="1200" height="630" alt="" fetchpriority="high" />
<!-- and for the fold-below images -->
<img src="/thumb.jpg" width="200" height="200" loading="lazy" alt="" />
Breaking up a long task to rescue INP
async function processAll(items) {
for (const item of items) {
doWork(item);
// hand the main thread back so a pending click can be painted
await new Promise((resolve) => setTimeout(resolve, 0));
}
}
Fixing Each One
LCP is almost always a loading problem, not a JavaScript problem. Find the LCP element in Lighthouse, then: serve it from a CDN, preload it, set fetchpriority="high", use modern formats, and stop lazy-loading it. Lazy-loading the hero image is the single most common self-inflicted LCP wound.
INP is a main-thread problem. It is caused by long tasks: heavy event handlers, giant re-renders, third-party scripts. Yield often, defer non-urgent work, and move real computation to a Web Worker.
CLS is a layout problem. Give every image and embed explicit dimensions, reserve space for ads and banners, use font-display: optional or size-adjust to avoid text reflow, and never insert content above what the user is already reading.
Optimization Patterns
- Debouncing: If a user types 100 letters in 1 second, don't run your search function 100 times. Wait until they stop typing (
debounce). - Throttling: If a user scrolls, only run your "Sticky Navbar" code every 100ms instead of every 1ms.
Both are covered in depth in the next lesson.
Real World Example
The 300ms that came from a cookie banner. A retail site had a good LCP in the lab and a poor one in the field. The culprit was a consent script loaded synchronously in the head: it blocked rendering while it fetched its own config, so the hero image did not even start downloading until it finished. Moving the banner to load asynchronously, and pre-reserving its height so it did not shove the page down, fixed LCP and CLS in one change. No application code was touched.
Common Mistakes
- Optimising the lab score only. A perfect Lighthouse run on a fast laptop tells you nothing about a mid-range Android on 4G. Throttle CPU 4x and network to "Slow 4G".
- Lazy-loading above-the-fold images. It delays the exact element LCP is measuring.
- Chasing the aggregate. Vitals are reported at the 75th percentile, so the median user being fine does not help. Look at the tail.
- Ignoring CLS after load. The metric keeps accumulating for the whole visit, including shifts caused by infinite scroll.
- Treating INP as FID. A page can accept the first click instantly and still block the next twenty.
- Adding a third-party tag without measuring it. Most performance regressions arrive through a tag manager, not a pull request.
Best Practices
- Measure in the field with the
web-vitalslibrary and send results to your own endpoint; do not rely on Lighthouse alone. - Set a performance budget in CI so a regression fails the build rather than surprising you in Search Console a month later.
- Give every image, video and iframe explicit dimensions. It is the highest-value CLS fix there is.
- Ship less JavaScript. Code-split by route, drop polyfills for browsers you do not support, and audit your dependencies by weight.
- Preload the LCP resource and the fonts it needs; preconnect to origins you will definitely hit.
Practice Exercises
- The Field Reporter: Install the
web-vitalspackage on any project and log LCP, INP and CLS to the console on a real page load. Compare them with what Lighthouse reports. - The Layout Shifter: Create a page that has a "Jump" effect (e.g., an image that loads and pushes text down). Use Google Lighthouse to see your CLS score, then fix it by adding
widthandheight. - The Long Task: Write a click handler that blocks for 600ms, measure the INP hit, then split the work with yields and measure again.