File size: 1,537 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
declare global {
  interface Window {
    __NEXT_HMR_LATENCY_CB: ((latencyMs: number) => void) | undefined
  }
}

/**
 * Logs information about a completed HMR to the console, the server (via a
 * `client-hmr-latency` event), and to `self.__NEXT_HMR_LATENCY_CB` (a debugging
 * hook).
 *
 * @param hasUpdate Set this to `false` to avoid reporting the HMR event via a
 *   `client-hmr-latency` event or to `self.__NEXT_HMR_LATENCY_CB`. Used by
 *   turbopack when we must report a message to the browser console (because we
 *   already logged a "rebuilding" message), but it's not a real HMR, so we
 *   don't want to impact our telemetry.
 */
export default function reportHmrLatency(
  sendMessage: (message: string) => void,
  updatedModules: ReadonlyArray<string | number>,
  startMsSinceEpoch: number,
  endMsSinceEpoch: number,
  hasUpdate: boolean = true
) {
  const latencyMs = endMsSinceEpoch - startMsSinceEpoch
  console.log(`[Fast Refresh] done in ${latencyMs}ms`)
  if (!hasUpdate) {
    return
  }
  sendMessage(
    JSON.stringify({
      event: 'client-hmr-latency',
      id: window.__nextDevClientId,
      startTime: startMsSinceEpoch,
      endTime: endMsSinceEpoch,
      page: window.location.pathname,
      updatedModules,
      // Whether the page (tab) was hidden at the time the event occurred.
      // This can impact the accuracy of the event's timing.
      isPageHidden: document.visibilityState === 'hidden',
    })
  )
  if (self.__NEXT_HMR_LATENCY_CB) {
    self.__NEXT_HMR_LATENCY_CB(latencyMs)
  }
}