If you're like me, the command line is life! If an app has an option that will keep you in your favorite terminal, you're a happy developer. Enter Google Lighthouse. It has become the de facto standard for front-end performance optimization. Let's take a look at how to get started with the Lighthouse CLI.
Why Should You Use Google Lighthouse?
In short, it's open source, has sane defaults, and is highly extensible. Testing site performance is often a mixed bag of inconsistency. You end up with multiple tools, conflicting recommendations, and too much time spent worrying about tooling.
For example, say you're particularly concerned with SEO. You could enhance the existing SEO module with additional features. You could also remove the standard report and substitute it with your own version.
Maybe you want to ensure meta tags stay within an arbitrary character length? If you can write functions to grab DOM elements and check the character counts of their contents, making a custom Lighthouse report module will be a cinch.
Before we get too deep in to customization, first let's get started installing Lighthouse CLI.
Installing The Lighthouse CLI
To get started, you'll need Node 8 LTS, as of the time of this writing. You can install the CLI with either NPM or Yarn.
GitHub repo: https://github.com/GoogleChrome/lighthouse
npm install -g lighthouse
yarn global add lighthouse
Lighthouse CLI Options & Flags
This isn't an exhaustive list of all options and flags. I've highlighted some of the more interesting and useful ones here. You can find the rest from the official docs.
The path to your JSON config file.
Custom flags to pass to Chrome.
When using a local TLS server, bypasses "connection not secure" interstitial. This allows testing to continue uninterrupted.
Specify your desired port. You can also use 0 for a random port.
Choose your desired format.
Choose where to output.
Have lighthouse cli open the report after the run completes.
Sometimes you may want to do a simple and quick desktop test.
Local Testing With TLS
For local testing purposes, most audit tools throw false positive errors about lack of TLS and HTTP/2. Here's a simple solution to run a local TLS and H2-enabled server:
Example CLI Command:
lighthouse https://localhost:3000 \
Specify port to listen on e.g.
simplehttp2server -listen :3000
Specify custom JSON config file to use
Set allowed origins (default "*"). Not needed for most testing.
General Questions & Tips
Is Lighthouse capable of network throttling, and can I configure it?
Yes and yes. By default, Lighthouse cli throttles the network, CPU, and attempts to emulate a 4G connection.
Does Lighthouse "call home" to Google or have any external tracking?
That's a definitive NO. Lighthouse cli runs completely local. The app itself does not fire any external requests for tracking or other purposes.
This is fantastic to hear these days. If you were unsure about using Lighthouse in your development environment, rest assured.
Keep in mind, Lighthouse uses a local Chrome instance to perform its tests. So tracking scripts on the sites you are testing may still fire.
Is there a way to automatically open Lighthouse reports?
Add the flag --view when you run Lighthouse. Once the tests finish, your HTML report will immediately open in your browser.