Skip to content

Commit

Permalink
Wrap individual puppeteer navigations in try/catch block
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jan 29, 2025
1 parent c8cfc87 commit c9ea68b
Showing 1 changed file with 107 additions and 103 deletions.
210 changes: 107 additions & 103 deletions cli/commands/benchmark-web-vitals.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -460,121 +460,125 @@ async function benchmarkURL(
}

for ( let requestNum = 0; requestNum < params.amount; requestNum++ ) {
if ( logProgress ) {
logPartial(
`Benchmarking ${ requestNum + 1 } / ${ params.amount }...`
);
}
const page = await browser.newPage();
await page.setBypassCSP( true ); // Bypass CSP so the web vitals script tag can be injected below.
if ( params.cpuThrottleFactor ) {
await page.emulateCPUThrottling( params.cpuThrottleFactor );
}

if ( params.networkConditions ) {
await page.emulateNetworkConditions( params.networkConditions );
}
try {
if ( logProgress ) {
logPartial(
`Benchmarking ${ requestNum + 1 } / ${ params.amount }...`
);
}
const page = await browser.newPage();
await page.setBypassCSP( true ); // Bypass CSP so the web vitals script tag can be injected below.
if ( params.cpuThrottleFactor ) {
await page.emulateCPUThrottling( params.cpuThrottleFactor );
}

if ( params.emulateDevice ) {
await page.emulate( params.emulateDevice );
}
if ( params.windowViewport ) {
await page.setViewport( {
...( params.emulateDevice
? params.emulateDevice.viewport
: {} ),
...params.windowViewport,
} );
}
if ( params.networkConditions ) {
await page.emulateNetworkConditions( params.networkConditions );
}

// Load the page.
const urlObj = new URL( url );
urlObj.searchParams.append( 'rnd', String( Math.random() ) );
if ( params.emulateDevice ) {
await page.emulate( params.emulateDevice );
}
if ( params.windowViewport ) {
await page.setViewport( {
...( params.emulateDevice
? params.emulateDevice.viewport
: {} ),
...params.windowViewport,
} );
}

// Make sure any username and password in the URL is passed along for authentication.
if ( urlObj.username && urlObj.password ) {
await page.authenticate( {
username: urlObj.username,
password: urlObj.password,
} );
}
// Load the page.
const urlObj = new URL( url );
urlObj.searchParams.append( 'rnd', String( Math.random() ) );

const response = await page.goto( urlObj.toString(), {
waitUntil: 'networkidle0',
} );
if ( scriptTag ) {
await page.addScriptTag( { content: scriptTag, type: 'module' } );
}
// Make sure any username and password in the URL is passed along for authentication.
if ( urlObj.username && urlObj.password ) {
await page.authenticate( {
username: urlObj.username,
password: urlObj.password,
} );
}

if ( response.status() !== 200 ) {
if ( logProgress ) {
log(
formats.error(
`Error: Bad response code ${ response.status() }.`
)
);
const response = await page.goto( urlObj.toString(), {
waitUntil: 'networkidle0',
} );
if ( scriptTag ) {
await page.addScriptTag( { content: scriptTag, type: 'module' } );

Check failure on line 507 in cli/commands/benchmark-web-vitals.mjs

View workflow job for this annotation

GitHub Actions / Lint

Replace `·content:·scriptTag,·type:·'module'·` with `⏎↹↹↹↹↹content:·scriptTag,⏎↹↹↹↹↹type:·'module',⏎↹↹↹↹`
}
continue;
}

completeRequests++;
if ( response.status() !== 200 ) {
if ( logProgress ) {
log(
formats.error(
`Error: Bad response code ${ response.status() }.`
)
);
}
continue;
}

if ( groupedMetrics.webVitals ) {
await Promise.all(
Object.values( groupedMetrics.webVitals ).map(
async ( value ) => {
// Wait until global is populated.
await page.waitForFunction(
`window.${ value.global } !== undefined`
);
completeRequests++;

/*
* Do a random click, since only that triggers certain metrics
* like LCP, as only a user interaction stops reporting new LCP
* entries. See https://web.dev/lcp/.
*
* Click off screen to prevent clicking a link by accident and navigating away.
*/
await page.click( 'body', {
offset: { x: -500, y: -500 },
} );
// Get the metric value from the global.
const metric =
/** @type {number} */ await page.evaluate(
( global ) => window[ global ],
value.global
if ( groupedMetrics.webVitals ) {
await Promise.all(
Object.values( groupedMetrics.webVitals ).map(
async ( value ) => {
// Wait until global is populated.
await page.waitForFunction(
`window.${ value.global } !== undefined`
);
value.results.push( metric );
}
)
).catch( () => {
/* Ignore errors. */
} );
}

if ( groupedMetrics.serverTiming ) {
const serverTimingMetrics = await page.evaluate( () => {
const entry = performance.getEntries().find(
( ent ) => ent instanceof PerformanceNavigationTiming // eslint-disable-line no-undef
);
// eslint-disable-next-line no-undef
if ( entry instanceof PerformanceNavigationTiming ) {
return entry.serverTiming.reduce( ( acc, value ) => {
acc[ value.name ] = value.duration;
return acc;
}, {} );
}
return {};
} );
Object.values( groupedMetrics.serverTiming ).forEach( ( value ) => {
if ( serverTimingMetrics[ value.name ] ) {
value.results.push( serverTimingMetrics[ value.name ] );
}
} );
}
/*
* Do a random click, since only that triggers certain metrics
* like LCP, as only a user interaction stops reporting new LCP
* entries. See https://web.dev/lcp/.
*
* Click off screen to prevent clicking a link by accident and navigating away.
*/
await page.click( 'body', {
offset: { x: -500, y: -500 },
} );
// Get the metric value from the global.
const metric =
/** @type {number} */ await page.evaluate(
( global ) => window[ global ],
value.global
);
value.results.push( metric );
}
)
).catch( () => {
/* Ignore errors. */
} );
}

if ( logProgress ) {
log( formats.success( 'Success.' ) );
if ( groupedMetrics.serverTiming ) {
const serverTimingMetrics = await page.evaluate( () => {
const entry = performance.getEntries().find(
( ent ) => ent instanceof PerformanceNavigationTiming // eslint-disable-line no-undef
);
// eslint-disable-next-line no-undef
if ( entry instanceof PerformanceNavigationTiming ) {
return entry.serverTiming.reduce( ( acc, value ) => {
acc[ value.name ] = value.duration;
return acc;
}, {} );
}
return {};
} );
Object.values( groupedMetrics.serverTiming ).forEach( ( value ) => {

Check failure on line 570 in cli/commands/benchmark-web-vitals.mjs

View workflow job for this annotation

GitHub Actions / Lint

Replace `·` with `⏎↹↹↹↹↹`
if ( serverTimingMetrics[ value.name ] ) {

Check failure on line 571 in cli/commands/benchmark-web-vitals.mjs

View workflow job for this annotation

GitHub Actions / Lint

Insert `↹`
value.results.push( serverTimingMetrics[ value.name ] );

Check failure on line 572 in cli/commands/benchmark-web-vitals.mjs

View workflow job for this annotation

GitHub Actions / Lint

Replace `value.results.push(·serverTimingMetrics[·value.name·]·` with `↹value.results.push(⏎↹↹↹↹↹↹↹↹serverTimingMetrics[·value.name·]⏎↹↹↹↹↹↹↹`
}

Check failure on line 573 in cli/commands/benchmark-web-vitals.mjs

View workflow job for this annotation

GitHub Actions / Lint

Insert `↹`
} );

Check failure on line 574 in cli/commands/benchmark-web-vitals.mjs

View workflow job for this annotation

GitHub Actions / Lint

Replace `}·` with `↹}⏎↹↹↹↹`
}

if ( logProgress ) {
log( formats.success( 'Success.' ) );
}
} catch ( err ) {
log( formats.error( `Error: ${ err.message }.` ) );
}
}

Expand Down

0 comments on commit c9ea68b

Please sign in to comment.