Skip to content

Commit 7e69f6b

Browse files
authored
Merge pull request #1712 from WordPress/fix/od-abort-url-metrics-submission-on-viewport-size-change
Prevent submitting URL Metric if viewport size changed
2 parents 6036183 + e222c85 commit 7e69f6b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

plugins/optimization-detective/detect.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,16 @@ export default async function detect( {
325325
return;
326326
}
327327

328+
// Keep track of whether the window resized. If it resized, we abort sending the URLMetric.
329+
let didWindowResize = false;
330+
window.addEventListener(
331+
'resize',
332+
() => {
333+
didWindowResize = true;
334+
},
335+
{ once: true }
336+
);
337+
328338
// TODO: Does this make sense here?
329339
// Prevent detection when page is not scrolled to the initial viewport.
330340
if ( doc.documentElement.scrollTop > 0 ) {
@@ -507,6 +517,17 @@ export default async function detect( {
507517
);
508518
} );
509519

520+
// Only proceed with submitting the URL Metric if viewport stayed the same size. Changing the viewport size (e.g. due
521+
// to resizing a window or changing the orientation of a device) will result in unexpected metrics being collected.
522+
if ( didWindowResize ) {
523+
if ( isDebug ) {
524+
log(
525+
'Aborting URL Metric collection due to viewport size change.'
526+
);
527+
}
528+
return;
529+
}
530+
510531
if ( extensions.size > 0 ) {
511532
for ( const [
512533
extensionModuleUrl,

0 commit comments

Comments
 (0)