Skip to content

Commit

Permalink
remove condition of onPerfEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Sep 8, 2021
1 parent 3ea62ac commit bc52d29
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 43 deletions.
75 changes: 35 additions & 40 deletions packages/next/client/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,39 +279,37 @@ export async function initNext(opts: { webpackHMR?: any } = {}) {
const { component: app, exports: mod } = appEntrypoint
CachedApp = app as AppComponent
const exportedReportWebVitals = mod && mod.reportWebVitals
if (exportedReportWebVitals) {
onPerfEntry = ({
id,
name,
startTime,
value,
duration,
entryType,
entries,
}): void => {
// Combines timestamp with random number for unique ID
const uniqueID: string = `${Date.now()}-${
Math.floor(Math.random() * (9e12 - 1)) + 1e12
}`
let perfStartEntry: string | undefined

if (entries && entries.length) {
perfStartEntry = entries[0].startTime
}
onPerfEntry = ({
id,
name,
startTime,
value,
duration,
entryType,
entries,
}: any): void => {
// Combines timestamp with random number for unique ID
const uniqueID: string = `${Date.now()}-${
Math.floor(Math.random() * (9e12 - 1)) + 1e12
}`
let perfStartEntry: string | undefined

if (entries && entries.length) {
perfStartEntry = entries[0].startTime
}

const webVitals: NextWebVitalsMetric = {
id: id || uniqueID,
name,
startTime: startTime || perfStartEntry,
value: value == null ? duration : value,
label:
entryType === 'mark' || entryType === 'measure'
? 'custom'
: 'web-vital',
}
exportedReportWebVitals?.(webVitals)
webVitalsCallbacks.forEach((callback) => callback(webVitals))
const webVitals: NextWebVitalsMetric = {
id: id || uniqueID,
name,
startTime: startTime || perfStartEntry,
value: value == null ? duration : value,
label:
entryType === 'mark' || entryType === 'measure'
? 'custom'
: 'web-vital',
}
exportedReportWebVitals?.(webVitals)
webVitalsCallbacks.forEach((callback) => callback(webVitals))
}

const pageEntrypoint =
Expand Down Expand Up @@ -554,9 +552,7 @@ function markHydrateComplete(): void {
)
performance.measure('Next.js-hydration', 'beforeRender', 'afterHydrate')

if (onPerfEntry) {
performance.getEntriesByName('Next.js-hydration').forEach(onPerfEntry)
}
performance.getEntriesByName('Next.js-hydration').forEach(onPerfEntry)
clearMarks()
}

Expand All @@ -577,12 +573,11 @@ function markRenderComplete(): void {
'beforeRender'
)
performance.measure('Next.js-render', 'beforeRender', 'afterRender')
if (onPerfEntry) {
performance.getEntriesByName('Next.js-render').forEach(onPerfEntry)
performance
.getEntriesByName('Next.js-route-change-to-render')
.forEach(onPerfEntry)
}
performance.getEntriesByName('Next.js-render').forEach(onPerfEntry)
performance
.getEntriesByName('Next.js-route-change-to-render')
.forEach(onPerfEntry)

clearMarks()
;['Next.js-route-change-to-render', 'Next.js-render'].forEach((measure) =>
performance.clearMeasures(measure)
Expand Down
1 change: 1 addition & 0 deletions packages/next/client/performance-relayer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global location */
import {
getCLS,
getFCP,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ export const webVitalsCallbacks = new Set<ReportWebVitalsCallback>()
export function useWebVitalsReport(callback: ReportWebVitalsCallback) {
// call on initial render, in a very early phase
useState(() => {
// typeof window !== 'undefined' &&
webVitalsCallbacks.add(callback)
})

useEffect(() => {
return () => {
webVitalsCallbacks.delete(callback)
}
})
}, [])
}
2 changes: 0 additions & 2 deletions test/integration/relay-analytics/test/hook-impl-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,3 @@ export default function MyApp({ Component, pageProps }) {
})
return <Component {...pageProps} />
}

export function reportWebVitals() {}

0 comments on commit bc52d29

Please sign in to comment.