Skip to content

Commit

Permalink
adjust code
Browse files Browse the repository at this point in the history
  • Loading branch information
Timer committed Oct 16, 2020
1 parent a1c8328 commit 1204206
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
4 changes: 1 addition & 3 deletions packages/next/client/index.tsx
Expand Up @@ -743,9 +743,7 @@ function Root({
// We should ask to measure the Web Vitals after rendering completes so we
// don't cause any hydration delay:
React.useEffect(() => {
if (onPerfEntry) {
measureWebVitals(onPerfEntry)
}
measureWebVitals(onPerfEntry)
}, [])
return children as React.ReactElement
}
31 changes: 25 additions & 6 deletions packages/next/client/performance-relayer.ts
Expand Up @@ -4,13 +4,32 @@ import {
getFID,
getLCP,
getTTFB,
Metric,
ReportHandler,
} from 'web-vitals'

export default (onPerfEntry: ReportHandler) => {
getCLS(onPerfEntry)
getFID(onPerfEntry)
getFCP(onPerfEntry)
getLCP(onPerfEntry)
getTTFB(onPerfEntry)
let isRegistered = false
let userReportHandler: ReportHandler | undefined

function onReport(metric: Metric) {
if (userReportHandler) {
userReportHandler(metric)
}
}

export default (onPerfEntry?: ReportHandler) => {
// Update function if it changes:
userReportHandler = onPerfEntry

// Only register listeners once:
if (isRegistered) {
return
}
isRegistered = true

getCLS(onReport)
getFID(onReport)
getFCP(onReport)
getLCP(onReport)
getTTFB(onReport)
}

0 comments on commit 1204206

Please sign in to comment.