Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton committed Sep 29, 2024
1 parent cdb2336 commit f944e86
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,6 @@ In addition to using the `id` field to group multiple deltas for the same metric

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

The `sendToAnalytics()` function uses the [`navigator.sendBeacon()`](https://developer.mozilla.org/docs/Web/API/Navigator/sendBeacon) method (if available), but falls back to the [`fetch()`](https://developer.mozilla.org/docs/Web/API/Fetch_API) API when not.

```js
import {onCLS, onINP, onLCP} from 'web-vitals';

Expand All @@ -272,9 +270,9 @@ function sendToAnalytics(metric) {
// Note: JSON.stringify will likely include more data than you need.
const body = JSON.stringify(metric);

// Use `navigator.sendBeacon()` if available, falling back to `fetch()`.
navigator?.sendBeacon('/analytics', body) ||
fetch('/analytics', {body, method: 'POST', keepalive: true});
// Use `navigator.sendBeacon()` to send the data, which supports
// sending while the page is unloading.
navigator.sendBeacon('/analytics', body);
}

onCLS(sendToAnalytics);
Expand Down Expand Up @@ -392,9 +390,9 @@ function flushQueue() {
// Note: JSON.stringify will likely include more data than you need.
const body = JSON.stringify([...queue]);

// Use `navigator.sendBeacon()` if available, falling back to `fetch()`.
(navigator.sendBeacon && navigator.sendBeacon('/analytics', body)) ||
fetch('/analytics', {body, method: 'POST', keepalive: true});
// Use `navigator.sendBeacon()` to send the data, which supports
// sending while the page is unloading.
navigator.sendBeacon('/analytics', body);

queue.clear();
}
Expand Down

0 comments on commit f944e86

Please sign in to comment.