diff --git a/plugins/optimization-detective/detect.js b/plugins/optimization-detective/detect.js index 385f81b0d5..40d91375c7 100644 --- a/plugins/optimization-detective/detect.js +++ b/plugins/optimization-detective/detect.js @@ -325,6 +325,16 @@ export default async function detect( { return; } + // Keep track of whether the window resized. If it resized, we abort sending the URLMetric. + let didWindowResize = false; + window.addEventListener( + 'resize', + () => { + didWindowResize = true; + }, + { once: true } + ); + // TODO: Does this make sense here? // Prevent detection when page is not scrolled to the initial viewport. if ( doc.documentElement.scrollTop > 0 ) { @@ -507,6 +517,17 @@ export default async function detect( { ); } ); + // 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 ( didWindowResize ) { + if ( isDebug ) { + log( + 'Aborting URL Metric collection due to viewport size change.' + ); + } + return; + } + if ( extensions.size > 0 ) { for ( const [ extensionModuleUrl,