spatie/lighthouse-php

Run Google Lighthouse using PHP

Fund package maintenance!
spatie

Installs: 12 478

Dependents: 2

Suggesters: 0

Security: 0

Stars: 219

Watchers: 10

Forks: 23

Open Issues: 1

1.2.0 2023-08-11 07:44 UTC

README

Latest Version on Packagist Tests Total Downloads

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.

This package makes it easy to run Lighthouse using PHP. Here's an example on how to get the scores of the five categories of audits that Lighthouse offers.

use Spatie\Lighthouse\Lighthouse;

// returns an instance of Spatie\Lighthouse\LighthouseResult
$result = Lighthouse::url('https://example.com')->run();

$result->scores(); // returns an array like this one:
/*
 * [
 *    'performance' => 98,
 *    'accessibility' => 83,
 *    'best-practices' => 90,
 *    'seo' => 92,
 *    'pwa' => 43,  
 * ]
 */

It's easy to configure various options:

use Spatie\Lighthouse\Lighthouse;
use Spatie\Lighthouse\Enums\Category;

Lighthouse::url('https://example.com')
    ->userAgent('My user agent')
    ->headers(['MyExtraHeader' => 'HeaderValue'])
    ->categories(Category::Performance, Category::Accessibility)
    ->throttleCpu()
    ->run();

Here's how you can get the results of an 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'
 * ]
 */

You can also write a full HTML report to disk:

$result->saveHtml($pathToReport)

Support us

68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6c69676874686f7573652d7068702e6a70673f743d31

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Documentation

All documentation is available on our documentation site.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

This package contains code copied from Laravel's Arr class.

License

The MIT License (MIT). Please see License File for more information.