Web Vitals Measurement Library

5.2.0 · active · verified Sun Apr 19

The `web-vitals` library provides a lightweight, modular way to measure essential performance metrics, known as Web Vitals, directly in the browser. It focuses on the current Core Web Vitals (Largest Contentful Paint (LCP), Interaction to Next Paint (INP), and Cumulative Layout Shift (CLS)), as well as First Contentful Paint (FCP) and Time to First Byte (TTFB). Maintained by the Google Chrome team, this library ensures that the collected data accurately reflects how these metrics are measured by Chrome and reported in official Google tools like the Chrome User Experience Report. It is widely used for Real User Monitoring (RUM) to collect and send performance data to analytics endpoints. The library is actively developed, with its current stable version being 5.2.0, and releases generally align with updates to Web Vitals definitions and Chrome's underlying measurement methodologies.

Common errors

Warnings

Install

Imports

Quickstart

This code demonstrates how to import and use `web-vitals` to measure all standard metrics (CLS, FCP, INP, LCP, TTFB) and send them to a hypothetical analytics endpoint using `navigator.sendBeacon` or `fetch` with `keepalive` for robust reporting.

import { onCLS, onFCP, onINP, onLCP, onTTFB, type Metric } from 'web-vitals';

interface AnalyticsData {
  name: string;
  value: number;
  id: string;
  navigationType: string;
  delta?: number;
  entries?: PerformanceEntry[];
}

function sendToAnalytics(metric: Metric) {
  const analyticsEndpoint = process.env.ANALYTICS_URL ?? '/api/analytics';
  const data: AnalyticsData = {
    name: metric.name,
    value: metric.value,
    id: metric.id,
    navigationType: metric.navigationType,
    delta: metric.delta,
    entries: metric.entries // Include entries for detailed analysis if needed
  };

  // Use sendBeacon for reliable reporting on page unload
  (navigator.sendBeacon && navigator.sendBeacon(analyticsEndpoint, JSON.stringify(data))) ||
  fetch(analyticsEndpoint, {
    body: JSON.stringify(data),
    method: 'POST',
    credentials: 'omit',
    keepalive: true, // Ensures request completes even if page unloads
    headers: {
      'Content-Type': 'application/json'
    }
  }).catch(error => console.error('Failed to send analytics:', error));

  console.log(`Metric Reported: ${metric.name} - Value: ${metric.value} (ID: ${metric.id})`);
}

// Measure all Core Web Vitals and other key metrics, sending them to analytics
onCLS(sendToAnalytics);
onFCP(sendToAnalytics);
onINP(sendToAnalytics);
onLCP(sendToAnalytics);
onTTFB(sendToAnalytics);

console.log('Web Vitals measurement initialized.');

view raw JSON →