Skip to content

Instantly share code, notes, and snippets.

@digitalbase
Created March 8, 2021 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitalbase/f087e0ce0a16c5476d8f74c9d234a3b9 to your computer and use it in GitHub Desktop.
Save digitalbase/f087e0ce0a16c5476d8f74c9d234a3b9 to your computer and use it in GitHub Desktop.
const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
const AirtablePlus = require('airtable-plus');
const airtable = new AirtablePlus({
baseID: 'app6ttxB70e3TjmCL',
apiKey: 'API_KEY',
tableName: 'Themes',
});
let lighthouseConfigDesktop = {
extends: 'lighthouse:default',
settings: {
maxWaitForFcp: 15 * 1000,
maxWaitForLoad: 35 * 1000,
formFactor: 'desktop',
throttling: {
rttMs: 40,
throughputKbps: 10 * 1024,
cpuSlowdownMultiplier: 1,
requestLatencyMs: 0, // 0 means unset
downloadThroughputKbps: 0,
uploadThroughputKbps: 0,
},
screenEmulation: {
mobile: {
mobile: true,
width: 360,
height: 640,
// Moto G4 is really 3, but a higher value here works against
// our perf recommendations.
// https://github.com/GoogleChrome/lighthouse/issues/10741#issuecomment-626903508
deviceScaleFactor: 2.625,
disabled: false,
},
desktop: {
mobile: false,
width: 1350,
height: 940,
deviceScaleFactor: 1,
disabled: false,
},
},
emulatedUserAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4420.0 Safari/537.36 Chrome-Lighthouse',
// Skip the h2 audit so it doesn't lie to us. See https://github.com/GoogleChrome/lighthouse/issues/6539
skipAudits: ['uses-http2'],
},
};
let lighthouseConfigMobile = {
extends: 'lighthouse:default',
settings: {
maxWaitForFcp: 15 * 1000,
maxWaitForLoad: 35 * 1000,
// lighthouse:default is mobile by default
// Skip the h2 audit so it doesn't lie to us. See https://github.com/GoogleChrome/lighthouse/issues/6539
skipAudits: ['uses-http2'],
},
audits: [
'metrics/first-contentful-paint-3g',
],
categories: {
// TODO(bckenny): type extended Config where e.g. category.title isn't required
performance: /** @type {LH.Config.CategoryJson} */({
auditRefs: [
{id: 'first-contentful-paint-3g', weight: 0},
],
}),
},
};
(async () => {
const chrome = await chromeLauncher.launch({chromeFlags: ['--headless']});
lighthouseConfigDesktop.port = chrome.port;
lighthouseConfigMobile.port = chrome.port;
try {
const themes = await airtable.read({ view: 'Newsroom', filterByFormula: `({Lighthouse Performance Desktop} = '')`});
for (let theme of themes) {
console.log(`Running report for ${theme.fields.Url}`);
const runnerResultDesktop = await lighthouse(theme.fields.Url, lighthouseConfigDesktop);
const runnerResultMobile = await lighthouse(theme.fields.Url, lighthouseConfigMobile);
const data = {
'Lighthouse Performance Desktop' : runnerResultDesktop.lhr.categories.performance.score,
'Lighthouse Performance Mobile' : runnerResultMobile.lhr.categories.performance.score
}
console.log(data);
await airtable.update(theme.id, data)
}
} catch (Error) {
console.log(Error);
}
await chrome.kill();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment