Skip to content

Commit

Permalink
Prevent submitting URL Metric if viewport size changed
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Nov 23, 2024
1 parent cf4b6e9 commit a75b94f
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions plugins/optimization-detective/detect.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ function recursiveFreeze( obj ) {
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.
*
Expand Down Expand Up @@ -442,10 +448,7 @@ export default async function detect( {

urlMetric = {
url: currentUrl,
viewport: {
width: win.innerWidth,
height: win.innerHeight,
},
viewport,
elements: [],
};

Expand Down Expand Up @@ -506,6 +509,20 @@ 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 (
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,
Expand Down

0 comments on commit a75b94f

Please sign in to comment.