UNPKG

28.7 kBMarkdownView Raw
1# `web-vitals`
2
3- [Overview](#overview)
4- [Installation](#installation)
5- [Usage](#usage)
6 - [Load the library](#load-the-library)
7 - [Basic usage](#basic-usage)
8 - [Report the value on every change](#report-the-value-on-every-change)
9 - [Report only the delta of changes](#report-only-the-delta-of-changes)
10 - [Send the results to an analytics endpoint](#send-the-results-to-an-analytics-endpoint)
11 - [Send the results to Google Analytics](#send-the-results-to-google-analytics)
12 - [Send the results to Google Tag Manager](#send-the-results-to-google-tag-manager)
13 - [Load `web-vitals` from a CDN](#load-web-vitals-from-a-cdn)
14- [Bundle versions](#bundle-versions)
15 - [Which bundle is right for you?](#which-bundle-is-right-for-you)
16 - [How the polyfill works](#how-the-polyfill-works)
17- [API](#api)
18 - [Types](#types)
19 - [Functions](#functions)
20- [Browser Support](#browser-support)
21- [Limitations](#limitations)
22- [Development](#development)
23- [License](#license)
24
25## Overview
26
27The `web-vitals` library is a tiny (~1K), 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)).
28
29The library supports all of the [Core Web Vitals](https://web.dev/vitals/#core-web-vitals) as well as all of the [other Web Vitals](https://web.dev/vitals/#other-web-vitals) that can be measured [in the field](https://web.dev/user-centric-performance-metrics/#how-metrics-are-measured):
30
31### Core Web Vitals
32
33- [Cumulative Layout Shift (CLS)](https://web.dev/cls/)
34- [First Input Delay (FID)](https://web.dev/fid/)
35- [Largest Contentful Paint (LCP)](https://web.dev/lcp/)
36
37### Other Web Vitals
38
39- [First Contentful Paint (FCP)](https://web.dev/fcp/)
40- [Time to First Byte (TTFB)](https://web.dev/time-to-first-byte/)
41
42## Installation
43
44You can install this library from npm by running:
45
46```sh
47npm install web-vitals
48```
49
50_**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._
51
52## Usage
53
54### Load the library
55
56There are two different versions of the `web-vitals` library (the "standard" version and the "base+polyfill" version), and how you load the library depends on which version you want to use.
57
58For details on the difference between the two versions, see <a href="#which-bundle-is-right-for-you">which bundle is right for you</a>.
59
60**1. The "standard" version**
61
62To load the "standard" version, import modules from the `web-vitals` package in your application code (as you would with any npm package and node-based build tool):
63
64```js
65import {getLCP, getFID, getCLS} from 'web-vitals';
66```
67
68**2. The "base+polyfill" version**
69
70Loading the "base+polyfill" version is a two-step process:
71
72First, 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`:
73
74```diff
75- import {getLCP, getFID, getCLS} from 'web-vitals';
76+ import {getLCP, getFID, getCLS} from 'web-vitals/base';
77```
78
79Then, inline the code from `dist/polyfill.js` into the `<head>` of your pages.
80
81```html
82<!DOCTYPE html>
83<html>
84 <head>
85 <script>
86 // Inline code from `dist/polyfill.js` here
87 </script>
88 </head>
89 <body>
90 ...
91 </body>
92</html>
93```
94
95Note 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.
96
97_**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._
98
99### Basic usage
100
101Each of the Web Vitals metrics is exposed as a single function that takes an `onReport` callback. This callback will be called any time the metric value is available and ready to be reported.
102
103The following example measures each of the Core Web Vitals metrics and logs the result to the console once its value is ready to report.
104
105_(The examples below import the "standard" version, but they will work with the polyfill version as well.)_
106
107```js
108import {getCLS, getFID, getLCP} from 'web-vitals';
109
110getCLS(console.log);
111getFID(console.log);
112getLCP(console.log);
113```
114
115Note 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://developers.google.com/web/tools/chrome-devtools/console/reference#persist) enabled) or switching tabs and then switching back.
116
117Also, in some cases a metric callback may never be called:
118
119- FID is not reported if the user never interacts with the page.
120- FCP, FID, and LCP are not reported if the page was loaded in the background.
121
122In other cases, a metric callback may be called more than once:
123
124- CLS should be reported any time the [page's `visibilityState` changes to hidden](https://developers.google.com/web/updates/2018/07/page-lifecycle-api#advice-hidden).
125- CLS, FCP, FID, and LCP are reported again after a page is restored from the [back/forward cache](https://web.dev/bfcache/).
126
127_**Warning:** do not call any of the Web Vitals functions (e.g. `getCLS()`, `getFID()`, `getLCP()`) 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._
128
129### Report the value on every change
130
131In most cases, you only want `onReport` 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 the optional, second argument (`reportAllChanges`) to `true`.
132
133This can be useful when debugging, but in general using `reportAllChanges` is not needed (or recommended).
134
135```js
136import {getCLS} from 'web-vitals';
137
138// Logs CLS as the value changes.
139getCLS(console.log, true);
140```
141
142### Report only the delta of changes
143
144Some 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`).
145
146Other 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.
147
148The following example shows how to use the `id` and `delta` properties:
149
150```js
151import {getCLS, getFID, getLCP} from 'web-vitals';
152
153function logDelta({name, id, delta}) {
154 console.log(`${name} matching ID ${id} changed by ${delta}`);
155}
156
157getCLS(logDelta);
158getFID(logDelta);
159getLCP(logDelta);
160```
161
162_**Note:** the first time the `onReport` function is called, its `value` and `delta` properties will be the same._
163
164In 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).
165
166### Send the results to an analytics endpoint
167
168The 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.
169
170The `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.
171
172```js
173import {getCLS, getFID, getLCP} from 'web-vitals';
174
175function sendToAnalytics(metric) {
176 const body = JSON.stringify({[metric.name]: metric.value});
177 // Use `navigator.sendBeacon()` if available, falling back to `fetch()`.
178 (navigator.sendBeacon && navigator.sendBeacon('/analytics', body)) ||
179 fetch('/analytics', {body, method: 'POST', keepalive: true});
180}
181
182getCLS(sendToAnalytics);
183getFID(sendToAnalytics);
184getLCP(sendToAnalytics);
185```
186
187### Send the results to Google Analytics
188
189Google 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.
190
191As 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.
192
193[![web-vitals-report](https://user-images.githubusercontent.com/326742/101584324-3f9a0900-3992-11eb-8f2d-182f302fb67b.png)](https://github.com/GoogleChromeLabs/web-vitals-report)
194
195In 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:
196
197#### Using `analytics.js`
198
199```js
200import {getCLS, getFID, getLCP} from 'web-vitals';
201
202function sendToGoogleAnalytics({name, delta, id}) {
203 // Assumes the global `ga()` function exists, see:
204 // https://developers.google.com/analytics/devguides/collection/analyticsjs
205 ga('send', 'event', {
206 eventCategory: 'Web Vitals',
207 eventAction: name,
208 // The `id` value will be unique to the current page load. When sending
209 // multiple values from the same page (e.g. for CLS), Google Analytics can
210 // compute a total by grouping on this ID (note: requires `eventLabel` to
211 // be a dimension in your report).
212 eventLabel: id,
213 // Google Analytics metrics must be integers, so the value is rounded.
214 // For CLS the value is first multiplied by 1000 for greater precision
215 // (note: increase the multiplier for greater precision if needed).
216 eventValue: Math.round(name === 'CLS' ? delta * 1000 : delta),
217 // Use a non-interaction event to avoid affecting bounce rate.
218 nonInteraction: true,
219 // Use `sendBeacon()` if the browser supports it.
220 transport: 'beacon',
221 });
222}
223
224getCLS(sendToGoogleAnalytics);
225getFID(sendToGoogleAnalytics);
226getLCP(sendToGoogleAnalytics);
227```
228
229#### Using `gtag.js`
230
231```js
232import {getCLS, getFID, getLCP} from 'web-vitals';
233
234function sendToGoogleAnalytics({name, delta, id}) {
235 // Assumes the global `gtag()` function exists, see:
236 // https://developers.google.com/analytics/devguides/collection/gtagjs
237 gtag('event', name, {
238 event_category: 'Web Vitals',
239 // The `id` value will be unique to the current page load. When sending
240 // multiple values from the same page (e.g. for CLS), Google Analytics can
241 // compute a total by grouping on this ID (note: requires `eventLabel` to
242 // be a dimension in your report).
243 event_label: id,
244 // Google Analytics metrics must be integers, so the value is rounded.
245 // For CLS the value is first multiplied by 1000 for greater precision
246 // (note: increase the multiplier for greater precision if needed).
247 value: Math.round(name === 'CLS' ? delta * 1000 : delta),
248 // Use a non-interaction event to avoid affecting bounce rate.
249 non_interaction: true,
250 });
251}
252
253getCLS(sendToGoogleAnalytics);
254getFID(sendToGoogleAnalytics);
255getLCP(sendToGoogleAnalytics);
256```
257
258#### Using Google Tag Manager
259
260The following example measures each of the Core Web Vitals metrics and sends them as separate `dataLayer-events` to be used by Google Tag Manager. With the `web-vitals` trigger you send the metrics to any tag inside your account (see [this comment](https://github.com/GoogleChrome/web-vitals/pull/28#discussion_r422701126) for implementation details).
261
262```js
263import {getCLS, getFID, getLCP} from 'web-vitals';
264
265function sendToGTM({name, delta, id}) {
266 // Assumes the global `dataLayer` array exists, see:
267 // https://developers.google.com/tag-manager/devguide
268 dataLayer.push({
269 event: 'web-vitals',
270 event_category: 'Web Vitals',
271 event_action: name,
272 // The `id` value will be unique to the current page load. When sending
273 // multiple values from the same page (e.g. for CLS), Google Analytics can
274 // compute a total by grouping on this ID (note: requires `eventLabel` to
275 // be a dimension in your report).
276 event_label: id,
277 // Google Analytics metrics must be integers, so the value is rounded.
278 // For CLS the value is first multiplied by 1000 for greater precision
279 // (note: increase the multiplier for greater precision if needed).
280 event_value: Math.round(name === 'CLS' ? delta * 1000 : delta),
281 });
282}
283
284getCLS(sendToGTM);
285getFID(sendToGTM);
286getLCP(sendToGTM);
287```
288
289### Load `web-vitals` from a CDN
290
291The 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.
292
293The following examples show how to load `web-vitals` from [unpkg.com](https://unpkg.com), whether your targeting just modern browsers (using the "standard" version) or all browsers (using the "base+polyfill" version):
294
295**Load the "standard" version** (using a module script)
296
297```html
298<!-- Append the `?module` param to load the module version of `web-vitals` -->
299<script type="module">
300 import {getCLS, getFID, getLCP} from 'https://unpkg.com/web-vitals?module';
301
302 getCLS(console.log);
303 getFID(console.log);
304 getLCP(console.log);
305</script>
306```
307
308**Load the "standard" version** _(using a classic script)_
309
310```html
311<!-- Without the `?module` param, the UMD version is loaded and sets the `webVitals` global -->
312<script defer src="https://unpkg.com/web-vitals"></script>
313<script>
314addEventListener('DOMContentLoaded', function() {
315 webVitals.getCLS(console.log);
316 webVitals.getFID(console.log);
317 webVitals.getLCP(console.log);
318});
319</script>
320```
321
322**Load the "base+polyfill" version** _(using a classic script)_
323
324```html
325<!DOCTYPE html>
326<html>
327 <head>
328 <script>
329 // Inline code from `https://unpkg.com/web-vitals/dist/polyfill.js` here.
330 </script>
331 </head>
332 <body>
333 ...
334 <!-- Load the UMD version of the "base" bundle. -->
335 <script defer src="https://unpkg.com/web-vitals/dist/web-vitals.base.umd.js"></script>
336 <script>
337 addEventListener('DOMContentLoaded', function() {
338 webVitals.getCLS(console.log);
339 webVitals.getFID(console.log);
340 webVitals.getLCP(console.log);
341 });
342 </script>
343 </body>
344</html>
345```
346
347## Bundle versions
348
349The `web-vitals` package includes builds for both the "standard" and "base+polyfill" versions, as well as different formats of each to allow developers to choose the format that best meets their needs or integrates with their architecture.
350
351The following table lists all the bundles distributed with the `web-vitals` package on npm.
352
353<table>
354 <tr>
355 <td width="35%">
356 <strong>Filename</strong> <em>(all within <code>dist/*</code>)</em>
357 </td>
358 <td><strong>Export</strong></td>
359 <td><strong>Description</strong></td>
360 </tr>
361 <tr>
362 <td><code>web-vitals.js</code></td>
363 <td><code>pkg.module</code></td>
364 <td>
365 <p>An ES module bundle of all metric functions, without any extra polyfills to expand browser support.</p>
366 This is the "standard" version and is the simplest way to consume this library out of the box.
367 </td>
368 </tr>
369 <tr>
370 <td><code>web-vitals.umd.js</code></td>
371 <td><code>pgk.main</code></td>
372 <td>
373 A UMD version of the <code>web-vitals.js</code> bundle (exposed on the <code>window.webVitals.*</code> namespace).
374 </td>
375 </tr>
376 <tr>
377 <td><code>web-vitals.base.js</code></td>
378 <td>--</td>
379 <td>
380 <p>An ES module bundle containing just the "base" part of the "base+polyfill" version.</p>
381 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.
382 </td>
383 </tr>
384 <tr>
385 <td><code>web-vitals.base.umd.js</code></td>
386 <td><code>--</code></td>
387 <td>
388 A UMD version of the <code>web-vitals.base.js</code> bundle (exposed on the <code>window.webVitals.*</code> namespace).
389 </td>
390 </tr>
391 <tr>
392 <td><code>polyfill.js</code></td>
393 <td>--</td>
394 <td>
395 <p>The "polyfill" part of the "base+polyfill" version. This script should be used with either <code>web-vitals.base.js</code> or <code>web-vitals.base.umd.js</code> (it will not work with the <code>web-vitals.js</code> or <code>web-vitals.umd.js</code> bundles).</p>
396 See <a href="#how-to-use-the-polyfill">how to use the polyfill</a> for more details.
397 </td>
398 </tr>
399</table>
400
401### Which bundle is right for you?
402
403Most developers will generally want to use the "standard" bundle (either the ES module or UMD version, depending on your build system), as it's the easiest to use out of the box and integrate into existing build tools.
404
405However, there are a few good reasons to consider using the "base+polyfill" version, for example:
406
407- FID can be measured in all browsers.
408- FCP, FID, and LCP will be more accurate in some cases (since the polyfill detects the page's initial `visibilityState` earlier).
409
410### How the polyfill works
411
412The `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 FCP, LCP, and FID).
413
414In order for it 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.
415
416The "standard" version 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" version, 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.
417
418## API
419
420### Types:
421
422#### `Metric`
423
424```ts
425interface Metric {
426 // The name of the metric (in acronym form).
427 name: 'CLS' | 'FCP' | 'FID' | 'LCP' | 'TTFB';
428
429 // The current value of the metric.
430 value: number;
431
432 // The delta between the current value and the last-reported value.
433 // On the first report, `delta` and `value` will always be the same.
434 delta: number;
435
436 // A unique ID representing this particular metric that's specific to the
437 // current page. This ID can be used by an analytics tool to dedupe
438 // multiple values sent for the same metric, or to group multiple deltas
439 // together and calculate a total.
440 id: string;
441
442 // Any performance entries used in the metric value calculation.
443 // Note, entries will be added to the array as the value changes.
444 entries: (PerformanceEntry | FirstInputPolyfillEntry | NavigationTimingPolyfillEntry)[];
445}
446```
447
448#### `ReportHandler`
449
450```ts
451interface ReportHandler {
452 (metric: Metric): void;
453}
454```
455
456#### `FirstInputPolyfillEntry`
457
458When using the FID polyfill (and if the browser doesn't natively support the Event Timing API), `metric.entries` will contain an object that polyfills the `PerformanceEventTiming` entry:
459
460```ts
461type FirstInputPolyfillEntry = Omit<PerformanceEventTiming,
462 'processingEnd' | 'processingEnd', 'toJSON'>
463```
464
465#### `FirstInputPolyfillCallback`
466
467```ts
468interface FirstInputPolyfillCallback {
469 (entry: FirstInputPolyfillEntry): void;
470}
471```
472
473#### `NavigationTimingPolyfillEntry`
474
475When calling `getTTFB()`, if the browser doesn't support the [Navigation Timing API Level 2](https://www.w3.org/TR/navigation-timing-2/) interface, it will polyfill the entry object using timings from `performance.timing`:
476
477```ts
478export type NavigationTimingPolyfillEntry = Omit<PerformanceNavigationTiming,
479 'initiatorType' | 'nextHopProtocol' | 'redirectCount' | 'transferSize' |
480 'encodedBodySize' | 'decodedBodySize' | 'toJSON'>
481```
482
483#### `WebVitalsGlobal`
484
485If using the "base+polyfill" build, the `polyfill.js` script creates the global `webVitals` namespace matching the following interface:
486
487```ts
488interface WebVitalsGlobal {
489 firstInputPolyfill: (onFirstInput: FirstInputPolyfillCallback) => void;
490 resetFirstInputPolyfill: () => void;
491 firstHiddenTime: number;
492}
493```
494
495### Functions:
496
497#### `getCLS()`
498
499```ts
500type getCLS = (onReport: ReportHandler, reportAllChanges?: boolean) => void
501```
502
503Calculates the [CLS](https://web.dev/cls/) value for the current page and calls the `onReport` 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 value](https://wicg.github.io/layout-instability/#layout-shift-value)).
504
505If the `reportAllChanges` param is `true`, the `onReport` function will be called any time a new `layout-shift` performance entry is dispatched, or once the final value of the metric has been determined.
506
507_**Important:** unlike other metrics, CLS continues to monitor changes for the entire lifespan of the page&mdash;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://developers.google.com/web/updates/2018/07/page-lifecycle-api#advice-hidden), `onReport` is always called when the page's visibility state changes to hidden. As a result, the `onReport` 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)._
508
509#### `getFCP()`
510
511```ts
512type getFCP = (onReport: ReportHandler, reportAllChanges?: boolean) => void
513```
514
515Calculates the [FCP](https://web.dev/fcp/) value for the current page and calls the `onReport` function once the value is ready, along with the relevant `paint` performance entry used to determine the value. The reported value is a `DOMHighResTimeStamp`.
516
517#### `getFID()`
518
519```ts
520type getFID = (onReport: ReportHandler, reportAllChanges?: boolean) => void
521```
522
523Calculates the [FID](https://web.dev/fid/) value for the current page and calls the `onReport` function once the value is ready, along with the relevant `first-input` performance entry used to determine the value (and optionally the input event if using the [FID polyfill](#fid-polyfill)). The reported value is a `DOMHighResTimeStamp`.
524
525_**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._
526
527#### `getLCP()`
528
529```ts
530type getLCP = (onReport: ReportHandler, reportAllChanges?: boolean) => void
531```
532
533Calculates the [LCP](https://web.dev/lcp/) value for the current page and calls the `onReport` function once the value is ready (along with the relevant `largest-contentful-paint` performance entries used to determine the value). The reported value is a `DOMHighResTimeStamp`.
534
535If the `reportAllChanges` param is `true`, the `onReport` 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.
536
537#### `getTTFB()`
538
539```ts
540type getTTFB = (onReport: ReportHandler, reportAllChanges?: boolean) => void
541```
542
543Calculates the [TTFB](https://web.dev/time-to-first-byte/) value for the current page and calls the `onReport` function once the page has loaded, along with the relevant `navigation` performance entry used to determine the value. The reported value is a `DOMHighResTimeStamp`.
544
545Note, this function waits until after the page is loaded to call `onReport` 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/).
546
547For 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](https://developers.google.com/web/fundamentals/performance/navigation-and-resource-timing#the_life_and_timings_of_a_network_request) time spent on DNS lookup, connection negotiation, network latency, and unloading the previous document. If, in addition to TTFB, you want a metric that excludes these timings and _just_ captures the time spent making the request and receiving the first byte of the response, you could compute that from data found on the performance entry:
548
549```js
550import {getTTFB} from 'web-vitals';
551
552getTTFB((metric) => {
553 // Calculate the request time by subtracting from TTFB
554 // everything that happened prior to the request starting.
555 const requestTime = metric.value - metric.entries[0].requestStart;
556 console.log('Request time:', requestTime);
557});
558```
559
560_**Note:** browsers that do not support `navigation` entries will fall back to
561using `performance.timing` (with the timestamps converted from epoch time to `DOMHighResTimeStamp`). This ensures code referencing these values (like in the example above) will work the same in all browsers._
562
563## Browser Support
564
565The `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).
566
567Browser support for each function is as follows:
568
569- `getCLS()`: Chromium,
570- `getFCP()`: Chromium, Safari Technology Preview
571- `getFID()`: Chromium, Firefox, Safari, Internet Explorer (with the [polyfill](#how-to-use-the-polyfill))
572- `getLCP()`: Chromium
573- `getTTFB()`: Chromium, Firefox, Safari, Internet Explorer
574
575## Limitations
576
577The `web-vitals` library is primarily a wrapper around the Web APIs that
578measure the Web Vitals metrics, which means the limitations of those APIs will
579mostly apply to this library as well.
580
581The 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).
582
583For 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.
584
585_**Note:** given the lack of iframe support, the `getCLS()` function technically measures [DCLS](https://github.com/wicg/layout-instability#cumulative-scores) (Document Cumulative Layout Shift) rather than CLS, if the page includes iframes)._
586
587## Development
588
589### Building the code
590
591The `web-vitals` source code is written in TypeScript. To transpile the code and build the production bundles, run the following command.
592
593```sh
594npm run build
595```
596
597To build the code and watch for changes, run:
598
599```sh
600npm run watch
601```
602
603### Running the tests
604
605The `web-vitals` code is tested in real browsers using [webdriver.io](https://webdriver.io/). Use the following command to run the tests:
606
607```sh
608npm test
609```
610
611To test any of the APIs manually, you can start the test server
612
613```sh
614npm run test:server
615```
616
617Then navigate to `http://localhost:9090/test/<view>`, where `<view>` is the basename of one the templates under [/test/views/](/test/views/).
618
619You'll likely want to combine this with `npm run watch` to ensure any changes you make are transpiled and rebuilt.
620
621## License
622
623[Apache 2.0](/LICENSE)