Skip to content

Commit

Permalink
lazy load web-vitals and web-vitals/attribution
Browse files Browse the repository at this point in the history
  • Loading branch information
kyliau committed Sep 28, 2022
1 parent 062bb1e commit 6ca72f2
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/next/client/performance-relayer.ts
Expand Up @@ -69,7 +69,7 @@ function onReport(metric: Metric): void {
}
}

export default (onPerfEntry?: ReportCallback): void => {
export default async (onPerfEntry?: ReportCallback): Promise<void> => {
// Update function if it changes:
userReportHandler = onPerfEntry

Expand All @@ -80,10 +80,16 @@ export default (onPerfEntry?: ReportCallback): void => {
isRegistered = true

for (const webVital of WEB_VITALS) {
const m = config?.attributions.includes(webVital)
? require('next/dist/compiled/web-vitals-attribution')
: require('next/dist/compiled/web-vitals')
m[`on${webVital}`](onReport)
try {
const m: any = config?.attributions.includes(webVital)
? // @ts-ignore module available at runtime, see taskfile.js
await import('../compiled/web-vitals-attribution')
: // @ts-ignore module available at runtime, see taskfile.js
await import('../compiled/web-vitals')
m[`on${webVital}`](onReport)
} catch {
// Do nothing if the module fails to load
}
}
}

Expand Down

0 comments on commit 6ca72f2

Please sign in to comment.