MCPcopy
hub / github.com/addyosmani/puppeteer-webperf / getCLS

Function getCLS

cumulative-layout-shift.js:38–67  ·  view source on GitHub ↗

* Get cumulative layout shift for a URL * @param {String} url - url to measure * @return {Number} - cumulative layout shift

(url)

Source from the content-addressed store, hash-verified

36 * @return {Number} - cumulative layout shift
37 */
38async function getCLS(url) {
39 const browser = await puppeteer.launch({
40 args: ['--no-sandbox'],
41 timeout: 10000,
42 });
43
44 try {
45 const page = await browser.newPage();
46 const client = await page.target().createCDPSession();
47
48 await client.send('Network.enable');
49 await client.send('ServiceWorker.enable');
50 await page.emulateNetworkConditions(puppeteer.networkConditions['Good 3G']);
51 await page.emulateCPUThrottling(4);
52 await page.emulate(phone);
53 // inject a function with the code from
54 // https://web.dev/cls/#measure-cls-in-javascript
55 await page.evaluateOnNewDocument(calculateShifts);
56 await page.goto(url, {waitUntil: 'load', timeout: 60000});
57
58 const cls = await page.evaluate(() => {
59 return window.cumulativeLayoutShiftScore;
60 });
61 browser.close();
62 return cls;
63 } catch (error) {
64 console.log(error);
65 browser.close();
66 }
67}
68
69getCLS('https://pptr.dev').then((cls) => console.log('CLS is: ' + cls));

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected