Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing final screenshot resolution for (Chrome) Lighthouse report

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 213px
  • final-screenshot - 280px x 498px

Can I get anything larger than that?

like image 461
Luca Avatar asked Sep 09 '25 14:09

Luca


1 Answers

Intro

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

Through Nodejs

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:

  • https://github.com/GoogleChrome/lighthouse
  • node-npm documentation.
  • https://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-core/config/lr-desktop-config.js
like image 68
Menelaos Avatar answered Sep 12 '25 05:09

Menelaos