-
Notifications
You must be signed in to change notification settings - Fork 103
/
vitals.js
390 lines (364 loc) · 13 KB
/
vitals.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
Copyright 2020 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
(async () => {
const src = chrome.runtime.getURL('src/browser_action/web-vitals.js');
const webVitals = await import(src);
let overlayClosedForSession = false;
let latestCLS = {};
let enableLogging = localStorage.getItem('web-vitals-extension-debug')=='TRUE';
let enableUserTiming = localStorage.getItem('web-vitals-extension-user-timing')=='TRUE';
// Core Web Vitals thresholds
const LCP_THRESHOLD = 2500;
const FID_THRESHOLD = 100;
const INP_THRESHOLD = 200;
const CLS_THRESHOLD = 0.1;
// CLS update frequency
const DEBOUNCE_DELAY = 500;
// Registry for badge metrics
const badgeMetrics = initializeMetrics();
function initializeMetrics() {
let metricsState = localStorage.getItem('web-vitals-extension-metrics');
if (metricsState) {
metricsState = JSON.parse(metricsState);
if (metricsState.navigationStart == performance.timing.navigationStart) {
return metricsState;
}
}
// Create a fresh state.
// Default all metric values to null.
return {
lcp: {
value: null,
pass: true,
},
cls: {
value: null,
pass: true,
},
fid: {
value: null,
pass: true,
},
inp: {
value: null,
pass: true,
},
// This is used to distinguish between navigations.
// TODO: Is there a cleaner way?
navigationStart: performance.timing.navigationStart
};
}
/**
* Very simple classifier for metrics values
* @param {Object} metrics
* @return {String} overall metrics score
*/
function scoreBadgeMetrics(metrics) {
// Note: overallScore is treated as a string rather than
// a boolean to give us the flexibility of introducing a
// 'NEEDS IMPROVEMENT' option here in the future.
let overallScore = 'GOOD';
if (metrics.lcp.value > LCP_THRESHOLD) {
overallScore = 'POOR';
metrics.lcp.pass = false;
}
if (metrics.cls.value > CLS_THRESHOLD) {
overallScore = 'POOR';
metrics.cls.pass = false;
}
if (metrics.fid.value > FID_THRESHOLD) {
overallScore = 'POOR';
metrics.fid.pass = false;
}
if (metrics.inp.value > INP_THRESHOLD) {
// INP does not affect overall score
metrics.inp.pass = false;
}
return overallScore;
}
/**
*
* Draw or update the HUD overlay to the page
* @param {Object} metrics
* @param {Number} tabId
*/
function drawOverlay(metrics, tabId) {
let tabLoadedInBackground = false;
const key = tabId.toString();
localStorage.setItem('web-vitals-extension-metrics', JSON.stringify(metrics));
// Check if tab was loaded in background
chrome.storage.local.get(key, (result) => {
tabLoadedInBackground = result[key];
});
// Check for preferences set in options
chrome.storage.sync.get({
enableOverlay: false,
debug: false,
userTiming: false,
}, ({
enableOverlay, debug, userTiming,
}) => {
if (enableOverlay === true && overlayClosedForSession == false) {
// Overlay
const overlayElement = document.getElementById('web-vitals-extension-overlay');
if (overlayElement === null) {
const overlayElement = document.createElement('div');
overlayElement.id = 'web-vitals-extension-overlay';
overlayElement.classList.add('web-vitals-chrome-extension');
overlayElement.innerHTML = buildOverlayTemplate(metrics, tabLoadedInBackground);
document.body.appendChild(overlayElement);
} else {
overlayElement.innerHTML = buildOverlayTemplate(metrics, tabLoadedInBackground);
}
// Overlay close button
const overlayClose = document.getElementById('web-vitals-close');
if (overlayClose === null) {
const overlayClose = document.createElement('button');
overlayClose.innerText = 'Close';
overlayClose.id = 'web-vitals-close';
overlayClose.className = 'lh-overlay-close';
overlayClose.addEventListener('click', () => {
overlayElement.remove();
overlayClose.remove();
overlayClosedForSession = true;
});
document.body.appendChild(overlayClose);
} else {
overlayClose.addEventListener('click', () => {
overlayElement.remove();
overlayClose.remove();
overlayClosedForSession = true;
});
}
}
if (debug) {
localStorage.setItem('web-vitals-extension-debug', 'TRUE');
enableLogging = true;
} else {
localStorage.removeItem('web-vitals-extension-debug');
enableLogging = false;
}
if (debug) {
localStorage.setItem('web-vitals-extension-user-timing', 'TRUE');
enableUserTiming = true;
} else {
localStorage.removeItem('web-vitals-extension-user-timing');
enableUserTiming = false;
}
});
}
/**
* Return a short (host) and full URL for the measured page
* @return {Object}
*/
function getURL() {
const url = document.location.href;
const shortURL = document.location.origin;
return {shortURL, url};
}
/**
* Return a short timestamp (HH:MM:SS) for current time
* @return {String}
*/
function getTimestamp() {
const date = new Date();
return date.toLocaleTimeString('en-US', {hourCycle: 'h23'});
}
/**
*
* Broadcasts metrics updates using chrome.runtime(), triggering
* updates to the badge. Will also update the overlay if this option
* is enabled.
* @param {String} metricName
* @param {Object} body
*/
function broadcastMetricsUpdates(metricName, body) {
if (metricName === undefined || badgeMetrics === undefined) {
return;
}
if (enableLogging) {
console.log('[Web Vitals]', body.name, body.value.toFixed(2), body);
}
if (enableUserTiming) {
addUserTimings(body);
}
badgeMetrics[metricName].value = body.value;
badgeMetrics.location = getURL();
badgeMetrics.timestamp = getTimestamp();
const passes = scoreBadgeMetrics(badgeMetrics);
// Broadcast metrics updates for badging
chrome.runtime.sendMessage(
{
passesAllThresholds: passes,
metrics: badgeMetrics,
},
(response) => drawOverlay(badgeMetrics, response.tabId),
);
}
function addUserTimings(metric) {
switch (metric.name) {
case "LCP":
// LCP has a loadTime/renderTime (startTime), but not a duration.
// Could visualize relative to timeOrigin, or from loadTime -> renderTime.
// Skip for now.
break;
case "CLS":
// CLS has a startTime, but not a duration.
// Could visualize the time between rendering tasks (Commit-to-Commit).
// Skip for now.
break;
case "INP":
if (metric.entries.length > 0) {
const inpEntry = metric.entries[0];
// RenderTime is an estimate, because duration is rounded, and may get rounded keydown
// In rare cases it can be less than processingEnd and that breaks performance.measure().
// Lets make sure its at least 4ms in those cases so you can just barely see it.
const presentationTime = inpEntry.startTime + inpEntry.duration;
const adjustedPresentationTime = Math.max(inpEntry.processingEnd + 4, presentationTime);
performance.measure(`[Web Vitals] INP.duration (${inpEntry.name})`, {
start: inpEntry.startTime,
end: presentationTime,
});
performance.measure(`[Web Vitals] INP.inputDelay (${inpEntry.name})`, {
start: inpEntry.startTime,
end: inpEntry.processingStart,
});
performance.measure(`[Web Vitals] INP.processingTime (${inpEntry.name})`, {
start: inpEntry.processingStart,
end: inpEntry.processingEnd,
});
performance.measure(`[Web Vitals] INP.presentationDelay (${inpEntry.name})`, {
start: inpEntry.processingEnd,
end: adjustedPresentationTime,
});
}
break;
case "FID":
if (metric.entries.length > 0) {
const fidEntry = metric.entries[0]
performance.measure(`[Web Vitals Extension] FID (${fidEntry.name})`, {
start: fidEntry.startTime,
end: fidEntry.processingStart,
});
}
}
}
/**
* Broadcasts the latest CLS value
*/
function broadcastCLS() {
broadcastMetricsUpdates('cls', latestCLS);
}
/**
* Debounces the broadcast of CLS values for stability.
* broadcastCLS is invoked on the trailing edge of the
* DEBOUNCE_DELAY timeout if invoked more than once during
* the wait timeout.
*/
let debouncedCLSBroadcast = () => {};
if (typeof _ !== 'undefined') {
debouncedCLSBroadcast = _.debounce(broadcastCLS, DEBOUNCE_DELAY, {
leading: true,
trailing: true,
maxWait: 1000});
}
/**
*
* Fetches Web Vitals metrics via WebVitals.js
*/
function fetchWebPerfMetrics() {
// web-vitals.js doesn't have a way to remove previous listeners, so we'll save whether
// we've already installed the listeners before installing them again.
// See https://github.com/GoogleChrome/web-vitals/issues/55.
if (self._hasInstalledPerfMetrics) return;
self._hasInstalledPerfMetrics = true;
webVitals.getCLS((metric) => {
// As CLS values can fire frequently in the case
// of animations or highly-dynamic content, we
// debounce the broadcast of the metric.
latestCLS = metric;
debouncedCLSBroadcast();
}, true);
webVitals.getLCP((metric) => {
broadcastMetricsUpdates('lcp', metric);
}, true);
webVitals.getFID((metric) => {
broadcastMetricsUpdates('fid', metric);
}, true);
webVitals.getINP((metric) => {
broadcastMetricsUpdates('inp', metric);
}, true);
}
/**
* Build a template of metrics
* @param {Object} metrics The metrics
* @param {Boolean} tabLoadedInBackground
* @return {String} a populated template of metrics
*/
function buildOverlayTemplate(metrics, tabLoadedInBackground) {
return `
<div id="lh-overlay-container" class="lh-unset lh-root lh-vars dark" style="display: block;">
<div class="lh-overlay">
<div class="lh-audit-group lh-audit-group--metrics">
<div class="lh-audit-group__header">
<span class="lh-audit-group__title">Metrics</span>
</div>
<div class="lh-columns">
<div class="lh-column">
<div class="lh-metric lh-metric--${metrics.lcp.pass ? 'pass':'fail'}">
<div class="lh-metric__innerwrap">
<div>
<span class="lh-metric__title">Largest Contentful Paint</span>
${tabLoadedInBackground ? '<span class="lh-metric__subtitle">Value inflated as tab was loaded in background</span>' : ''}
</div>
<div class="lh-metric__value">${((metrics.lcp.value || 0)/1000).toFixed(2)} s</div>
</div>
</div>
<div class="lh-metric lh-metric--${metrics.cls.pass ? 'pass':'fail'}">
<div class="lh-metric__innerwrap">
<span class="lh-metric__title">Cumulative Layout Shift</span>
<div class="lh-metric__value">${(metrics.cls.value || 0).toFixed(3)}</div>
</div>
</div>
<div class="lh-metric lh-metric--${metrics.fid.pass ? 'pass':'fail'} lh-metric--${metrics.fid.value === null ? 'waiting' : 'ready'}">
<div class="lh-metric__innerwrap">
<span class="lh-metric__title">
First Input Delay
<span class="lh-metric-state">${metrics.fid.value === null ? '(waiting for input)' : ''}</span>
</span>
<div class="lh-metric__value">${
metrics.fid.value === null ? '' :
`${metrics.fid.value.toFixed(2)} ms`
}</div>
</div>
</div>
<div class="lh-metric lh-metric--${metrics.inp.pass ? 'pass':'fail'} lh-metric--${metrics.inp.value === null ? 'waiting' : 'ready'}">
<div class="lh-metric__innerwrap">
<span class="lh-metric__title">
Interaction to Next Paint*
<span class="lh-metric-state">${metrics.inp.value === null ? '(waiting for input)' : ''}</span>
</span>
<div class="lh-metric__value">${
metrics.inp.value === null ? '' :
`${metrics.inp.value.toFixed(2)} ms`
}</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>`;
}
fetchWebPerfMetrics();
})();