-
-
Notifications
You must be signed in to change notification settings - Fork 27k
Add performance relayer + documentation (web-vitals) #9116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ianschmitz
merged 6 commits into
facebook:master
from
housseindjirdeh:add-performance-relayer
Jun 11, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
715b5d6
adds web vitals lib + documentation
housseindjirdeh de2187d
removes console log and fixes typo
housseindjirdeh c8a84ba
moves web-vitals lib to template dependency
housseindjirdeh b812d97
adds web-vitals to typescript template
housseindjirdeh c556b2b
Merge branch 'master' into add-performance-relayer
mrmckeb ba4ad05
Add web-vitals to dependency check
ianschmitz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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); | ||
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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) => { | ||
ianschmitz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.