-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathdetect.js
578 lines (521 loc) · 16.1 KB
/
detect.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/**
* @typedef {import("web-vitals").LCPMetric} LCPMetric
* @typedef {import("./types.ts").ElementData} ElementData
* @typedef {import("./types.ts").URLMetric} URLMetric
* @typedef {import("./types.ts").URLMetricGroupStatus} URLMetricGroupStatus
* @typedef {import("./types.ts").Extension} Extension
* @typedef {import("./types.ts").ExtendedRootData} ExtendedRootData
* @typedef {import("./types.ts").ExtendedElementData} ExtendedElementData
*/
const win = window;
const doc = win.document;
const consoleLogPrefix = '[Optimization Detective]';
const storageLockTimeSessionKey = 'odStorageLockTime';
/**
* Checks whether storage is locked.
*
* @param {number} currentTime - Current time in milliseconds.
* @param {number} storageLockTTL - Storage lock TTL in seconds.
* @return {boolean} Whether storage is locked.
*/
function isStorageLocked( currentTime, storageLockTTL ) {
if ( storageLockTTL === 0 ) {
return false;
}
try {
const storageLockTime = parseInt(
sessionStorage.getItem( storageLockTimeSessionKey )
);
return (
! isNaN( storageLockTime ) &&
currentTime < storageLockTime + storageLockTTL * 1000
);
} catch ( e ) {
return false;
}
}
/**
* Sets the storage lock.
*
* @param {number} currentTime - Current time in milliseconds.
*/
function setStorageLock( currentTime ) {
try {
sessionStorage.setItem(
storageLockTimeSessionKey,
String( currentTime )
);
} catch ( e ) {}
}
/**
* Logs a message.
*
* @param {...*} message
*/
function log( ...message ) {
// eslint-disable-next-line no-console
console.log( consoleLogPrefix, ...message );
}
/**
* Logs a warning.
*
* @param {...*} message
*/
function warn( ...message ) {
// eslint-disable-next-line no-console
console.warn( consoleLogPrefix, ...message );
}
/**
* Logs an error.
*
* @param {...*} message
*/
function error( ...message ) {
// eslint-disable-next-line no-console
console.error( consoleLogPrefix, ...message );
}
/**
* Checks whether the URL Metric(s) for the provided viewport width is needed.
*
* @param {number} viewportWidth - Current viewport width.
* @param {URLMetricGroupStatus[]} urlMetricGroupStatuses - Viewport group statuses.
* @return {boolean} Whether URL Metrics are needed.
*/
function isViewportNeeded( viewportWidth, urlMetricGroupStatuses ) {
let lastWasLacking = false;
for ( const { minimumViewportWidth, complete } of urlMetricGroupStatuses ) {
if ( viewportWidth >= minimumViewportWidth ) {
lastWasLacking = ! complete;
} else {
break;
}
}
return lastWasLacking;
}
/**
* Gets the current time in milliseconds.
*
* @return {number} Current time in milliseconds.
*/
function getCurrentTime() {
return Date.now();
}
/**
* Recursively freezes an object to prevent mutation.
*
* @param {Object} obj Object to recursively freeze.
*/
function recursiveFreeze( obj ) {
for ( const prop of Object.getOwnPropertyNames( obj ) ) {
const value = obj[ prop ];
if ( null !== value && typeof value === 'object' ) {
recursiveFreeze( value );
}
}
Object.freeze( obj );
}
// This needs to be captured early in case the user later resizes the window.
const viewport = {
width: win.innerWidth,
height: win.innerHeight,
};
/**
* URL Metric being assembled for submission.
*
* @type {URLMetric}
*/
let urlMetric;
/**
* Reserved root property keys.
*
* @see {URLMetric}
* @see {ExtendedElementData}
* @type {Set<string>}
*/
const reservedRootPropertyKeys = new Set( [ 'url', 'viewport', 'elements' ] );
/**
* Gets root URL Metric data.
*
* @return {URLMetric} URL Metric.
*/
function getRootData() {
const immutableUrlMetric = structuredClone( urlMetric );
recursiveFreeze( immutableUrlMetric );
return immutableUrlMetric;
}
/**
* Extends root URL Metric data.
*
* @param {ExtendedRootData} properties
*/
function extendRootData( properties ) {
for ( const key of Object.getOwnPropertyNames( properties ) ) {
if ( reservedRootPropertyKeys.has( key ) ) {
throw new Error( `Disallowed setting of key '${ key }' on root.` );
}
}
Object.assign( urlMetric, properties );
}
/**
* Mapping of XPath to element data.
*
* @type {Map<string, ElementData>}
*/
const elementsByXPath = new Map();
/**
* Reserved element property keys.
*
* @see {ElementData}
* @see {ExtendedRootData}
* @type {Set<string>}
*/
const reservedElementPropertyKeys = new Set( [
'isLCP',
'isLCPCandidate',
'xpath',
'intersectionRatio',
'intersectionRect',
'boundingClientRect',
] );
/**
* Gets element data.
*
* @param {string} xpath XPath.
* @return {ElementData|null} Element data, or null if no element for the XPath exists.
*/
function getElementData( xpath ) {
const elementData = elementsByXPath.get( xpath );
if ( elementData ) {
const cloned = structuredClone( elementData );
recursiveFreeze( cloned );
return cloned;
}
return null;
}
/**
* Extends element data.
*
* @param {string} xpath XPath.
* @param {ExtendedElementData} properties Properties.
*/
function extendElementData( xpath, properties ) {
if ( ! elementsByXPath.has( xpath ) ) {
throw new Error( `Unknown element with XPath: ${ xpath }` );
}
for ( const key of Object.getOwnPropertyNames( properties ) ) {
if ( reservedElementPropertyKeys.has( key ) ) {
throw new Error(
`Disallowed setting of key '${ key }' on element.`
);
}
}
const elementData = elementsByXPath.get( xpath );
Object.assign( elementData, properties );
}
/**
* Detects the LCP element, loaded images, client viewport and store for future optimizations.
*
* @param {Object} args Args.
* @param {string[]} args.extensionModuleUrls URLs for extension script modules to import.
* @param {number} args.minViewportAspectRatio Minimum aspect ratio allowed for the viewport.
* @param {number} args.maxViewportAspectRatio Maximum aspect ratio allowed for the viewport.
* @param {boolean} args.isDebug Whether to show debug messages.
* @param {string} args.restApiEndpoint URL for where to send the detection data.
* @param {string} args.currentETag Current ETag.
* @param {string} args.currentUrl Current URL.
* @param {string} args.urlMetricSlug Slug for URL Metric.
* @param {number|null} args.cachePurgePostId Cache purge post ID.
* @param {string} args.urlMetricHMAC HMAC for URL Metric storage.
* @param {URLMetricGroupStatus[]} args.urlMetricGroupStatuses URL Metric group statuses.
* @param {number} args.storageLockTTL The TTL (in seconds) for the URL Metric storage lock.
* @param {string} args.webVitalsLibrarySrc The URL for the web-vitals library.
* @param {Object} [args.urlMetricGroupCollection] URL Metric group collection, when in debug mode.
*/
export default async function detect( {
minViewportAspectRatio,
maxViewportAspectRatio,
isDebug,
extensionModuleUrls,
restApiEndpoint,
currentETag,
currentUrl,
urlMetricSlug,
cachePurgePostId,
urlMetricHMAC,
urlMetricGroupStatuses,
storageLockTTL,
webVitalsLibrarySrc,
urlMetricGroupCollection,
} ) {
if ( isDebug ) {
log( 'Stored URL Metric group collection:', urlMetricGroupCollection );
}
// Abort if the current viewport is not among those which need URL Metrics.
if ( ! isViewportNeeded( win.innerWidth, urlMetricGroupStatuses ) ) {
if ( isDebug ) {
log( 'No need for URL Metrics from the current viewport.' );
}
return;
}
// Abort if the viewport aspect ratio is not in a common range.
const aspectRatio = win.innerWidth / win.innerHeight;
if (
aspectRatio < minViewportAspectRatio ||
aspectRatio > maxViewportAspectRatio
) {
if ( isDebug ) {
warn(
`Viewport aspect ratio (${ aspectRatio }) is not in the accepted range of ${ minViewportAspectRatio } to ${ maxViewportAspectRatio }.`
);
}
return;
}
// Ensure the DOM is loaded (although it surely already is since we're executing in a module).
await new Promise( ( resolve ) => {
if ( doc.readyState !== 'loading' ) {
resolve();
} else {
doc.addEventListener( 'DOMContentLoaded', resolve, { once: true } );
}
} );
// Wait until the resources on the page have fully loaded.
await new Promise( ( resolve ) => {
if ( doc.readyState === 'complete' ) {
resolve();
} else {
win.addEventListener( 'load', resolve, { once: true } );
}
} );
// Wait yet further until idle.
if ( typeof requestIdleCallback === 'function' ) {
await new Promise( ( resolve ) => {
requestIdleCallback( resolve );
} );
}
// TODO: Does this make sense here? Should it be moved up above the isViewportNeeded condition?
// As an alternative to this, the od_print_detection_script() function can short-circuit if the
// od_is_url_metric_storage_locked() function returns true. However, the downside with that is page caching could
// result in metrics missed from being gathered when a user navigates around a site and primes the page cache.
if ( isStorageLocked( getCurrentTime(), storageLockTTL ) ) {
if ( isDebug ) {
warn( 'Aborted detection due to storage being locked.' );
}
return;
}
// TODO: Does this make sense here?
// Prevent detection when page is not scrolled to the initial viewport.
if ( doc.documentElement.scrollTop > 0 ) {
if ( isDebug ) {
warn(
'Aborted detection since initial scroll position of page is not at the top.'
);
}
return;
}
if ( isDebug ) {
log( 'Proceeding with detection' );
}
/** @type {Map<string, Extension>} */
const extensions = new Map();
for ( const extensionModuleUrl of extensionModuleUrls ) {
try {
/** @type {Extension} */
const extension = await import( extensionModuleUrl );
extensions.set( extensionModuleUrl, extension );
// TODO: There should to be a way to pass additional args into the module. Perhaps extensionModuleUrls should be a mapping of URLs to args. It's important to pass webVitalsLibrarySrc to the extension so that onLCP, onCLS, or onINP can be obtained.
if ( extension.initialize instanceof Function ) {
extension.initialize( { isDebug } );
}
} catch ( err ) {
error(
`Failed to initialize extension '${ extensionModuleUrl }':`,
err
);
}
}
const breadcrumbedElements = doc.body.querySelectorAll( '[data-od-xpath]' );
/** @type {Map<Element, string>} */
const breadcrumbedElementsMap = new Map(
[ ...breadcrumbedElements ].map(
/**
* @param {HTMLElement} element
* @return {[HTMLElement, string]} Tuple of element and its XPath.
*/
( element ) => [ element, element.dataset.odXpath ]
)
);
/** @type {IntersectionObserverEntry[]} */
const elementIntersections = [];
/** @type {?IntersectionObserver} */
let intersectionObserver;
function disconnectIntersectionObserver() {
if ( intersectionObserver instanceof IntersectionObserver ) {
intersectionObserver.disconnect();
win.removeEventListener( 'scroll', disconnectIntersectionObserver ); // Clean up, even though this is registered with once:true.
}
}
// Wait for the intersection observer to report back on the initially-visible elements.
// Note that the first callback will include _all_ observed entries per <https://github.com/w3c/IntersectionObserver/issues/476>.
if ( breadcrumbedElementsMap.size > 0 ) {
await new Promise( ( resolve ) => {
intersectionObserver = new IntersectionObserver(
( entries ) => {
for ( const entry of entries ) {
elementIntersections.push( entry );
}
resolve();
},
{
root: null, // To watch for intersection relative to the device's viewport.
threshold: 0.0, // As soon as even one pixel is visible.
}
);
for ( const element of breadcrumbedElementsMap.keys() ) {
intersectionObserver.observe( element );
}
} );
// Stop observing as soon as the page scrolls since we only want initial-viewport elements.
win.addEventListener( 'scroll', disconnectIntersectionObserver, {
once: true,
passive: true,
} );
}
const { onLCP } = await import( webVitalsLibrarySrc );
/** @type {LCPMetric[]} */
const lcpMetricCandidates = [];
// Obtain at least one LCP candidate. More may be reported before the page finishes loading.
await new Promise( ( resolve ) => {
onLCP(
( /** @type LCPMetric */ metric ) => {
lcpMetricCandidates.push( metric );
resolve();
},
{
// This avoids needing to click to finalize LCP candidate. While this is helpful for testing, it also
// ensures that we always get an LCP candidate reported. Otherwise, the callback may never fire if the
// user never does a click or keydown, per <https://github.com/GoogleChrome/web-vitals/blob/07f6f96/src/onLCP.ts#L99-L107>.
reportAllChanges: true,
}
);
} );
// Stop observing.
disconnectIntersectionObserver();
if ( isDebug ) {
log( 'Detection is stopping.' );
}
urlMetric = {
url: currentUrl,
viewport,
elements: [],
};
const lcpMetric = lcpMetricCandidates.at( -1 );
for ( const elementIntersection of elementIntersections ) {
const xpath = breadcrumbedElementsMap.get( elementIntersection.target );
if ( ! xpath ) {
if ( isDebug ) {
error( 'Unable to look up XPath for element' );
}
continue;
}
const element = /** @type {Element|null} */ (
lcpMetric?.entries[ 0 ]?.element
);
const isLCP = elementIntersection.target === element;
/** @type {ElementData} */
const elementData = {
isLCP,
isLCPCandidate: !! lcpMetricCandidates.find(
( lcpMetricCandidate ) => {
const candidateElement = /** @type {Element|null} */ (
lcpMetricCandidate.entries[ 0 ]?.element
);
return candidateElement === elementIntersection.target;
}
),
xpath,
intersectionRatio: elementIntersection.intersectionRatio,
intersectionRect: elementIntersection.intersectionRect,
boundingClientRect: elementIntersection.boundingClientRect,
};
urlMetric.elements.push( elementData );
elementsByXPath.set( elementData.xpath, elementData );
}
if ( isDebug ) {
log( 'Current URL Metric:', urlMetric );
}
// Wait for the page to be hidden.
await new Promise( ( resolve ) => {
win.addEventListener( 'pagehide', resolve, { once: true } );
win.addEventListener( 'pageswap', resolve, { once: true } );
doc.addEventListener(
'visibilitychange',
() => {
if ( document.visibilityState === 'hidden' ) {
// TODO: This will fire even when switching tabs.
resolve();
}
},
{ once: true }
);
} );
// Only proceed with submitting the URL Metric if viewport stayed the same size. Changing the viewport size (e.g. due
// to resizing a window or changing the orientation of a device) will result in unexpected metrics being collected.
if (
window.innerWidth !== urlMetric.viewport.width ||
window.innerHeight !== urlMetric.viewport.height
) {
if ( isDebug ) {
log(
'Aborting URL Metric collection due to viewport size change.'
);
}
return;
}
if ( extensions.size > 0 ) {
for ( const [
extensionModuleUrl,
extension,
] of extensions.entries() ) {
if ( extension.finalize instanceof Function ) {
try {
await extension.finalize( {
isDebug,
getRootData,
getElementData,
extendElementData,
extendRootData,
} );
} catch ( err ) {
error(
`Unable to finalize module '${ extensionModuleUrl }':`,
err
);
}
}
}
}
// Even though the server may reject the REST API request, we still have to set the storage lock
// because we can't look at the response when sending a beacon.
setStorageLock( getCurrentTime() );
if ( isDebug ) {
log( 'Sending URL Metric:', urlMetric );
}
const url = new URL( restApiEndpoint );
url.searchParams.set( 'slug', urlMetricSlug );
url.searchParams.set( 'current_etag', currentETag );
if ( typeof cachePurgePostId === 'number' ) {
url.searchParams.set(
'cache_purge_post_id',
cachePurgePostId.toString()
);
}
url.searchParams.set( 'hmac', urlMetricHMAC );
navigator.sendBeacon(
url,
new Blob( [ JSON.stringify( urlMetric ) ], {
type: 'application/json',
} )
);
// Clean up.
breadcrumbedElementsMap.clear();
}