Skip to content

Commit 094ef41

Browse files
authored
Merge pull request #1656 from WordPress/improve/od-debugging
Improve debugging stored URL Metrics in Optimization Detective
2 parents 3258112 + f182ec7 commit 094ef41

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

plugins/optimization-detective/detect.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ function extendElementData( xpath, properties ) {
233233
Object.assign( elementData, properties );
234234
}
235235

236+
/**
237+
* @typedef {{timestamp: number, creationDate: Date}} UrlMetricDebugData
238+
* @typedef {{groups: Array<{url_metrics: Array<UrlMetricDebugData>}>}} CollectionDebugData
239+
*/
240+
236241
/**
237242
* Detects the LCP element, loaded images, client viewport and store for future optimizations.
238243
*
@@ -250,7 +255,7 @@ function extendElementData( xpath, properties ) {
250255
* @param {URLMetricGroupStatus[]} args.urlMetricGroupStatuses URL Metric group statuses.
251256
* @param {number} args.storageLockTTL The TTL (in seconds) for the URL Metric storage lock.
252257
* @param {string} args.webVitalsLibrarySrc The URL for the web-vitals library.
253-
* @param {Object} [args.urlMetricGroupCollection] URL Metric group collection, when in debug mode.
258+
* @param {CollectionDebugData} [args.urlMetricGroupCollection] URL Metric group collection, when in debug mode.
254259
*/
255260
export default async function detect( {
256261
minViewportAspectRatio,
@@ -269,7 +274,21 @@ export default async function detect( {
269274
urlMetricGroupCollection,
270275
} ) {
271276
if ( isDebug ) {
272-
log( 'Stored URL Metric group collection:', urlMetricGroupCollection );
277+
const allUrlMetrics = /** @type Array<UrlMetricDebugData> */ [];
278+
for ( const group of urlMetricGroupCollection.groups ) {
279+
for ( const otherUrlMetric of group.url_metrics ) {
280+
otherUrlMetric.creationDate = new Date(
281+
otherUrlMetric.timestamp * 1000
282+
);
283+
allUrlMetrics.push( otherUrlMetric );
284+
}
285+
}
286+
log( 'Stored URL Metric Group Collection:', urlMetricGroupCollection );
287+
allUrlMetrics.sort( ( a, b ) => b.timestamp - a.timestamp );
288+
log(
289+
'Stored URL Metrics in reverse chronological order:',
290+
allUrlMetrics
291+
);
273292
}
274293

275294
// Abort if the current viewport is not among those which need URL Metrics.

0 commit comments

Comments
 (0)