You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Time to First Byte (TTFB)](https://web.dev/articles/ttfb)
45
45
46
-
<aname="installation"><a>
47
-
<aname="load-the-library"><a>
46
+
<aname="installation"></a>
47
+
<aname="load-the-library"></a>
48
48
49
49
## Install and load the library
50
50
51
-
<aname="import-web-vitals-from-npm"><a>
51
+
<aname="import-web-vitals-from-npm"></a>
52
52
53
53
The `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.
54
54
@@ -75,16 +75,9 @@ To load the "standard" build, import modules from the `web-vitals` package in yo
75
75
76
76
```js
77
77
import {onLCP, onINP, onCLS} from'web-vitals';
78
-
79
-
onCLS(console.log);
80
-
onINP(console.log);
81
-
onLCP(console.log);
82
78
```
83
79
84
-
> [!NOTE]
85
-
> 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
-
<aname="attribution-build"><a>
80
+
<aname="attribution-build"></a>
88
81
89
82
**2. The "attribution" build**
90
83
@@ -97,15 +90,15 @@ The "attribution" build is slightly larger than the "standard" build (by about 1
97
90
To load the "attribution" build, change any `import` statements that reference `web-vitals` to `web-vitals/attribution`:
98
91
99
92
```diff
100
-
-import {onLCP, onINP, onCLS} from 'web-vitals';
101
-
+import {onLCP, onINP, onCLS} from 'web-vitals/attribution';
93
+
import {onLCP, onINP, onCLS} from 'web-vitals';
94
+
import {onLCP, onINP, onCLS} from 'web-vitals/attribution';
102
95
```
103
96
104
97
Usage for each of the imported function is identical to the standard build, but when importing from the attribution build, the [metric](#metric) objects will contain an additional [`attribution`](#attribution) property.
105
98
106
99
See [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
100
108
-
<aname="load-web-vitals-from-a-cdn"><a>
101
+
<aname="load-web-vitals-from-a-cdn"></a>
109
102
110
103
### From a CDN
111
104
@@ -128,6 +121,8 @@ _**Important!** The [unpkg.com](https://unpkg.com), [jsDelivr](https://www.jsdel
128
121
</script>
129
122
```
130
123
124
+
Note: When the web-vitals code is isolated from the application code in this way, there is less need to depend on dynamic imports so this code uses a regular `import` line.
125
+
131
126
**Load the "standard" build**_(using a classic script)_
132
127
133
128
```html
@@ -221,8 +216,7 @@ In other cases, a metric callback may be called more than once:
221
216
222
217
In 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).
223
218
224
-
> [!IMPORTANT]
225
-
> `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.
219
+
> [!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.
226
220
227
221
This can be useful when debugging, but in general using `reportAllChanges` is not needed (or recommended) for measuring these metrics in production.
228
222
@@ -268,9 +262,13 @@ The `sendToAnalytics()` function uses the [`navigator.sendBeacon()`](https://dev
268
262
import {onCLS, onINP, onLCP} from'web-vitals';
269
263
270
264
functionsendToAnalytics(metric) {
271
-
// Replace with whatever serialization method you prefer.
272
-
// Note: JSON.stringify will likely include more data than you need.
273
-
constbody=JSON.stringify(metric);
265
+
constbody=JSON.stringify({
266
+
name:metric.name,
267
+
value:metric.value,
268
+
id:metric.id,
269
+
270
+
// Include additional data as needed...
271
+
});
274
272
275
273
// Use `navigator.sendBeacon()` to send the data, which supports
> 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` is recommended over events like `beforeunload` and `unload`.
417
415
418
-
<aname="bundle-versions"><a>
416
+
<aname="bundle-versions"></a>
419
417
420
418
## Build options
421
419
@@ -477,7 +475,7 @@ The following table lists all the builds distributed with the `web-vitals` packa
Copy file name to clipboardExpand all lines: docs/upgrading-to-v5.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ All of the [builds](README#build-options) in `web-vitals` v5 use only [Baseline
17
17
18
18
If your site needs to support legacy browsers, you can still use the `web-vitals` library without causing errors in those browsers by adhering to the following recommendations:
19
19
20
-
We recommend loading the `web-vitals` library in a separate script file from your site's main application bundle(s), either via `<script type="module">` and import statements or via your bundlers code splitting feature. This ensures that any errors encountered while loading the library do not impact other code on your site.
20
+
We recommend loading the `web-vitals` library in a separate script file from your site's main application bundle(s), either via `<script type="module">` and `import` statements or via your bundler's code splitting feature (for example, [rollup](https://rollupjs.org/tutorial/#code-splitting), [esbuild](https://esbuild.github.io/api/#splitting), and [webpack](https://webpack.js.org/guides/code-splitting/)). This ensures that any errors encountered while loading the library do not impact other code on your site.
21
21
22
22
If you do choose to include the `web-vitals` library code in your main application bundle—and you also need to support very old browsers—it's critical that you configure your bundler to transpile the `web-vitals` code along with the rest of you application JavaScript. This is important because most bundlers do not transpile `node_modules` by default.
0 commit comments