To measure and improve Core Web Vitals (LCP, INP, CLS) for better user experience and SEO rankings.
- When users complain about slowness.
- Preparing for a production launch.
- Continuous monitoring.
- Lighthouse: Lab data. Run in Chrome DevTools.
- Web Vitals Extension: Real-time checking.
- Code:
import { onLCP, onINP, onCLS } from 'web-vitals'; onLCP(console.log);
Goal: < 2.5s.
- Images: Use WebP/AVIF. Explicit
width/height. - Preload:
<link rel="preload" as="image" href="hero.jpg">. - Server: Improve TTFB (Time to First Byte) via caching/CDN.
Goal: < 200ms. Replaces FID.
- Main Thread: Break up long tasks using
setTimeoutorrequestIdleCallback. - React: Use
useTransitionfor non-urgent updates. - Events: Avoid heavy logic in event handlers.
Goal: < 0.1.
- Space: Always reserve space for images/ads/embeds (CSS
aspect-ratio). - Fonts: Use
font-display: swapto avoid FOIT (Flash of Invisible Text).
- Third-Party Scripts: Analytics/Ads often hurt performance. Defer them (
<script defer>) or load via Web Workers (Partytown). - Network: Test on "Fast 3G" in DevTools, not just Wi-Fi.
A faster, more stable website that passes Google's Core Web Vitals assessment.