I am using lighthouse for creating performance reports for a bunch of our own websites. I also need to take a screenshot to see how the website looks on a desktop computer.
It seems like Lighthouse by default takes a screenshot with mobile resolution.
How can I change that?
I am trying to avoid using other libraries like Puppeteer if possible.
Here's some code that runs lighthouse
import lighthouse from 'lighthouse';
import chromeLauncher from 'chrome-launcher';
const chrome = await chromeLauncher.launch({ chromeFlags: ['--headless'] });
const options = {
logLevel: 'info',
output: 'json',
onlyCategories: ['performance', 'accessibility', 'best-practices', 'seo'],
port: chrome.port
};
const runnerResult = await lighthouse(url, options);
Lighthouse returns display captures from the audits for:
screenshot-thumbnails
- 120px x 213pxfinal-screenshot
- 280px x 498pxCan I get anything larger than that?
You need to make use of the lighthouse CLI options
listed in the documentation - for example see the github readme and the node-npm documentation.
There are many options, but the ones that are the most interesting option is:
--screenEmulation
Note: See also
--preset
option, or use--screenEmulation.disabled
to disable.Otherwise you can set these 4 parameters individually:
--screenEmulation.mobile --screenEmulation.width=360 --screenEmulation.height=640 --screenEmulation.deviceScaleFactor=2
Also please have a look at the device emulation / emulatedUserAgents, and specifically play with params like the following:
lighthouse <url> --chrome-flags="--window-size=1024,768"
lighthouse <url> --screenEmulation.desktop--screenEmulation.width=1024 --screenEmulation.height=768 --screenEmulation.deviceScaleFactor=2
lighthouse <url> --screenEmulation.mobile --screenEmulation.width=1024 --screenEmulation.height=768 --screenEmulation.deviceScaleFactor=2
and
lighthouse <url> --preset=desktop
Have a look also at some additional configs located at: https://github.com/GoogleChrome/lighthouse/tree/master/lighthouse-core/config
In terms of launching through nodejs, I need to find my example that I had, but I did find the example from the documentation of lighthouse. In order to run lighthouse + chrome headless from nodejs use the following:
const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
function launchChromeAndRunLighthouse(url, flags = {}, config = null) {
return chromeLauncher.launch(flags).then(chrome => {
flags.port = chrome.port;
return lighthouse(url, flags, config).then(results =>
chrome.kill().then(() => results));
});
}
const flags = {
chromeFlags: ['--headless']
};
launchChromeAndRunLighthouse('https://github.com', flags).then(results => {
// Use results!
});
PS: Will update with more information soon
Sources:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With