The web-vitals Library
What you'll learn: How to measure Core Web Vitals in production React apps using Google's official web-vitals package.
You've learned what LCP, FID, and CLS are — but how do you actually measure them in your real-world React app? The web-vitals library is Google's official JavaScript package that makes capturing these metrics simple and production-ready.
What web-vitals Does
Think of web-vitals as a dashboard gauge for your car's performance — except for your website. It observes browser APIs in the background, waits for meaningful moments (like when the largest element paints or when the user first interacts), and calls your callback function with the measured value. You don't need to understand the complex browser APIs yourself.
Installing and Basic Usage
npm install web-vitals
In your React app's entry point (like index.js or main.jsx):
import { onCLS, onFID, onLCP } from 'web-vitals';
function sendToAnalytics(metric) {
// Send metric.name, metric.value, metric.id to your analytics service
console.log(metric);
}
onLCP(sendToAnalytics);
onFID(sendToAnalytics);
onCLS(sendToAnalytics);
That's it. Each function automatically starts observing and will call your callback with the metric data when available. The callback receives an object with name, value, id, and other metadata.
Why This Matters
Without web-vitals, you'd need to manually wire up Performance Observer APIs, handle edge cases, and ensure cross-browser compatibility. This library gives you production-grade CWV measurement with three lines of code.
Key Takeaway: The web-vitals library provides instant, reliable Core Web Vitals measurement in React apps — just import the metric functions, pass them a callback, and start collecting real user performance data.