Google Lighthouse is an open-source, automated tool for improving the quality of web pages. It has audits for performance, accessibility, progressive web apps, SEO, and more.
Our newest package, spatie/lighthouse-php makes it easy to run Lighthouse using PHP. In this blog post, I'd like to tell you all about it.
Introducing Google Lighthouse
Lighthouse is an open-source tool created by Google. It can audit any website.
The report Lighthouse generates detailed information and suggestions for improving the performance and quality of a website. It can be used to audit a wide range of factors, including the speed at which a page loads, the accessibility of the page for users with disabilities, and the security of the page.
A Lighthouse report also contains interesting metrics that allow webmasters and developers to understand the current state of a website and track progress over time. These numbers can help identify areas of a website that need the most attention and monitor the effectiveness of any changes made.
Here's what such a report looks like.
The report contains many tips on how to improve your site.
Running Lighthouse using PHP
Our newest package, spatie/lighthouse-php allows you to run Lighthouse using PHP.
The package makes running Lighthouse a one-liner:
$result = Lighthouse::url('https://example.com')->run();
Be aware that Lighthouse needs some time to run. In my tests, it averaged between 30 and 40 seconds.
That result is an instance of
Spatie\Lighthouse\LighthouseResult, containing many handy functions to grab interesting things from the result. In the following example, we will get the scores for the five major Lighthouse categories.
$result->scores(); // returns an array like this one: /* * [ * 'performance' => 98, * 'accessibility' => 83, * 'best-practices' => 90, * 'seo' => 92, * 'pwa' => 43, * ] */
Here's how to get the results of a specific audit.
$result->audit('first-contentful-paint') // returns this array /* * [ * 'id' => 'first-contentful-paint' * 'title' => 'First Contentful Paint' * 'score' => 0.98 * 'scoreDisplayMode' => 'numeric' * 'numericValue' => 1262.95 * 'numericUnit' => 'millisecond' * 'displayValue' => '1.3 s' * ] */
Lighthouse can generate a nicely formatted HTML report with the scores and results of all audits.
use Spatie\Lighthouse\Lighthouse; Lighthouse::url('https://example.com') ->run() ->saveHtml($pathToFile);
This is how that report looks in the browser.
Continuous monitoring of Lighthouse results
Oh Dear is a monitoring service that not only monitors the uptime of your homepage, but it crawls your entire website and sends notifications when it detects broken links. It also offers advanced checks, such as scheduled jobs monitoring and application health monitoring.
One of the new checks I'm working on is the Lighthouse check. This check will regularly run Lighthouse against your site, and Oh Dear will show the most important metrics and their evolution.
And you can even see the entire HTML report.
This information will also be available through our API.
We'll also notify you when Lighthouse detects issues. In the settings, you can customize when you want to be notified.
At the moment, we are beta testing this new check. It will be available for each Oh Dear subscriber in the January - February 2023 timeframe.
If you're not using Oh Dear already, you can register to start your ten day free trial.
Google Lighthouse is a handy tool that can help you improve your website. Our spatie/lighthouse-php makes it easy to create a Lighthouse report using PHP. Read the extensive documentation to learn all the options and possibilities.
This isn't the first package that we have published. On the Spatie website, check out all our open-source packages in this long list. If you want to support us, consider picking up any of our paid products.