Skip to content

Instantly share code, notes, and snippets.

@maxjing
Created February 12, 2019 19:09
Show Gist options
  • Save maxjing/934de68402ee475f5bf0e7521cdfae19 to your computer and use it in GitHub Desktop.
Save maxjing/934de68402ee475f5bf0e7521cdfae19 to your computer and use it in GitHub Desktop.
const lighthouse = require("lighthouse");
const chromeLauncher = require("chrome-launcher");
function launchChromeAndRunLighthouse(type, page, timestamp, res) {
const relativePath = `reports/${type}/${page}/${timestamp}.json`;
let opts = {
chromeFlags: ["--no-sandbox", "--disable-gpu", "--headless"],
extraHeaders: { Cookie: "split_tcv=100" }
};
let lighthouse_config_accessibilityOnly = {
extends: "lighthouse:default",
settings: {
onlyCategories: ["accessibility"]
}
};
let lighthouse_config;
if (page === "hp") {
lighthouse_config = null;
} else {
lighthouse_config = lighthouse_config_accessibilityOnly;
}
let url = getUrl(type, page);
return chromeLauncher
.launch({ chromeFlags: opts.chromeFlags })
.then(chrome => {
opts.port = chrome.port;
return lighthouse(url, opts, lighthouse_config).then(results => {
delete results.artifacts;
return chrome.kill().then(() => {
res.send(results.lhr),
s3.upload(
{
Body: JSON.stringify(results.lhr),
Bucket: config.bucketName,
Key: `${relativePath}`
},
(err, data) => {
if (err) console.error(`Upload Error ${err}`);
else console.log("Upload Completed");
}
);
});
});
});
}
module.exports = {
launchChromeAndRunLighthouse
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment