Reputation: 1656
If I use the PageSpeed Insights tool Google offers on its developer website, or the Lighthouse CLI with HTML as output, I get a very nicely formatted report with 0-100 scores like so:
However, if I run the Lighthouse CLI tool with the --output json
option, I get a Lighthouse Result Object (LHR) and Lighthouse's Understanding the Results page helpfully points out that the HTML version is just is "a rendering of information contained in the result object."
My question: How do I translate the JSON into the scores from the HTML version? I want to be able to programmatically react to changes for some custom monitoring I'm setting up for my site.
Upvotes: 1
Views: 569
Reputation: 1656
I actually ended up finding the answer myself by browsing the Lighthouse source code. So I'll just leave the answer here in case others are trying to do this.
With performance as an example, simply multiply lhr.categories.performance.score
with 100 in the lighthouse result object, like so:
import chromeLauncher from 'chrome-launcher';
import lighthouse from 'lighthouse';
const chrome = await chromeLauncher.launch({ chromeFlags: ['--headless'] });
const result = await lighthouse(`https://example.com`, {
onlyCategories: ['performance'],
port: chrome.port
});
const score = result.lhr.categories.performance.score * 100;
Upvotes: 2