Skip to content

Commit e36393e

Browse files
Misc README updates (#616)
* Add note on dynamic imports to README * Add comment on batching * Fix #81 * Review feedback * Remove Event Listener warning * Upgrading doc update * Update docs/upgrading-to-v5.md Co-authored-by: Philip Walton <[email protected]> --------- Co-authored-by: Philip Walton <[email protected]>
1 parent 15a3773 commit e36393e

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

README.md

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ The library supports all of the [Core Web Vitals](https://web.dev/articles/vital
4343
- [First Contentful Paint (FCP)](https://web.dev/articles/fcp)
4444
- [Time to First Byte (TTFB)](https://web.dev/articles/ttfb)
4545

46-
<a name="installation"><a>
47-
<a name="load-the-library"><a>
46+
<a name="installation"></a>
47+
<a name="load-the-library"></a>
4848

4949
## Install and load the library
5050

51-
<a name="import-web-vitals-from-npm"><a>
51+
<a name="import-web-vitals-from-npm"></a>
5252

5353
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.
5454

@@ -75,16 +75,9 @@ To load the "standard" build, import modules from the `web-vitals` package in yo
7575

7676
```js
7777
import {onLCP, onINP, onCLS} from 'web-vitals';
78-
79-
onCLS(console.log);
80-
onINP(console.log);
81-
onLCP(console.log);
8278
```
8379

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-
<a name="attribution-build"><a>
80+
<a name="attribution-build"></a>
8881

8982
**2. The "attribution" build**
9083

@@ -97,15 +90,15 @@ The "attribution" build is slightly larger than the "standard" build (by about 1
9790
To load the "attribution" build, change any `import` statements that reference `web-vitals` to `web-vitals/attribution`:
9891

9992
```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';
10295
```
10396

10497
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.
10598

10699
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.
107100

108-
<a name="load-web-vitals-from-a-cdn"><a>
101+
<a name="load-web-vitals-from-a-cdn"></a>
109102

110103
### From a CDN
111104

@@ -128,6 +121,8 @@ _**Important!** The [unpkg.com](https://unpkg.com), [jsDelivr](https://www.jsdel
128121
</script>
129122
```
130123

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+
131126
**Load the "standard" build** _(using a classic script)_
132127

133128
```html
@@ -221,8 +216,7 @@ In other cases, a metric callback may be called more than once:
221216

222217
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).
223218

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.
226220
227221
This can be useful when debugging, but in general using `reportAllChanges` is not needed (or recommended) for measuring these metrics in production.
228222

@@ -268,9 +262,13 @@ The `sendToAnalytics()` function uses the [`navigator.sendBeacon()`](https://dev
268262
import {onCLS, onINP, onLCP} from 'web-vitals';
269263

270264
function sendToAnalytics(metric) {
271-
// Replace with whatever serialization method you prefer.
272-
// Note: JSON.stringify will likely include more data than you need.
273-
const body = JSON.stringify(metric);
265+
const body = JSON.stringify({
266+
name: metric.name,
267+
value: metric.value,
268+
id: metric.id,
269+
270+
// Include additional data as needed...
271+
});
274272

275273
// Use `navigator.sendBeacon()` to send the data, which supports
276274
// sending while the page is unloading.
@@ -415,7 +413,7 @@ addEventListener('visibilitychange', () => {
415413
> [!NOTE]
416414
> 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`.
417415
418-
<a name="bundle-versions"><a>
416+
<a name="bundle-versions"></a>
419417

420418
## Build options
421419

@@ -477,7 +475,7 @@ The following table lists all the builds distributed with the `web-vitals` packa
477475
</tr>
478476
</table>
479477

480-
<a name="which-build-is-right-for-you"><a>
478+
<a name="which-build-is-right-for-you"></a>
481479

482480
### Which build is right for you?
483481

docs/upgrading-to-v5.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ All of the [builds](README#build-options) in `web-vitals` v5 use only [Baseline
1717

1818
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:
1919

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.
2121

2222
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.
2323

0 commit comments

Comments
 (0)