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.
npm install web-vitalsVerified import paths — ran on the pinned version, not inferred.
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.
Replace all calls to `onFID()` with `onINP()`. The `onINP()` function takes the same `reportCallback` signature.
Migrate from `getXXX()` functions to their `onXXX()` counterparts (e.g., `getCLS` to `onCLS`). Ensure your analytics reporting handles multiple metric updates, especially for CLS and INP which can change over time.
Review your target browser support matrix. If you need to support very old or niche browsers, you might need to stick to an earlier `web-vitals` version or implement custom polyfills for certain `PerformanceObserver` APIs.
Update your analytics processing or UI code to reflect the new property names in the attribution data if you are consuming detailed diagnostic information. Refer to the `upgrading-to-v4.md` and `upgrading-to-v5.md` guides in the GitHub repository for a complete list of changes.
Implement a robust analytics reporting strategy. It's recommended to send the final metric value and any delta upon the `pagehide` event or when the page's visibility state changes to hidden, using `navigator.sendBeacon` for reliability. The library examples demonstrate this pattern.
Ensure `web-vitals` is only imported and executed in a browser context. If using a server-side rendering (SSR) framework, conditionally import or execute the `web-vitals` code only on the client side (e.g., inside `useEffect` in React, or within `window.onload` checks).
Update your code to use `onINP` instead of `onFID`. You may also need to upgrade your `web-vitals` package to a version that supports `onINP` if you are on a very old version (though `onINP` has been present for a while).
Implement a `sendToAnalytics` function (as shown in the quickstart example) and pass it to each `onXXX` function. Ensure this function correctly formats and dispatches the data to your analytics provider (e.g., Google Analytics, custom API) and that the endpoint is reachable and correctly configured to receive the data. Consider using `navigator.sendBeacon` for improved reliability.
No dependency data recorded yet.