Skip to content

Instantly share code, notes, and snippets.

@SilentImp
Last active May 25, 2020 04:24
Show Gist options
  • Save SilentImp/2886de17a0bff9f1537abee49c0ef5dc to your computer and use it in GitHub Desktop.
Save SilentImp/2886de17a0bff9f1537abee49c0ef5dc to your computer and use it in GitHub Desktop.
Using lighthouse
const fs = require('fs');
const lighthouse = require('lighthouse');
const puppeteer = require('puppeteer');
const DOMAIN = process.env.DOMAIN;
// build report for single url
const buildReport = browser => async url => {
const data = await lighthouse(
`${DOMAIN}${url}`,
{
port: new URL(browser.wsEndpoint()).port,
output: 'json',
},
{
extends: 'lighthouse:full',
}
);
const { report: reportJSON } = data;
const report = JSON.parse(reportJSON);
const metrics = [
{
name: report.categories.performance.title,
value: report.categories.performance.score,
desiredSize: 'larger',
},
{
name: report.categories.accessibility.title,
value: report.categories.accessibility.score,
desiredSize: 'larger',
},
{
name: report.categories['best-practices'].title,
value: report.categories['best-practices'].score,
desiredSize: 'larger',
},
{
name: report.categories.seo.title,
value: report.categories.seo.score,
desiredSize: 'larger',
},
{
name: report.categories.pwa.title,
value: report.categories.pwa.score,
desiredSize: 'larger',
},
];
return {
subject: url,
metrics: metrics,
};
};
const urls = [
'/inloggen',
'/wachtwoord-herstellen-otp',
'/lp/service',
'/send-request-to/ww-tammer',
'/post-service-request/binnenschilderwerk',
];
(async () => {
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox', '--headless'],
});
const builder = buildReport(browser);
const report = [];
for (let url of urls) {
const metrics = await builder(url);
report.push(metrics);
}
fs.writeFileSync(`./performance.json`, JSON.stringify(report));
await browser.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment