-
-
Notifications
You must be signed in to change notification settings - Fork 26.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add performance relayer + documentation (web-vitals) (#9116)
Co-authored-by: Brody McKee <mrmckeb@users.noreply.github.com> Co-authored-by: Ian Schmitz <ianschmitz@gmail.com>
- Loading branch information
1 parent
a2dac9e
commit 8fda779
Showing
10 changed files
with
114 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- | ||
id: measuring-performance | ||
title: Measuring Performance | ||
--- | ||
|
||
By default, Create React App includes a performance relayer that allows you to measure and analyze | ||
the performance of your application using different metrics. | ||
|
||
To measure any of the supported metrics, you only need to pass a function into the `reportWebVitals` | ||
function in `index.js`: | ||
|
||
```js | ||
reportWebVitals(console.log); | ||
``` | ||
|
||
This function is fired when the final values for any of the metrics have finished calculating on the | ||
page. You can use it to log any of the results to the console or send to a particular endpoint. | ||
|
||
## Web Vitals | ||
|
||
[Web Vitals](https://web.dev/vitals/) are a set of useful metrics that aim to capture the user | ||
experience of a web page. In Create React App, a third-party library is used to measure these | ||
metrics ([web-vitals](https://github.com/GoogleChrome/web-vitals)). | ||
|
||
To understand more about the object returned to the function when a metric value is calculated, | ||
refer to the [documentation](https://github.com/GoogleChrome/web-vitals/#types). The [Browser | ||
Support](https://github.com/GoogleChrome/web-vitals/#browser-support) section also explains which browsers are supported. | ||
|
||
## Sending results to analytics | ||
|
||
With the `reportWebVitals` function, you can send any of results to an analytics endpoint to measure and track real user performance on your site. For example: | ||
|
||
```js | ||
function sendToAnalytics(metric) { | ||
const body = JSON.stringify(metric); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
const url = 'https://example.com/analytics'; | ||
|
||
// Use `navigator.sendBeacon()` if available, falling back to `fetch()` | ||
if (navigator.sendBeacon) { | ||
navigator.sendBeacon(url, body); | ||
} else { | ||
fetch(url, { body, method: 'POST', keepalive: true }); | ||
} | ||
} | ||
|
||
reportWebVitals(sendToAnalytics); | ||
``` | ||
|
||
> **Note:** If you use Google Analytics, use the `id` value to make it easier to construct metric distributions manually (to calculate percentiles, etc…). | ||
> | ||
> ```js | ||
> function sendToAnalytics({id, name, value}) { | ||
> ga('send', 'event', { | ||
> eventCategory: 'Web Vitals', | ||
> eventAction: name, | ||
> eventValue: Math.round(name === 'CLS' ? value * 1000 : value), // values must be integers | ||
> eventLabel: id, // id unique to current page load | ||
> nonInteraction: true, // avoids affecting bounce rate | ||
> }); | ||
> } | ||
> | ||
> reportWebVitals(sendToAnalytics); | ||
> ``` | ||
> | ||
> Read more about sending results to Google Analytics [here](https://github.com/GoogleChrome/web-vitals#send-the-results-to-google-analytics). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/cra-template-typescript/template/src/reportWebVitals.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { ReportHandler } from 'web-vitals'; | ||
|
||
const reportWebVitals = (onPerfEntry?: ReportHandler) => { | ||
if (onPerfEntry && onPerfEntry instanceof Function) { | ||
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { | ||
getCLS(onPerfEntry); | ||
getFID(onPerfEntry); | ||
getFCP(onPerfEntry); | ||
getLCP(onPerfEntry); | ||
getTTFB(onPerfEntry); | ||
}); | ||
} | ||
} | ||
|
||
export default reportWebVitals; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const reportWebVitals = (onPerfEntry) => { | ||
if (onPerfEntry && onPerfEntry instanceof Function) { | ||
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { | ||
getCLS(onPerfEntry); | ||
getFID(onPerfEntry); | ||
getFCP(onPerfEntry); | ||
getLCP(onPerfEntry); | ||
getTTFB(onPerfEntry); | ||
}); | ||
} | ||
} | ||
|
||
export default reportWebVitals; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JSON.stringify(metric)
may throwConverting circular structure to JSON
error (getLCP, see attached screenshot below), consider add try-catch wrapper to it?