-
Notifications
You must be signed in to change notification settings - Fork 28k
/
Copy pathclient-component-renderer-logger.ts
62 lines (54 loc) · 1.64 KB
/
client-component-renderer-logger.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import type { AppPageModule } from './route-modules/app-page/module'
// Combined load times for loading client components
let clientComponentLoadStart = 0
let clientComponentLoadTimes = 0
let clientComponentLoadCount = 0
export function wrapClientComponentLoader(
ComponentMod: AppPageModule
): AppPageModule['__next_app__'] {
if (!('performance' in globalThis)) {
return ComponentMod.__next_app__
}
return {
require: (...args) => {
const startTime = performance.now()
if (clientComponentLoadStart === 0) {
clientComponentLoadStart = startTime
}
try {
clientComponentLoadCount += 1
return ComponentMod.__next_app__.require(...args)
} finally {
clientComponentLoadTimes += performance.now() - startTime
}
},
loadChunk: (...args) => {
const startTime = performance.now()
const result = ComponentMod.__next_app__.loadChunk(...args)
// Avoid wrapping `loadChunk`'s result in an extra promise in case something like React depends on its identity.
// We only need to know when it's settled.
result.finally(() => {
clientComponentLoadTimes += performance.now() - startTime
})
return result
},
}
}
export function getClientComponentLoaderMetrics(
options: { reset?: boolean } = {}
) {
const metrics =
clientComponentLoadStart === 0
? undefined
: {
clientComponentLoadStart,
clientComponentLoadTimes,
clientComponentLoadCount,
}
if (options.reset) {
clientComponentLoadStart = 0
clientComponentLoadTimes = 0
clientComponentLoadCount = 0
}
return metrics
}