UNPKG

52.7 kBMarkdownView Raw
1# `web-vitals`
2
3- [Overview](#overview)
4- [Install and load the library](#installation)
5 - [From npm](#import-web-vitals-from-npm)
6 - [From a CDN](#load-web-vitals-from-a-cdn)
7- [Usage](#usage)
8 - [Basic usage](#basic-usage)
9 - [Report the value on every change](#report-the-value-on-every-change)
10 - [Report only the delta of changes](#report-only-the-delta-of-changes)
11 - [Send the results to an analytics endpoint](#send-the-results-to-an-analytics-endpoint)
12 - [Send the results to Google Analytics](#send-the-results-to-google-analytics)
13 - [Send the results to Google Tag Manager](#send-the-results-to-google-tag-manager)
14 - [Send attribution data](#send-attribution-data)
15 - [Batch multiple reports together](#batch-multiple-reports-together)
16- [Build options](#build-options)
17 - [Which build is right for you?](#which-build-is-right-for-you)
18 - [How the polyfill works](#how-the-polyfill-works)
19- [API](#api)
20 - [Types](#types)
21 - [Functions](#functions)
22 - [Rating Thresholds](#rating-thresholds)
23 - [Attribution](#attribution)
24- [Browser Support](#browser-support)
25- [Limitations](#limitations)
26- [Development](#development)
27- [Integrations](#integrations)
28- [License](#license)
29
30## Overview
31
32The `web-vitals` library is a tiny (~1.5K, brotli'd), modular library for measuring all the [Web Vitals](https://web.dev/articles/vitals) metrics on real users, in a way that accurately matches how they're measured by Chrome and reported to other Google tools (e.g. [Chrome User Experience Report](https://developers.google.com/web/tools/chrome-user-experience-report), [Page Speed Insights](https://developers.google.com/speed/pagespeed/insights/), [Search Console's Speed Report](https://webmasters.googleblog.com/2019/11/search-console-speed-report.html)).
33
34The library supports all of the [Core Web Vitals](https://web.dev/articles/vitals#core_web_vitals) as well as a number of other metrics that are useful in diagnosing [real-user](https://web.dev/articles/user-centric-performance-metrics) performance issues.
35
36### Core Web Vitals
37
38- [Cumulative Layout Shift (CLS)](https://web.dev/articles/cls)
39- [First Input Delay (FID)](https://web.dev/articles/fid)
40- [Largest Contentful Paint (LCP)](https://web.dev/articles/lcp)
41
42### Other metrics
43
44- [Interaction to next Paint (INP)](https://web.dev/articles/inp)
45- [First Contentful Paint (FCP)](https://web.dev/articles/fcp)
46- [Time to First Byte (TTFB)](https://web.dev/articles/ttfb)
47
48<a name="installation"><a>
49<a name="load-the-library"><a>
50
51## Install and load the library
52
53<a name="import-web-vitals-from-npm"><a>
54
55The `web-vitals` library uses the `buffered` flag for [PerformanceObserver](https://developer.mozilla.org/docs/Web/API/PerformanceObserver/observe), allowing it to access performance entries that occurred before the library was loaded.
56
57This means you do not need to load this library early in order to get accurate performance data. In general, this library should be deferred until after other user-impacting code has loaded.
58
59### From npm
60
61You can install this library from npm by running:
62
63```sh
64npm install web-vitals
65```
66
67_**Note:** If you're not using npm, you can still load `web-vitals` via `<script>` tags from a CDN like [unpkg.com](https://unpkg.com). See the [load `web-vitals` from a CDN](#load-web-vitals-from-a-cdn) usage example below for details._
68
69There are a few different builds of the `web-vitals` library, and how you load the library depends on which build you want to use.
70
71For details on the difference between the builds, see <a href="#which-build-is-right-for-you">which build is right for you</a>.
72
73**1. The "standard" build**
74
75To load the "standard" build, import modules from the `web-vitals` package in your application code (as you would with any npm package and node-based build tool):
76
77```js
78import {onLCP, onFID, onCLS} from 'web-vitals';
79
80onCLS(console.log);
81onFID(console.log);
82onLCP(console.log);
83```
84
85_**Note:** in version 2, these functions were named `getXXX()` rather than `onXXX()`. They've [been renamed](https://github.com/GoogleChrome/web-vitals/pull/222) in version 3 to reduce confusion (see [#217](https://github.com/GoogleChrome/web-vitals/pull/217) for details) and will continue to be available using the `getXXX()` until at least version 4. Users are encouraged to switch to the new names, though, for future compatibility._
86
87<a name="attribution-build"><a>
88
89**2. The "attribution" build**
90
91Measuring the Web Vitals scores for your real users is a great first step toward optimizing the user experience. But if your scores aren't _good_, the next step is to understand why they're not good and work to improve them.
92
93The "attribution" build helps you do that by including additional diagnostic information with each metric to help you identify the root cause of poor performance as well as prioritize the most important things to fix.
94
95The "attribution" build is slightly larger than the "standard" build (by about 600 bytes, brotli'd), so while the code size is still small, it's only recommended if you're actually using these features.
96
97To load the "attribution" build, change any `import` statements that reference `web-vitals` to `web-vitals/attribution`:
98
99```diff
100- import {onLCP, onFID, onCLS} from 'web-vitals';
101+ import {onLCP, onFID, onCLS} from 'web-vitals/attribution';
102```
103
104Usage for each of the imported function is identical to the standard build, but when importing from the attribution build, the [`Metric`](#metric) object will contain an additional [`attribution`](#metricwithattribution) property.
105
106See [Send attribution data](#send-attribution-data) for usage examples, and the [`attribution` reference](#attribution) for details on what values are added for each metric.
107
108<a name="how-to-use-the-polyfill"><a>
109
110**3. The "base+polyfill" build**
111
112_**⚠️ Warning ⚠️** the "base+polyfill" build is deprecated. See [#238](https://github.com/GoogleChrome/web-vitals/issues/238) for details._
113
114Loading the "base+polyfill" build is a two-step process:
115
116First, in your application code, import the "base" build rather than the "standard" build. To do this, change any `import` statements that reference `web-vitals` to `web-vitals/base`:
117
118```diff
119- import {onLCP, onFID, onCLS} from 'web-vitals';
120+ import {onLCP, onFID, onCLS} from 'web-vitals/base';
121```
122
123Then, inline the code from `dist/polyfill.js` into the `<head>` of your pages. This step is important since the "base" build will error if the polyfill code has not been added.
124
125```html
126<!doctype html>
127<html>
128 <head>
129 <script>
130 // Inline code from `dist/polyfill.js` here
131 </script>
132 </head>
133 <body>
134 ...
135 </body>
136</html>
137```
138
139It's important that the code is inlined directly into the HTML. _Do not link to an external script file, as that will negatively affect performance:_
140
141```html
142<!-- GOOD -->
143<script>
144 // Inline code from `dist/polyfill.js` here
145</script>
146
147<!-- BAD! DO NOT DO! -->
148<script src="/path/to/polyfill.js"></script>
149```
150
151Also note that the code _must_ go in the `<head>` of your pages in order to work. See [how the polyfill works](#how-the-polyfill-works) for more details.
152
153_**Tip:** while it's certainly possible to inline the code in `dist/polyfill.js` by copy and pasting it directly into your templates, it's better to automate this process in a build step—otherwise you risk the "base" and the "polyfill" scripts getting out of sync when new versions are released._
154
155<a name="load-web-vitals-from-a-cdn"><a>
156
157### From a CDN
158
159The recommended way to use the `web-vitals` package is to install it from npm and integrate it into your build process. However, if you're not using npm, it's still possible to use `web-vitals` by requesting it from a CDN that serves npm package files.
160
161The following examples show how to load `web-vitals` from [unpkg.com](https://unpkg.com):
162
163_**Important!** The [unpkg.com](https://unpkg.com) CDN is shown here for example purposes only. `unpkg.com` is not affiliated with Google, and there are no guarantees that the URLs shown in these examples will continue to work in the future._
164
165**Load the "standard" build** _(using a module script)_
166
167```html
168<!-- Append the `?module` param to load the module version of `web-vitals` -->
169<script type="module">
170 import {onCLS, onFID, onLCP} from 'https://unpkg.com/web-vitals@3?module';
171
172 onCLS(console.log);
173 onFID(console.log);
174 onLCP(console.log);
175</script>
176```
177
178**Load the "standard" build** _(using a classic script)_
179
180```html
181<script>
182 (function () {
183 var script = document.createElement('script');
184 script.src = 'https://unpkg.com/web-vitals@3/dist/web-vitals.iife.js';
185 script.onload = function () {
186 // When loading `web-vitals` using a classic script, all the public
187 // methods can be found on the `webVitals` global namespace.
188 webVitals.onCLS(console.log);
189 webVitals.onFID(console.log);
190 webVitals.onLCP(console.log);
191 };
192 document.head.appendChild(script);
193 })();
194</script>
195```
196
197**Load the "attribution" build** _(using a module script)_
198
199```html
200<!-- Append the `?module` param to load the module version of `web-vitals` -->
201<script type="module">
202 import {
203 onCLS,
204 onFID,
205 onLCP,
206 } from 'https://unpkg.com/web-vitals@3/dist/web-vitals.attribution.js?module';
207
208 onCLS(console.log);
209 onFID(console.log);
210 onLCP(console.log);
211</script>
212```
213
214**Load the "attribution" build** _(using a classic script)_
215
216```html
217<script>
218 (function () {
219 var script = document.createElement('script');
220 script.src =
221 'https://unpkg.com/web-vitals@3/dist/web-vitals.attribution.iife.js';
222 script.onload = function () {
223 // When loading `web-vitals` using a classic script, all the public
224 // methods can be found on the `webVitals` global namespace.
225 webVitals.onCLS(console.log);
226 webVitals.onFID(console.log);
227 webVitals.onLCP(console.log);
228 };
229 document.head.appendChild(script);
230 })();
231</script>
232```
233
234## Usage
235
236### Basic usage
237
238Each of the Web Vitals metrics is exposed as a single function that takes a `callback` function that will be called any time the metric value is available and ready to be reported.
239
240The following example measures each of the Core Web Vitals metrics and logs the result to the console once its value is ready to report.
241
242_(The examples below import the "standard" build, but they will work with the "attribution" build as well.)_
243
244```js
245import {onCLS, onFID, onLCP} from 'web-vitals';
246
247onCLS(console.log);
248onFID(console.log);
249onLCP(console.log);
250```
251
252Note that some of these metrics will not report until the user has interacted with the page, switched tabs, or the page starts to unload. If you don't see the values logged to the console immediately, try reloading the page (with [preserve log](https://developer.chrome.com/docs/devtools/console/reference/#persist) enabled) or switching tabs and then switching back.
253
254Also, in some cases a metric callback may never be called:
255
256- FID and INP are not reported if the user never interacts with the page.
257- CLS, FCP, FID, and LCP are not reported if the page was loaded in the background.
258
259In other cases, a metric callback may be called more than once:
260
261- CLS and INP should be reported any time the [page's `visibilityState` changes to hidden](https://developer.chrome.com/blog/page-lifecycle-api/#advice-hidden).
262- All metrics are reported again (with the above exceptions) after a page is restored from the [back/forward cache](https://web.dev/articles/bfcache).
263
264_**Warning:** do not call any of the Web Vitals functions (e.g. `onCLS()`, `onFID()`, `onLCP()`) more than once per page load. Each of these functions creates a `PerformanceObserver` instance and registers event listeners for the lifetime of the page. While the overhead of calling these functions once is negligible, calling them repeatedly on the same page may eventually result in a memory leak._
265
266### Report the value on every change
267
268In most cases, you only want the `callback` function to be called when the metric is ready to be reported. However, it is possible to report every change (e.g. each larger layout shift as it happens) by setting `reportAllChanges` to `true` in the optional, [configuration object](#reportopts) (second parameter).
269
270_**Important:** `reportAllChanges` only reports when the **metric changes**, not for each **input to the metric**. For example, a new layout shift that does not increase the CLS metric will not be reported even with `reportAllChanges` set to `true` because the CLS metric has not changed. Similarly, for INP, each interaction is not reported even with `reportAllChanges` set to `true`—just when an interaction causes an increase to INP._
271
272This can be useful when debugging, but in general using `reportAllChanges` is not needed (or recommended) for measuring these metrics in production.
273
274```js
275import {onCLS} from 'web-vitals';
276
277// Logs CLS as the value changes.
278onCLS(console.log, {reportAllChanges: true});
279```
280
281### Report only the delta of changes
282
283Some analytics providers allow you to update the value of a metric, even after you've already sent it to their servers (overwriting the previously-sent value with the same `id`).
284
285Other analytics providers, however, do not allow this, so instead of reporting the new value, you need to report only the delta (the difference between the current value and the last-reported value). You can then compute the total value by summing all metric deltas sent with the same ID.
286
287The following example shows how to use the `id` and `delta` properties:
288
289```js
290import {onCLS, onFID, onLCP} from 'web-vitals';
291
292function logDelta({name, id, delta}) {
293 console.log(`${name} matching ID ${id} changed by ${delta}`);
294}
295
296onCLS(logDelta);
297onFID(logDelta);
298onLCP(logDelta);
299```
300
301_**Note:** the first time the `callback` function is called, its `value` and `delta` properties will be the same._
302
303In addition to using the `id` field to group multiple deltas for the same metric, it can also be used to differentiate different metrics reported on the same page. For example, after a back/forward cache restore, a new metric object is created with a new `id` (since back/forward cache restores are considered separate page visits).
304
305### Send the results to an analytics endpoint
306
307The following example measures each of the Core Web Vitals metrics and reports them to a hypothetical `/analytics` endpoint, as soon as each is ready to be sent.
308
309The `sendToAnalytics()` function uses the [`navigator.sendBeacon()`](https://developer.mozilla.org/docs/Web/API/Navigator/sendBeacon) method (if available), but falls back to the [`fetch()`](https://developer.mozilla.org/docs/Web/API/Fetch_API) API when not.
310
311```js
312import {onCLS, onFID, onLCP} from 'web-vitals';
313
314function sendToAnalytics(metric) {
315 // Replace with whatever serialization method you prefer.
316 // Note: JSON.stringify will likely include more data than you need.
317 const body = JSON.stringify(metric);
318
319 // Use `navigator.sendBeacon()` if available, falling back to `fetch()`.
320 (navigator.sendBeacon && navigator.sendBeacon('/analytics', body)) ||
321 fetch('/analytics', {body, method: 'POST', keepalive: true});
322}
323
324onCLS(sendToAnalytics);
325onFID(sendToAnalytics);
326onLCP(sendToAnalytics);
327```
328
329### Send the results to Google Analytics
330
331Google Analytics does not support reporting metric distributions in any of its built-in reports; however, if you set a unique event parameter value (in this case, the metric_id, as shown in the example below) on every metric instance that you send to Google Analytics, you can create a report yourself by first getting the data via the [Google Analytics Data API](https://developers.google.com/analytics/devguides/reporting/data/v1) or via [BigQuery export](https://support.google.com/analytics/answer/9358801) and then visualizing it any charting library you choose.
332
333[Google Analytics 4](https://support.google.com/analytics/answer/10089681) introduces a new Event model allowing custom parameters instead of a fixed category, action, and label. It also supports non-integer values, making it easier to measure Web Vitals metrics compared to previous versions.
334
335```js
336import {onCLS, onFID, onLCP} from 'web-vitals';
337
338function sendToGoogleAnalytics({name, delta, value, id}) {
339 // Assumes the global `gtag()` function exists, see:
340 // https://developers.google.com/analytics/devguides/collection/ga4
341 gtag('event', name, {
342 // Built-in params:
343 value: delta, // Use `delta` so the value can be summed.
344 // Custom params:
345 metric_id: id, // Needed to aggregate events.
346 metric_value: value, // Optional.
347 metric_delta: delta, // Optional.
348
349 // OPTIONAL: any additional params or debug info here.
350 // See: https://web.dev/articles/debug-performance-in-the-field
351 // metric_rating: 'good' | 'needs-improvement' | 'poor',
352 // debug_info: '...',
353 // ...
354 });
355}
356
357onCLS(sendToGoogleAnalytics);
358onFID(sendToGoogleAnalytics);
359onLCP(sendToGoogleAnalytics);
360```
361
362For details on how to query this data in [BigQuery](https://cloud.google.com/bigquery), or visualise it in [Looker Studio](https://lookerstudio.google.com/), see [Measure and debug performance with Google Analytics 4 and BigQuery](https://web.dev/articles/vitals-ga4).
363
364### Send the results to Google Tag Manager
365
366The recommended way to measure Web Vitals metrics with Google Tag Manager is using the [Core Web Vitals](https://www.simoahava.com/custom-templates/core-web-vitals/) custom template tag created and maintained by [Simo Ahava](https://www.simoahava.com/).
367
368For full installation and usage instructions, see Simo's post: [Track Core Web Vitals in GA4 with Google Tag Manager](https://www.simoahava.com/analytics/track-core-web-vitals-in-ga4-with-google-tag-manager/).
369
370### Send attribution data
371
372When using the [attribution build](#attribution-build), you can send additional data to help you debug _why_ the metric values are they way they are.
373
374This example sends an additional `debug_target` param to Google Analytics, corresponding to the element most associated with each metric.
375
376```js
377import {onCLS, onFID, onLCP} from 'web-vitals/attribution';
378
379function sendToGoogleAnalytics({name, delta, value, id, attribution}) {
380 const eventParams = {
381 // Built-in params:
382 value: delta, // Use `delta` so the value can be summed.
383 // Custom params:
384 metric_id: id, // Needed to aggregate events.
385 metric_value: value, // Optional.
386 metric_delta: delta, // Optional.
387 };
388
389 switch (name) {
390 case 'CLS':
391 eventParams.debug_target = attribution.largestShiftTarget;
392 break;
393 case 'FID':
394 eventParams.debug_target = attribution.eventTarget;
395 break;
396 case 'LCP':
397 eventParams.debug_target = attribution.element;
398 break;
399 }
400
401 // Assumes the global `gtag()` function exists, see:
402 // https://developers.google.com/analytics/devguides/collection/ga4
403 gtag('event', name, eventParams);
404}
405
406onCLS(sendToGoogleAnalytics);
407onFID(sendToGoogleAnalytics);
408onLCP(sendToGoogleAnalytics);
409```
410
411_**Note:** this example relies on custom [event parameters](https://support.google.com/analytics/answer/11396839) in Google Analytics 4._
412
413See [Debug performance in the field](https://web.dev/articles/debug-performance-in-the-field) for more information and examples.
414
415### Batch multiple reports together
416
417Rather than reporting each individual Web Vitals metric separately, you can minimize your network usage by batching multiple metric reports together in a single network request.
418
419However, since not all Web Vitals metrics become available at the same time, and since not all metrics are reported on every page, you cannot simply defer reporting until all metrics are available.
420
421Instead, you should keep a queue of all metrics that were reported and flush the queue whenever the page is backgrounded or unloaded:
422
423```js
424import {onCLS, onFID, onLCP} from 'web-vitals';
425
426const queue = new Set();
427function addToQueue(metric) {
428 queue.add(metric);
429}
430
431function flushQueue() {
432 if (queue.size > 0) {
433 // Replace with whatever serialization method you prefer.
434 // Note: JSON.stringify will likely include more data than you need.
435 const body = JSON.stringify([...queue]);
436
437 // Use `navigator.sendBeacon()` if available, falling back to `fetch()`.
438 (navigator.sendBeacon && navigator.sendBeacon('/analytics', body)) ||
439 fetch('/analytics', {body, method: 'POST', keepalive: true});
440
441 queue.clear();
442 }
443}
444
445onCLS(addToQueue);
446onFID(addToQueue);
447onLCP(addToQueue);
448
449// Report all available metrics whenever the page is backgrounded or unloaded.
450addEventListener('visibilitychange', () => {
451 if (document.visibilityState === 'hidden') {
452 flushQueue();
453 }
454});
455
456// NOTE: Safari does not reliably fire the `visibilitychange` event when the
457// page is being unloaded. If Safari support is needed, you should also flush
458// the queue in the `pagehide` event.
459addEventListener('pagehide', flushQueue);
460```
461
462_**Note:** see [the Page Lifecycle guide](https://developers.google.com/web/updates/2018/07/page-lifecycle-api#legacy-lifecycle-apis-to-avoid) for an explanation of why `visibilitychange` and `pagehide` are recommended over events like `beforeunload` and `unload`._
463
464<a name="bundle-versions"><a>
465
466## Build options
467
468The `web-vitals` package includes builds for the "standard", "attribution", and "base+polyfill" ([deprecated](https://github.com/GoogleChrome/web-vitals/issues/238)) builds, as well as different formats of each to allow developers to choose the format that best meets their needs or integrates with their architecture.
469
470The following table lists all the builds distributed with the `web-vitals` package on npm.
471
472<table>
473 <tr>
474 <td width="35%">
475 <strong>Filename</strong> <em>(all within <code>dist/*</code>)</em>
476 </td>
477 <td><strong>Export</strong></td>
478 <td><strong>Description</strong></td>
479 </tr>
480 <tr>
481 <td><code>web-vitals.js</code></td>
482 <td><code>pkg.module</code></td>
483 <td>
484 <p>An ES module bundle of all metric functions, without any attribution features.</p>
485 This is the "standard" build and is the simplest way to consume this library out of the box.
486 </td>
487 </tr>
488 <tr>
489 <td><code>web-vitals.umd.cjs</code></td>
490 <td><code>pkg.main</code></td>
491 <td>
492 A UMD version of the <code>web-vitals.js</code> bundle (exposed on the <code>window.webVitals.*</code> namespace).
493 </td>
494 </tr>
495 <tr>
496 <td><code>web-vitals.iife.js</code></td>
497 <td>--</td>
498 <td>
499 An IIFE version of the <code>web-vitals.js</code> bundle (exposed on the <code>window.webVitals.*</code> namespace).
500 </td>
501 </tr>
502 <tr>
503 <td><code>web-vitals.attribution.js</code></td>
504 <td>--</td>
505 <td>
506 An ES module version of all metric functions that includes <a href="#attribution-build">attribution</a> features.
507 </td>
508 </tr>
509 <tr>
510 <td><code>web-vitals.attribution.umd.cjs</code></td>
511 <td>--</td>
512 <td>
513 A UMD version of the <code>web-vitals.attribution.js</code> build (exposed on the <code>window.webVitals.*</code> namespace).
514 </td>
515 </tr>
516 </tr>
517 <tr>
518 <td><code>web-vitals.attribution.iife.js</code></td>
519 <td>--</td>
520 <td>
521 An IIFE version of the <code>web-vitals.attribution.js</code> build (exposed on the <code>window.webVitals.*</code> namespace).
522 </td>
523 </tr>
524 <tr>
525 <td><code>web-vitals.base.js</code></td>
526 <td>--</td>
527 <td>
528 <p><strong>This build has been <a href="https://github.com/GoogleChrome/web-vitals/issues/238">deprecated</a>.</strong></p>
529 <p>An ES module bundle containing just the "base" part of the "base+polyfill" version.</p>
530 Use this bundle if (and only if) you've also added the <code>polyfill.js</code> script to the <code>&lt;head&gt;</code> of your pages. See <a href="#how-to-use-the-polyfill">how to use the polyfill</a> for more details.
531 </td>
532 </tr>
533 <tr>
534 <td><code>web-vitals.base.umd.cjs</code></td>
535 <td>--</td>
536 <td>
537 <p><strong>This build has been <a href="https://github.com/GoogleChrome/web-vitals/issues/238">deprecated</a>.</strong></p>
538 <p>A UMD version of the <code>web-vitals.base.js</code> bundle (exposed on the <code>window.webVitals.*</code> namespace).</p>
539 </td>
540 </tr>
541 </tr>
542 <tr>
543 <td><code>web-vitals.base.iife.js</code></td>
544 <td>--</td>
545 <td>
546 <p><strong>This build has been <a href="https://github.com/GoogleChrome/web-vitals/issues/238">deprecated</a>.</strong></p>
547 <p>An IIFE version of the <code>web-vitals.base.js</code> bundle (exposed on the <code>window.webVitals.*</code> namespace).</p>
548 </td>
549 </tr>
550 <tr>
551 <td><code>polyfill.js</code></td>
552 <td>--</td>
553 <td>
554 <p><strong>This build has been <a href="https://github.com/GoogleChrome/web-vitals/issues/238">deprecated</a>.</strong></p>
555 <p>The "polyfill" part of the "base+polyfill" version. This script should be used with either <code>web-vitals.base.js</code>, <code>web-vitals.base.umd.cjs</code>, or <code>web-vitals.base.iife.js</code> (it will not work with any script that doesn't have "base" in the filename).</p>
556 See <a href="#how-to-use-the-polyfill">how to use the polyfill</a> for more details.
557 </td>
558 </tr>
559</table>
560
561<a name="which-build-is-right-for-you"><a>
562
563### Which build is right for you?
564
565Most developers will generally want to use "standard" build (via either the ES module or UMD version, depending on your bundler/build system), as it's the easiest to use out of the box and integrate into existing tools.
566
567However, if you'd lke to collect additional debug information to help you diagnose performance bottlenecks based on real-user issues, use the ["attribution" build](#attribution-build).
568
569For guidance on how to collect and use real-user data to debug performance issues, see [Debug performance in the field](https://web.dev/debug-performance-in-the-field/).
570
571### How the polyfill works
572
573_**⚠️ Warning ⚠️** the "base+polyfill" build is deprecated. See [#238](https://github.com/GoogleChrome/web-vitals/issues/238) for details._
574
575The `polyfill.js` script adds event listeners (to track FID cross-browser), and it records initial page visibility state as well as the timestamp of the first visibility change to hidden (to improve the accuracy of CLS, FCP, LCP, and FID). It also polyfills the [Navigation Timing API Level 2](https://www.w3.org/TR/navigation-timing-2/) in browsers that only support the original (now deprecated) [Navigation Timing API](https://www.w3.org/TR/navigation-timing/).
576
577In order for the polyfill to work properly, the script must be the first script added to the page, and it must run before the browser renders any content to the screen. This is why it needs to be added to the `<head>` of the document.
578
579The "standard" build of the `web-vitals` library includes some of the same logic found in `polyfill.js`. To avoid duplicating that code when using the "base+polyfill" build, the `web-vitals.base.js` bundle does not include any polyfill logic, instead it coordinates with the code in `polyfill.js`, which is why the two scripts must be used together.
580
581## API
582
583### Types:
584
585#### `Metric`
586
587```ts
588interface Metric {
589 /**
590 * The name of the metric (in acronym form).
591 */
592 name: 'CLS' | 'FCP' | 'FID' | 'INP' | 'LCP' | 'TTFB';
593
594 /**
595 * The current value of the metric.
596 */
597 value: number;
598
599 /**
600 * The rating as to whether the metric value is within the "good",
601 * "needs improvement", or "poor" thresholds of the metric.
602 */
603 rating: 'good' | 'needs-improvement' | 'poor';
604
605 /**
606 * The delta between the current value and the last-reported value.
607 * On the first report, `delta` and `value` will always be the same.
608 */
609 delta: number;
610
611 /**
612 * A unique ID representing this particular metric instance. This ID can
613 * be used by an analytics tool to dedupe multiple values sent for the same
614 * metric instance, or to group multiple deltas together and calculate a
615 * total. It can also be used to differentiate multiple different metric
616 * instances sent from the same page, which can happen if the page is
617 * restored from the back/forward cache (in that case new metrics object
618 * get created).
619 */
620 id: string;
621
622 /**
623 * Any performance entries relevant to the metric value calculation.
624 * The array may also be empty if the metric value was not based on any
625 * entries (e.g. a CLS value of 0 given no layout shifts).
626 */
627 entries: (
628 | PerformanceEntry
629 | LayoutShift
630 | FirstInputPolyfillEntry
631 | NavigationTimingPolyfillEntry
632 )[];
633
634 /**
635 * The type of navigation.
636 *
637 * This will be the value returned by the Navigation Timing API (or
638 * `undefined` if the browser doesn't support that API), with the following
639 * exceptions:
640 * - 'back-forward-cache': for pages that are restored from the bfcache.
641 * - 'prerender': for pages that were prerendered.
642 * - 'restore': for pages that were discarded by the browser and then
643 * restored by the user.
644 */
645 navigationType:
646 | 'navigate'
647 | 'reload'
648 | 'back-forward'
649 | 'back-forward-cache'
650 | 'prerender'
651 | 'restore';
652}
653```
654
655Metric-specific subclasses:
656
657- [`CLSMetric`](/src/types/cls.ts#:~:text=interface%20CLSMetric)
658- [`FCPMetric`](/src/types/fcp.ts#:~:text=interface%20FCPMetric)
659- [`FIDMetric`](/src/types/fid.ts#:~:text=interface%20FIDMetric)
660- [`INPMetric`](/src/types/inp.ts#:~:text=interface%20INPMetric)
661- [`LCPMetric`](/src/types/lcp.ts#:~:text=interface%20LCPMetric)
662- [`TTFBMetric`](/src/types/ttfb.ts#:~:text=interface%20TTFBMetric)
663
664#### `MetricWithAttribution`
665
666See the [attribution build](#attribution-build) section for details on how to use this feature.
667
668```ts
669interface MetricWithAttribution extends Metric {
670 /**
671 * An object containing potentially-helpful debugging information that
672 * can be sent along with the metric value for the current page visit in
673 * order to help identify issues happening to real-users in the field.
674 */
675 attribution: {[key: string]: unknown};
676}
677```
678
679Metric-specific subclasses:
680
681- [`CLSMetricWithAttribution`](/src/types/cls.ts#:~:text=interface%20CLSMetricWithAttribution)
682- [`FCPMetricWithAttribution`](/src/types/fcp.ts#:~:text=interface%20FCPMetricWithAttribution)
683- [`FIDMetricWithAttribution`](/src/types/fid.ts#:~:text=interface%20FIDMetricWithAttribution)
684- [`INPMetricWithAttribution`](/src/types/inp.ts#:~:text=interface%20INPMetricWithAttribution)
685- [`LCPMetricWithAttribution`](/src/types/lcp.ts#:~:text=interface%20LCPMetricWithAttribution)
686- [`TTFBMetricWithAttribution`](/src/types/ttfb.ts#:~:text=interface%20TTFBMetricWithAttribution)
687
688#### `MetricRatingThresholds`
689
690The thresholds of metric's "good", "needs improvement", and "poor" ratings.
691
692- Metric values up to and including [0] are rated "good"
693- Metric values up to and including [1] are rated "needs improvement"
694- Metric values above [1] are "poor"
695
696| Metric value | Rating |
697| --------------- | ------------------- |
698| ≦ [0] | "good" |
699| > [0] and ≦ [1] | "needs improvement" |
700| > [1] | "poor" |
701
702```ts
703export type MetricRatingThresholds = [number, number];
704```
705
706_See also [Rating Thresholds](#rating-thresholds)._
707
708#### `ReportCallback`
709
710```ts
711interface ReportCallback {
712 (metric: Metric): void;
713}
714```
715
716Metric-specific subclasses:
717
718- [`CLSReportCallback`](/src/types/cls.ts#:~:text=interface%20CLSReportCallback)
719- [`FCPReportCallback`](/src/types/fcp.ts#:~:text=interface%20FCPReportCallback)
720- [`FIDReportCallback`](/src/types/fid.ts#:~:text=interface%20FIDReportCallback)
721- [`INPReportCallback`](/src/types/inp.ts#:~:text=interface%20INPReportCallback)
722- [`LCPReportCallback`](/src/types/lcp.ts#:~:text=interface%20LCPReportCallback)
723- [`TTFBReportCallback`](/src/types/ttfb.ts#:~:text=interface%20TTFBReportCallback)
724
725#### `ReportOpts`
726
727```ts
728interface ReportOpts {
729 reportAllChanges?: boolean;
730 durationThreshold?: number;
731}
732```
733
734#### `LoadState`
735
736The `LoadState` type is used in several of the metric [attribution objects](#attribution).
737
738```ts
739/**
740 * The loading state of the document. Note: this value is similar to
741 * `document.readyState` but it subdivides the "interactive" state into the
742 * time before and after the DOMContentLoaded event fires.
743 *
744 * State descriptions:
745 * - `loading`: the initial document response has not yet been fully downloaded
746 * and parsed. This is equivalent to the corresponding `readyState` value.
747 * - `dom-interactive`: the document has been fully loaded and parsed, but
748 * scripts may not have yet finished loading and executing.
749 * - `dom-content-loaded`: the document is fully loaded and parsed, and all
750 * scripts (except `async` scripts) have loaded and finished executing.
751 * - `complete`: the document and all of its sub-resources have finished
752 * loading. This is equivalent to the corresponding `readyState` value.
753 */
754type LoadState =
755 | 'loading'
756 | 'dom-interactive'
757 | 'dom-content-loaded'
758 | 'complete';
759```
760
761#### `FirstInputPolyfillEntry`
762
763If using the "base+polyfill" build (and if the browser doesn't natively support the Event Timing API), the `metric.entries` reported by `onFID()` will contain an object that polyfills the `PerformanceEventTiming` entry:
764
765```ts
766type FirstInputPolyfillEntry = Omit<
767 PerformanceEventTiming,
768 'processingEnd' | 'toJSON'
769>;
770```
771
772#### `FirstInputPolyfillCallback`
773
774```ts
775interface FirstInputPolyfillCallback {
776 (entry: FirstInputPolyfillEntry): void;
777}
778```
779
780#### `NavigationTimingPolyfillEntry`
781
782If using the "base+polyfill" build (and if the browser doesn't support the [Navigation Timing API Level 2](https://www.w3.org/TR/navigation-timing-2/) interface), the `metric.entries` reported by `onTTFB()` will contain an object that polyfills the `PerformanceNavigationTiming` entry using timings from the legacy `performance.timing` interface:
783
784```ts
785type NavigationTimingPolyfillEntry = Omit<
786 PerformanceNavigationTiming,
787 | 'initiatorType'
788 | 'nextHopProtocol'
789 | 'redirectCount'
790 | 'transferSize'
791 | 'encodedBodySize'
792 | 'decodedBodySize'
793 | 'type'
794> & {
795 type: PerformanceNavigationTiming['type'];
796};
797```
798
799#### `WebVitalsGlobal`
800
801If using the "base+polyfill" build, the `polyfill.js` script creates the global `webVitals` namespace matching the following interface:
802
803```ts
804interface WebVitalsGlobal {
805 firstInputPolyfill: (onFirstInput: FirstInputPolyfillCallback) => void;
806 resetFirstInputPolyfill: () => void;
807 firstHiddenTime: number;
808}
809```
810
811### Functions:
812
813#### `onCLS()`
814
815```ts
816type onCLS = (callback: CLSReportCallback, opts?: ReportOpts) => void;
817```
818
819Calculates the [CLS](https://web.dev/articles/cls) value for the current page and calls the `callback` function once the value is ready to be reported, along with all `layout-shift` performance entries that were used in the metric value calculation. The reported value is a [double](https://heycam.github.io/webidl/#idl-double) (corresponding to a [layout shift score](https://web.dev/articles/cls#layout_shift_score)).
820
821If the `reportAllChanges` [configuration option](#reportopts) is set to `true`, the `callback` function will be called as soon as the value is initially determined as well as any time the value changes throughout the page lifespan (Note [not necessarily for every layout shift](#report-the-value-on-every-change)).
822
823_**Important:** CLS should be continually monitored for changes throughout the entire lifespan of a page—including if the user returns to the page after it's been hidden/backgrounded. However, since browsers often [will not fire additional callbacks once the user has backgrounded a page](https://developer.chrome.com/blog/page-lifecycle-api/#advice-hidden), `callback` is always called when the page's visibility state changes to hidden. As a result, the `callback` function might be called multiple times during the same page load (see [Reporting only the delta of changes](#report-only-the-delta-of-changes) for how to manage this)._
824
825#### `onFCP()`
826
827```ts
828type onFCP = (callback: FCPReportCallback, opts?: ReportOpts) => void;
829```
830
831Calculates the [FCP](https://web.dev/articles/fcp) value for the current page and calls the `callback` function once the value is ready, along with the relevant `paint` performance entry used to determine the value. The reported value is a [`DOMHighResTimeStamp`](https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp).
832
833#### `onFID()`
834
835```ts
836type onFID = (callback: FIDReportCallback, opts?: ReportOpts) => void;
837```
838
839Calculates the [FID](https://web.dev/articles/fid) value for the current page and calls the `callback` function once the value is ready, along with the relevant `first-input` performance entry used to determine the value. The reported value is a [`DOMHighResTimeStamp`](https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp).
840
841_**Important:** since FID is only reported after the user interacts with the page, it's possible that it will not be reported for some page loads._
842
843#### `onINP()`
844
845```ts
846type onINP = (callback: INPReportCallback, opts?: ReportOpts) => void;
847```
848
849Calculates the [INP](https://web.dev/articles/inp) value for the current page and calls the `callback` function once the value is ready, along with the `event` performance entries reported for that interaction. The reported value is a [`DOMHighResTimeStamp`](https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp).
850
851A custom `durationThreshold` [configuration option](#reportopts) can optionally be passed to control what `event-timing` entries are considered for INP reporting. The default threshold is `40`, which means INP scores of less than 40 are reported as 0. Note that this will not affect your 75th percentile INP value unless that value is also less than 40 (well below the recommended [good](https://web.dev/articles/inp#what_is_a_good_inp_score) threshold).
852
853If the `reportAllChanges` [configuration option](#reportopts) is set to `true`, the `callback` function will be called as soon as the value is initially determined as well as any time the value changes throughout the page lifespan (Note [not necessarily for every interaction](#report-the-value-on-every-change)).
854
855_**Important:** INP should be continually monitored for changes throughout the entire lifespan of a page—including if the user returns to the page after it's been hidden/backgrounded. However, since browsers often [will not fire additional callbacks once the user has backgrounded a page](https://developer.chrome.com/blog/page-lifecycle-api/#advice-hidden), `callback` is always called when the page's visibility state changes to hidden. As a result, the `callback` function might be called multiple times during the same page load (see [Reporting only the delta of changes](#report-only-the-delta-of-changes) for how to manage this)._
856
857#### `onLCP()`
858
859```ts
860type onLCP = (callback: LCPReportCallback, opts?: ReportOpts) => void;
861```
862
863Calculates the [LCP](https://web.dev/articles/lcp) value for the current page and calls the `callback` function once the value is ready (along with the relevant `largest-contentful-paint` performance entry used to determine the value). The reported value is a [`DOMHighResTimeStamp`](https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp).
864
865If the `reportAllChanges` [configuration option](#reportopts) is set to `true`, the `callback` function will be called any time a new `largest-contentful-paint` performance entry is dispatched, or once the final value of the metric has been determined.
866
867#### `onTTFB()`
868
869```ts
870type onTTFB = (callback: TTFBReportCallback, opts?: ReportOpts) => void;
871```
872
873Calculates the [TTFB](https://web.dev/articles/ttfb) value for the current page and calls the `callback` function once the page has loaded, along with the relevant `navigation` performance entry used to determine the value. The reported value is a [`DOMHighResTimeStamp`](https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp).
874
875Note, this function waits until after the page is loaded to call `callback` in order to ensure all properties of the `navigation` entry are populated. This is useful if you want to report on other metrics exposed by the [Navigation Timing API](https://w3c.github.io/navigation-timing/).
876
877For example, the TTFB metric starts from the page's [time origin](https://www.w3.org/TR/hr-time-2/#sec-time-origin), which means it includes time spent on DNS lookup, connection negotiation, network latency, and server processing time.
878
879```js
880import {onTTFB} from 'web-vitals';
881
882onTTFB((metric) => {
883 // Calculate the request time by subtracting from TTFB
884 // everything that happened prior to the request starting.
885 const requestTime = metric.value - metric.entries[0].requestStart;
886 console.log('Request time:', requestTime);
887});
888```
889
890_**Note:** browsers that do not support `navigation` entries will fall back to
891using `performance.timing` (with the timestamps converted from epoch time to [`DOMHighResTimeStamp`](https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp)). This ensures code referencing these values (like in the example above) will work the same in all browsers._
892
893### Rating Thresholds:
894
895The thresholds of each metric's "good", "needs improvement", and "poor" ratings are available as [`MetricRatingThresholds`](#metricratingthresholds).
896
897Example:
898
899```ts
900import {CLSThresholds, FIDThresholds, LCPThresholds} from 'web-vitals';
901
902console.log(CLSThresholds); // [ 0.1, 0.25 ]
903console.log(FIDThresholds); // [ 100, 300 ]
904console.log(LCPThresholds); // [ 2500, 4000 ]
905```
906
907_**Note:** It's typically not necessary (or recommended) to manually calculate metric value ratings using these thresholds. Use the [`Metric['rating']`](#metric) supplied by the [`ReportCallback`](#reportcallback) functions instead._
908
909### Attribution:
910
911The following objects contain potentially-helpful debugging information that can be sent along with the metric values for the current page visit in order to help identify issues happening to real-users in the field.
912
913See the [attribution build](#attribution-build) section for details on how to use this feature.
914
915#### CLS `attribution`:
916
917```ts
918interface CLSAttribution {
919 /**
920 * A selector identifying the first element (in document order) that
921 * shifted when the single largest layout shift contributing to the page's
922 * CLS score occurred.
923 */
924 largestShiftTarget?: string;
925 /**
926 * The time when the single largest layout shift contributing to the page's
927 * CLS score occurred.
928 */
929 largestShiftTime?: DOMHighResTimeStamp;
930 /**
931 * The layout shift score of the single largest layout shift contributing to
932 * the page's CLS score.
933 */
934 largestShiftValue?: number;
935 /**
936 * The `LayoutShiftEntry` representing the single largest layout shift
937 * contributing to the page's CLS score. (Useful when you need more than just
938 * `largestShiftTarget`, `largestShiftTime`, and `largestShiftValue`).
939 */
940 largestShiftEntry?: LayoutShift;
941 /**
942 * The first element source (in document order) among the `sources` list
943 * of the `largestShiftEntry` object. (Also useful when you need more than
944 * just `largestShiftTarget`, `largestShiftTime`, and `largestShiftValue`).
945 */
946 largestShiftSource?: LayoutShiftAttribution;
947 /**
948 * The loading state of the document at the time when the largest layout
949 * shift contribution to the page's CLS score occurred (see `LoadState`
950 * for details).
951 */
952 loadState?: LoadState;
953}
954```
955
956#### FCP `attribution`:
957
958```ts
959interface FCPAttribution {
960 /**
961 * The time from when the user initiates loading the page until when the
962 * browser receives the first byte of the response (a.k.a. TTFB).
963 */
964 timeToFirstByte: number;
965 /**
966 * The delta between TTFB and the first contentful paint (FCP).
967 */
968 firstByteToFCP: number;
969 /**
970 * The loading state of the document at the time when FCP `occurred (see
971 * `LoadState` for details). Ideally, documents can paint before they finish
972 * loading (e.g. the `loading` or `dom-interactive` phases).
973 */
974 loadState: LoadState;
975 /**
976 * The `PerformancePaintTiming` entry corresponding to FCP.
977 */
978 fcpEntry?: PerformancePaintTiming;
979 /**
980 * The `navigation` entry of the current page, which is useful for diagnosing
981 * general page load issues. This can be used to access `serverTiming` for example:
982 * navigationEntry?.serverTiming
983 */
984 navigationEntry?: PerformanceNavigationTiming | NavigationTimingPolyfillEntry;
985}
986```
987
988#### FID `attribution`:
989
990```ts
991interface FIDAttribution {
992 /**
993 * A selector identifying the element that the user interacted with. This
994 * element will be the `target` of the `event` dispatched.
995 */
996 eventTarget: string;
997 /**
998 * The time when the user interacted. This time will match the `timeStamp`
999 * value of the `event` dispatched.
1000 */
1001 eventTime: number;
1002 /**
1003 * The `type` of the `event` dispatched from the user interaction.
1004 */
1005 eventType: string;
1006 /**
1007 * The `PerformanceEventTiming` entry corresponding to FID (or the
1008 * polyfill entry in browsers that don't support Event Timing).
1009 */
1010 eventEntry: PerformanceEventTiming | FirstInputPolyfillEntry;
1011 /**
1012 * The loading state of the document at the time when the first interaction
1013 * occurred (see `LoadState` for details). If the first interaction occurred
1014 * while the document was loading and executing script (e.g. usually in the
1015 * `dom-interactive` phase) it can result in long input delays.
1016 */
1017 loadState: LoadState;
1018}
1019```
1020
1021#### INP `attribution`:
1022
1023```ts
1024interface INPAttribution {
1025 /**
1026 * A selector identifying the element that the user interacted with for
1027 * the event corresponding to INP. This element will be the `target` of the
1028 * `event` dispatched.
1029 */
1030 eventTarget?: string;
1031 /**
1032 * The time when the user interacted for the event corresponding to INP.
1033 * This time will match the `timeStamp` value of the `event` dispatched.
1034 */
1035 eventTime?: number;
1036 /**
1037 * The `type` of the `event` dispatched corresponding to INP.
1038 */
1039 eventType?: string;
1040 /**
1041 * The `PerformanceEventTiming` entry corresponding to INP.
1042 */
1043 eventEntry?: PerformanceEventTiming;
1044 /**
1045 * The loading state of the document at the time when the even corresponding
1046 * to INP occurred (see `LoadState` for details). If the interaction occurred
1047 * while the document was loading and executing script (e.g. usually in the
1048 * `dom-interactive` phase) it can result in long delays.
1049 */
1050 loadState?: LoadState;
1051}
1052```
1053
1054#### LCP `attribution`:
1055
1056```ts
1057interface LCPAttribution {
1058 /**
1059 * The element corresponding to the largest contentful paint for the page.
1060 */
1061 element?: string;
1062 /**
1063 * The URL (if applicable) of the LCP image resource. If the LCP element
1064 * is a text node, this value will not be set.
1065 */
1066 url?: string;
1067 /**
1068 * The time from when the user initiates loading the page until when the
1069 * browser receives the first byte of the response (a.k.a. TTFB). See
1070 * [Optimize LCP](https://web.dev/articles/optimize-lcp) for details.
1071 */
1072 timeToFirstByte: number;
1073 /**
1074 * The delta between TTFB and when the browser starts loading the LCP
1075 * resource (if there is one, otherwise 0). See [Optimize
1076 * LCP](https://web.dev/articles/optimize-lcp) for details.
1077 */
1078 resourceLoadDelay: number;
1079 /**
1080 * The total time it takes to load the LCP resource itself (if there is one,
1081 * otherwise 0). See [Optimize LCP](https://web.dev/articles/optimize-lcp) for
1082 * details.
1083 */
1084 resourceLoadTime: number;
1085 /**
1086 * The delta between when the LCP resource finishes loading until the LCP
1087 * element is fully rendered. See [Optimize
1088 * LCP](https://web.dev/articles/optimize-lcp) for details.
1089 */
1090 elementRenderDelay: number;
1091 /**
1092 * The `navigation` entry of the current page, which is useful for diagnosing
1093 * general page load issues. This can be used to access `serverTiming` for example:
1094 * navigationEntry?.serverTiming
1095 */
1096 navigationEntry?: PerformanceNavigationTiming | NavigationTimingPolyfillEntry;
1097 /**
1098 * The `resource` entry for the LCP resource (if applicable), which is useful
1099 * for diagnosing resource load issues.
1100 */
1101 lcpResourceEntry?: PerformanceResourceTiming;
1102 /**
1103 * The `LargestContentfulPaint` entry corresponding to LCP.
1104 */
1105 lcpEntry?: LargestContentfulPaint;
1106}
1107```
1108
1109#### TTFB `attribution`:
1110
1111```ts
1112interface TTFBAttribution {
1113 /**
1114 * The total time from when the user initiates loading the page to when the
1115 * DNS lookup begins. This includes redirects, service worker startup, and
1116 * HTTP cache lookup times.
1117 */
1118 waitingTime: number;
1119 /**
1120 * The total time to resolve the DNS for the current request.
1121 */
1122 dnsTime: number;
1123 /**
1124 * The total time to create the connection to the requested domain.
1125 */
1126 connectionTime: number;
1127 /**
1128 * The time time from when the request was sent until the first byte of the
1129 * response was received. This includes network time as well as server
1130 * processing time.
1131 */
1132 requestTime: number;
1133 /**
1134 * The `navigation` entry of the current page, which is useful for diagnosing
1135 * general page load issues. This can be used to access `serverTiming` for example:
1136 * navigationEntry?.serverTiming
1137 */
1138 navigationEntry?: PerformanceNavigationTiming | NavigationTimingPolyfillEntry;
1139}
1140```
1141
1142## Browser Support
1143
1144The `web-vitals` code has been tested and will run without error in all major browsers as well as Internet Explorer back to version 9. However, some of the APIs required to capture these metrics are currently only available in Chromium-based browsers (e.g. Chrome, Edge, Opera, Samsung Internet).
1145
1146Browser support for each function is as follows:
1147
1148- `onCLS()`: Chromium
1149- `onFCP()`: Chromium, Firefox, Safari 14.1+
1150- `onFID()`: Chromium, Firefox _(with [polyfill](#how-to-use-the-polyfill): Safari, Internet Explorer)_
1151- `onINP()`: Chromium
1152- `onLCP()`: Chromium
1153- `onTTFB()`: Chromium, Firefox, Safari 15+ _(with [polyfill](#how-to-use-the-polyfill): Safari 8+, Internet Explorer)_
1154
1155## Limitations
1156
1157The `web-vitals` library is primarily a wrapper around the Web APIs that measure the Web Vitals metrics, which means the limitations of those APIs will mostly apply to this library as well. More details on these limitations is available in [this blog post](https://web.dev/articles/crux-and-rum-differences).
1158
1159The primary limitation of these APIs is they have no visibility into `<iframe>` content (not even same-origin iframes), which means pages that make use of iframes will likely see a difference between the data measured by this library and the data available in the Chrome User Experience Report (which does include iframe content).
1160
1161For same-origin iframes, it's possible to use the `web-vitals` library to measure metrics, but it's tricky because it requires the developer to add the library to every frame and `postMessage()` the results to the parent frame for aggregation.
1162
1163_**Note:** given the lack of iframe support, the `onCLS()` function technically measures [DCLS](https://github.com/wicg/layout-instability#cumulative-scores) (Document Cumulative Layout Shift) rather than CLS, if the page includes iframes)._
1164
1165## Development
1166
1167### Building the code
1168
1169The `web-vitals` source code is written in TypeScript. To transpile the code and build the production bundles, run the following command.
1170
1171```sh
1172npm run build
1173```
1174
1175To build the code and watch for changes, run:
1176
1177```sh
1178npm run watch
1179```
1180
1181### Running the tests
1182
1183The `web-vitals` code is tested in real browsers using [webdriver.io](https://webdriver.io/). Use the following command to run the tests:
1184
1185```sh
1186npm test
1187```
1188
1189To test any of the APIs manually, you can start the test server
1190
1191```sh
1192npm run test:server
1193```
1194
1195Then navigate to `http://localhost:9090/test/<view>`, where `<view>` is the basename of one the templates under [/test/views/](/test/views/).
1196
1197You'll likely want to combine this with `npm run watch` to ensure any changes you make are transpiled and rebuilt.
1198
1199## Integrations
1200
1201- [**Web Vitals Connector**](https://goo.gle/web-vitals-connector): Data Studio connector to create dashboards from [Web Vitals data captured in BiqQuery](https://web.dev/articles/vitals-ga4).
1202- [**Core Web Vitals Custom Tag template**](https://www.simoahava.com/custom-templates/core-web-vitals/): Custom GTM template tag to [add measurement handlers](https://www.simoahava.com/analytics/track-core-web-vitals-in-ga4-with-google-tag-manager/) for all Core Web Vitals metrics.
1203- [**`web-vitals-reporter`**](https://github.com/treosh/web-vitals-reporter): JavaScript library to batch `callback` functions and send data with a single request.
1204
1205## License
1206
1207[Apache 2.0](/LICENSE)