-
Notifications
You must be signed in to change notification settings - Fork 26k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental: hook version of reportWebVitals (#28769)
### Experimental Introduce `next/vitals` and `useExperimentalWebVitalsReport` API which is not limited by `_app`. `pages/index.js` ```jsx import { useExperimentalWebVitalsReport } from 'next/vitals' export default function Index() { useExperimentalWebVitalsReport((metric) => { // handle metric... }) return 'sup' } ```
- Loading branch information
Showing
10 changed files
with
176 additions
and
42 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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/* global location */ | ||
import { | ||
getCLS, | ||
getFCP, | ||
|
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 @@ | ||
export * from './dist/vitals/index' |
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 @@ | ||
module.exports = require('./dist/vitals/index') |
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 @@ | ||
export { useExperimentalWebVitalsReport } from './vitals' |
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,33 @@ | ||
import { useEffect, useRef } from 'react' | ||
import { NextWebVitalsMetric } from '../pages/_app' | ||
|
||
type ReportWebVitalsCallback = (webVitals: NextWebVitalsMetric) => any | ||
export const webVitalsCallbacks = new Set<ReportWebVitalsCallback>() | ||
const metrics: NextWebVitalsMetric[] = [] | ||
|
||
export function trackWebVitalMetric(metric: NextWebVitalsMetric) { | ||
metrics.push(metric) | ||
webVitalsCallbacks.forEach((callback) => callback(metric)) | ||
} | ||
|
||
export function useExperimentalWebVitalsReport( | ||
callback: ReportWebVitalsCallback | ||
) { | ||
const metricIndexRef = useRef(0) | ||
|
||
useEffect(() => { | ||
// Flush calculated metrics | ||
const reportMetric = (metric: NextWebVitalsMetric) => { | ||
callback(metric) | ||
metricIndexRef.current = metrics.length | ||
} | ||
for (let i = metricIndexRef.current; i < metrics.length; i++) { | ||
reportMetric(metrics[i]) | ||
} | ||
|
||
webVitalsCallbacks.add(reportMetric) | ||
return () => { | ||
webVitalsCallbacks.delete(reportMetric) | ||
} | ||
}, [callback]) | ||
} |
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