UNPKG

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