Skip to content

Commit

Permalink
Cap length at clientHeight for new requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian de Bhal committed Feb 8, 2021
1 parent 8322156 commit fb9c438
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions lib/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,36 @@ function take_snapshot( p_uri, p_filename, p_width, p_height, screen_width, scre
await page.waitFor( 2000 );
makeDirIfRequired( _path.dirname( p_filename ) );

// Backwards compatibility requires we continue to allow
// screenshots longer than the target.
//
// However, as we support longer pages guessing the wrong
// length becomes uglier and more wasteful, so we'd like
// to limit the clip length to the screen size.
//
// Prior to an actual `/mshots/v2/` bump we can compromise by
// limiting to the clientHeight only on requests that have
// specified the new screen_width/screen_height parameter.
let clientWidth = Number.MAX_SAFE_INTEGER;
let clientHeight = Number.MAX_SAFE_INTEGER;
if ( screen_width !== p_width || screen_height !== p_height ) {
[ clientWidth, clientHeight ] = await page.evaluate(
() => [ document.body.clientWidth, document.body.clientHeight ] );
}

const snapshotHeight = screen_height
? Math.min( screen_height, clientHeight )
: p_height;

const snapshotWidth = screen_width
? Math.min( screen_width, clientWidth )
: p_width;

await page.screenshot( { path: p_filename, fullPage: false, type: 'jpeg', quality: 90, clip: {
x: 0,
y: 0,
width: screen_width || p_width,
height: screen_height || p_height
width: snapshotWidth,
height: snapshotHeight
} } );

await page.close();
Expand Down

0 comments on commit fb9c438

Please sign in to comment.