|
17 | 17 | import {onBFCacheRestore} from './lib/bfcache.js'; |
18 | 18 | import {initMetric} from './lib/initMetric.js'; |
19 | 19 | import {observe} from './lib/observe.js'; |
20 | | -import {onHidden} from './lib/onHidden.js'; |
21 | 20 | import {bindReporter} from './lib/bindReporter.js'; |
22 | 21 | import {doubleRAF} from './lib/doubleRAF.js'; |
23 | | -import {whenActivated} from './lib/whenActivated.js'; |
| 22 | +import {onHidden} from './lib/onHidden.js'; |
| 23 | +import {runOnce} from './lib/runOnce.js'; |
24 | 24 | import {onFCP} from './onFCP.js'; |
25 | 25 | import {CLSMetric, CLSReportCallback, ReportOpts} from './types.js'; |
26 | 26 |
|
@@ -49,99 +49,88 @@ export const onCLS = (onReport: CLSReportCallback, opts?: ReportOpts) => { |
49 | 49 | // Set defaults |
50 | 50 | opts = opts || {}; |
51 | 51 |
|
52 | | - whenActivated(() => { |
53 | | - // https://web.dev/cls/#what-is-a-good-cls-score |
54 | | - const thresholds = [0.1, 0.25]; |
| 52 | + // Start monitoring FCP so we can only report CLS if FCP is also reported. |
| 53 | + // Note: this is done to match the current behavior of CrUX. |
| 54 | + onFCP( |
| 55 | + runOnce(() => { |
| 56 | + // https://web.dev/cls/#what-is-a-good-cls-score |
| 57 | + const thresholds = [0.1, 0.25]; |
55 | 58 |
|
56 | | - let metric = initMetric('CLS'); |
57 | | - let report: ReturnType<typeof bindReporter>; |
| 59 | + let metric = initMetric('CLS', 0); |
| 60 | + let report: ReturnType<typeof bindReporter>; |
58 | 61 |
|
59 | | - let fcpValue = -1; |
60 | | - let sessionValue = 0; |
61 | | - let sessionEntries: PerformanceEntry[] = []; |
62 | | - |
63 | | - const onReportWrapped: CLSReportCallback = (arg) => { |
64 | | - if (fcpValue > -1) { |
65 | | - onReport(arg); |
66 | | - } |
67 | | - }; |
| 62 | + let sessionValue = 0; |
| 63 | + let sessionEntries: PerformanceEntry[] = []; |
68 | 64 |
|
69 | | - // const handleEntries = (entries: Metric['entries']) => { |
70 | | - const handleEntries = (entries: LayoutShift[]) => { |
71 | | - entries.forEach((entry) => { |
72 | | - // Only count layout shifts without recent user input. |
73 | | - if (!entry.hadRecentInput) { |
74 | | - const firstSessionEntry = sessionEntries[0]; |
75 | | - const lastSessionEntry = sessionEntries[sessionEntries.length - 1]; |
| 65 | + // const handleEntries = (entries: Metric['entries']) => { |
| 66 | + const handleEntries = (entries: LayoutShift[]) => { |
| 67 | + entries.forEach((entry) => { |
| 68 | + // Only count layout shifts without recent user input. |
| 69 | + if (!entry.hadRecentInput) { |
| 70 | + const firstSessionEntry = sessionEntries[0]; |
| 71 | + const lastSessionEntry = sessionEntries[sessionEntries.length - 1]; |
76 | 72 |
|
77 | | - // If the entry occurred less than 1 second after the previous entry |
78 | | - // and less than 5 seconds after the first entry in the session, |
79 | | - // include the entry in the current session. Otherwise, start a new |
80 | | - // session. |
81 | | - if ( |
82 | | - sessionValue && |
83 | | - entry.startTime - lastSessionEntry.startTime < 1000 && |
84 | | - entry.startTime - firstSessionEntry.startTime < 5000 |
85 | | - ) { |
86 | | - sessionValue += entry.value; |
87 | | - sessionEntries.push(entry); |
88 | | - } else { |
89 | | - sessionValue = entry.value; |
90 | | - sessionEntries = [entry]; |
| 73 | + // If the entry occurred less than 1 second after the previous entry |
| 74 | + // and less than 5 seconds after the first entry in the session, |
| 75 | + // include the entry in the current session. Otherwise, start a new |
| 76 | + // session. |
| 77 | + if ( |
| 78 | + sessionValue && |
| 79 | + entry.startTime - lastSessionEntry.startTime < 1000 && |
| 80 | + entry.startTime - firstSessionEntry.startTime < 5000 |
| 81 | + ) { |
| 82 | + sessionValue += entry.value; |
| 83 | + sessionEntries.push(entry); |
| 84 | + } else { |
| 85 | + sessionValue = entry.value; |
| 86 | + sessionEntries = [entry]; |
| 87 | + } |
91 | 88 | } |
| 89 | + }); |
92 | 90 |
|
93 | | - // If the current session value is larger than the current CLS value, |
94 | | - // update CLS and the entries contributing to it. |
95 | | - if (sessionValue > metric.value) { |
96 | | - metric.value = sessionValue; |
97 | | - metric.entries = sessionEntries; |
98 | | - report(); |
99 | | - } |
100 | | - } |
101 | | - }); |
102 | | - }; |
103 | | - |
104 | | - const po = observe('layout-shift', handleEntries); |
105 | | - if (po) { |
106 | | - report = bindReporter( |
107 | | - onReportWrapped, |
108 | | - metric, |
109 | | - thresholds, |
110 | | - opts!.reportAllChanges |
111 | | - ); |
112 | | - |
113 | | - // Start monitoring FCP so we can only report CLS if FCP is also reported. |
114 | | - // Note: this is done to match the current behavior of CrUX. |
115 | | - // Also, if there have not been any layout shifts when FCP is dispatched, |
116 | | - // call "report" with a zero value |
117 | | - onFCP((fcpMetric) => { |
118 | | - fcpValue = fcpMetric.value; |
119 | | - if (metric.value < 0) { |
120 | | - metric.value = 0; |
| 91 | + // If the current session value is larger than the current CLS value, |
| 92 | + // update CLS and the entries contributing to it. |
| 93 | + if (sessionValue > metric.value) { |
| 94 | + metric.value = sessionValue; |
| 95 | + metric.entries = sessionEntries; |
121 | 96 | report(); |
122 | 97 | } |
123 | | - }); |
124 | | - |
125 | | - onHidden(() => { |
126 | | - handleEntries(po.takeRecords() as CLSMetric['entries']); |
127 | | - report(true); |
128 | | - }); |
| 98 | + }; |
129 | 99 |
|
130 | | - // Only report after a bfcache restore if the `PerformanceObserver` |
131 | | - // successfully registered. |
132 | | - onBFCacheRestore(() => { |
133 | | - sessionValue = 0; |
134 | | - fcpValue = -1; |
135 | | - metric = initMetric('CLS', 0); |
| 100 | + const po = observe('layout-shift', handleEntries); |
| 101 | + if (po) { |
136 | 102 | report = bindReporter( |
137 | | - onReportWrapped, |
| 103 | + onReport, |
138 | 104 | metric, |
139 | 105 | thresholds, |
140 | 106 | opts!.reportAllChanges |
141 | 107 | ); |
142 | 108 |
|
143 | | - doubleRAF(() => report()); |
144 | | - }); |
145 | | - } |
146 | | - }); |
| 109 | + onHidden(() => { |
| 110 | + handleEntries(po.takeRecords() as CLSMetric['entries']); |
| 111 | + report(true); |
| 112 | + }); |
| 113 | + |
| 114 | + // Only report after a bfcache restore if the `PerformanceObserver` |
| 115 | + // successfully registered. |
| 116 | + onBFCacheRestore(() => { |
| 117 | + sessionValue = 0; |
| 118 | + metric = initMetric('CLS', 0); |
| 119 | + report = bindReporter( |
| 120 | + onReport, |
| 121 | + metric, |
| 122 | + thresholds, |
| 123 | + opts!.reportAllChanges |
| 124 | + ); |
| 125 | + |
| 126 | + doubleRAF(() => report()); |
| 127 | + }); |
| 128 | + |
| 129 | + // Queue a task to report (if nothing else triggers a report first). |
| 130 | + // This allows CLS to be reported as soon as FCP fires when |
| 131 | + // `reportAllChanges` is true. |
| 132 | + setTimeout(report, 0); |
| 133 | + } |
| 134 | + }) |
| 135 | + ); |
147 | 136 | }; |
0 commit comments