Lesson 6 of 10
Web Vitals & Core Web Vitals
Core Web Vitals (LCP, INP, CLS) are Google's metrics for page experience that affect SEO ranking. The web-vitals library lets you measure these in real user sessions and report to analytics - giving production performance data rather than lab measurements.
Measuring & Reporting Web Vitals
import { onCLS, onINP, onLCP, onFCP, onTTFB } from 'web-vitals';
// Report to Google Analytics 4
function reportToAnalytics({ name, value, rating, id }: MetricReport) {
// rating: 'good' | 'needs-improvement' | 'poor'
gtag('event', name, {
value: Math.round(name === 'CLS' ? value * 1000 : value),
metric_id: id,
metric_value: value,
metric_rating: rating,
non_interaction: true,
});
}
// Register all vitals - call once in your app entry point
onLCP(reportToAnalytics); // Largest Contentful Paint: largest image/text visible
onINP(reportToAnalytics); // Interaction to Next Paint: how fast UI responds to interaction
onCLS(reportToAnalytics); // Cumulative Layout Shift: how much layout shifts during load
onFCP(reportToAnalytics); // First Contentful Paint: when first content appears
onTTFB(reportToAnalytics); // Time to First Byte: server response time
// GOOD thresholds:
// LCP < 2.5s (time for main content to load)
// INP < 200ms (time from user interaction to visible response)
// CLS < 0.1 (layout stability score, 0 = no shift, 1 = very unstable)
// Common LCP problems and fixes:
// Slow: Large unoptimized hero image → Use WebP, add fetchpriority="high"
// Slow: LCP element is lazy loaded → Remove loading="lazy" from above-fold images
// Slow: Render-blocking CSS/JS → Defer non-critical resources
// Common CLS problems and fixes:
// Shifting: Images without dimensions → Always set width + height or aspect-ratio
// Shifting: Ads or embeds load without reserved space → Add min-height to containers
// Shifting: Web fonts cause flash → Use font-display: optional or size-adjust
// Common INP problems and fixes:
// Slow: Heavy synchronous event handlers → Move work to Web Workers
// Slow: Long tasks blocking main thread → Use startTransition, split work with scheduler
// Slow: Excessive DOM updates → Memoize components, use virtualizationTip
Tip
Practice Web Vitals Core Web Vitals in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and flow for Web Vitals & Core Web Vitals
Practice Task
Note
Practice Task - (1) Write a working example of Web Vitals Core Web Vitals from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Web Vitals Core Web Vitals is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.
Finished this lesson?