Skip to content

Commit a5bd367

Browse files
committed
Prevent submitting URL Metric if viewport size changed
1 parent 14db8d1 commit a5bd367

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

plugins/optimization-detective/detect.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ function recursiveFreeze( obj ) {
127127
Object.freeze( obj );
128128
}
129129

130+
// This needs to be captured early in case the user later resizes the window.
131+
const viewport = {
132+
width: win.innerWidth,
133+
height: win.innerHeight,
134+
};
135+
130136
/**
131137
* URL Metric being assembled for submission.
132138
*
@@ -443,10 +449,7 @@ export default async function detect( {
443449

444450
urlMetric = {
445451
url: currentUrl,
446-
viewport: {
447-
width: win.innerWidth,
448-
height: win.innerHeight,
449-
},
452+
viewport,
450453
elements: [],
451454
};
452455

@@ -507,6 +510,20 @@ export default async function detect( {
507510
);
508511
} );
509512

513+
// Only proceed with submitting the URL Metric if viewport stayed the same size. Changing the viewport size (e.g. due
514+
// to resizing a window or changing the orientation of a device) will result in unexpected metrics being collected.
515+
if (
516+
window.innerWidth !== urlMetric.viewport.width ||
517+
window.innerHeight !== urlMetric.viewport.height
518+
) {
519+
if ( isDebug ) {
520+
log(
521+
'Aborting URL Metric collection due to viewport size change.'
522+
);
523+
}
524+
return;
525+
}
526+
510527
if ( extensions.size > 0 ) {
511528
for ( const [
512529
extensionModuleUrl,

0 commit comments

Comments
 (0)