Git Product home page Git Product logo

lighthouse-php's Introduction

Run Google Lighthouse using PHP

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

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.

lighthouse-php's People

Contributors

danielstgt avatar dependabot[bot] avatar freekmurze avatar github-actions[bot] avatar jonnx avatar justinbuhrmann1997 avatar pluuk avatar rubenvanerk avatar styleshit avatar thecaliskan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lighthouse-php's Issues

Timeout / memory issue / something else (?) on complex / large sites

Hi friends! I'm running in to an issue running the full test on a large site. My error log shows things are working, but it throws an error before completing.

$result = Lighthouse::url($this->page->url)
                ->timeoutInSeconds(180)
                ->run();

This works just fine with most URLs, but on what seem to be larger / more complicated pages, an error is thrown

...
several lines of debug
...
Sat, 11 Mar 2023 19:25:12 GMT LH:status Auditing: robots.txt is valid
Sat, 11 Mar 2023 19:25:12 GMT LH:status Auditing: Tap targets are sized appropriately
Sat, 11 Mar 2023 19:25:12 GMT LH:status Auditing: Document has a valid `hreflang`
Sat, 11 Mar 2023 19:25:12 GMT LH:status Auditing: Document avoids plugins
Sat, 11 Mar 2023 19:25:12 GMT LH:status Auditing: Document has a valid `rel=canonical`
Sat, 11 Mar 2023 19:25:12 GMT LH:status Auditing: Structured data is valid
Sat, 11 Mar 2023 19:25:12 GMT LH:status Generating results...
`

  at vendor/spatie/lighthouse-php/src/Exceptions/CouldNotRunLighthouse.php:9
      5▕ class CouldNotRunLighthouse extends \Exception
      6▕ {
      7▕     public static function make(string $errorOutput): self
      8▕     {
  ➜   9▕         return new self("Could not run Lighthouse. Error : `{$errorOutput}`");
     10▕     }
     11▕ }
     12▕

I've attempted to increase the timeout, but it doesn't seem to make a difference. The error is thrown before we get close to the length of the timeout.

Is there a memory limit I'm hitting somewhere? I know the screenshot audits add a lot of base64 encoded data, but specifically ignoring those tests aren't enough. I can make it work if I limit the audits using ->onlyAudits()

I can break apart my tests into several different jobs if it is related to a memory issue, but figured I'd open an issue here in case anyone has noticed this and found a good approach. Thank you, Spatie team!

Laravel 10 move to "module" in package.json breaks lighthouse run

Laravel recently witched to modules for the default package.json: laravel/laravel@d3b2ead

When using a fresh laravel install with this package, you get an obscure error message:


   Spatie\Lighthouse\Exceptions\CouldNotRunLighthouse  Could not run Lighthouse. Error : `file://vendor/spatie/lighthouse-php/bin/lighthouse.js:4
        const arguments = JSON.parse(process.argv.slice(2));
              ^^^^^^^^^

SyntaxError: Unexpected eval or arguments in strict mode
    at ESMLoader.moduleStrategy (node:internal/modules/esm/translators:117:18)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:337:14)
    at async link (node:internal/modules/esm/module_job:70:21)

This can be resolved by updating the package.json with:

{
    "private": true,
    "type": "commonjs",
    ...
}

Not sure if this is a long term solution but a work around. I may give updating the lighhouse.js file a shot this weekend.

PHP 8.2 issue?

Awesome project.

Installed it on PHP 8.1 all good. But then updated that machine's PHP to 8.2 using Remi-php docs. It gave me 8.2.1RC1 and I receive the following error:

Fatal error: Uncaught Symfony\Component\Process\Exception\ProcessSignaledException: The process has been signaled with signal "5". in /var/www/vhosts/lighthouse/vendor/symfony/process/Process.php:434 

Stack trace: 
 #0 /var/www/vhosts//lighthouse/vendor/symfony/process/Process.php(249): Symfony\Component\Process\Process->wait() 
 #1 /var/www/vhosts//lighthouse/vendor/spatie/lighthouse-php/src/Lighthouse.php(215): Symfony\Component\Process\Process->run() 
 #2 /var/www/vhosts//lighthouse/index.php(15): Spatie\Lighthouse\Lighthouse->run() 
 #3 {main} thrown in /var/www/vhosts//lighthouse/vendor/symfony/process/Process.php on line 434

Ran composer update - and nothing was found to update. 🤷‍♂️

There was an error running Lighthouse: DNS servers could not resolve the provided domain. (code: DNS_FAILURE)

Hi i have setup every thing as you mentioned in the docs.
It's working fine on Local Serve for every sites.
The issue is when i make it live or move it server with all configurations the some site having issue with 403 and DNS_FAILURE
for some sites only otherwise it working properly i have also set the extraHeaders after that some site's issue resolve but still remaining site's lighthouse reports is not working. Kindly suggest me to how to get the results for these sites.

https://unifiedportal-emp.epfindia.gov.in/epfo/

follow this to check bug #29

I install follow document and when try test it get Error [ERR_REQUIRE_ESM]: require() of ES Module

Could you help me check the issue with error log

[2023-02-10 05:11:26] development.ERROR: Could not run Lighthouse. Error : `E:\MyProjects\theseotool.site\vendor\spatie\lighthouse-php\bin\lighthouse.js:1
const lighthouse = require('lighthouse');
^

Error [ERR_REQUIRE_ESM]: require() of ES Module E:\MyProjects\theseotool.site\node_modules\lighthouse\core\index.js from E:\MyProjects\theseotool.site\vendor\spatie\lighthouse-php\bin\lighthouse.js not supported.
Instead change the require of index.js in E:\MyProjects\theseotool.site\vendor\spatie\lighthouse-php\bin\lighthouse.js to a dynamic import() which is available in all CommonJS modules.
at Object. (E:\MyProjects\theseotool.site\vendor\spatie\lighthouse-php\bin\lighthouse.js:1:20) {
code: 'ERR_REQUIRE_ESM'
}

Node.js v18.12.1
{"exception":"[object] (Spatie\\Lighthouse\\Exceptions\\CouldNotRunLighthouse(code: 0): Could not run Lighthouse. Error :E:\MyProjects\theseotool.site\vendor\spatie\lighthouse-php\bin\lighthouse.js:1

const lighthouse = require('lighthouse');

Always runs as mobile despite the formFactor

The documentation states that setting the formFactor will generate to either Mobile or Desktop; https://spatie.be/docs/lighthouse-php/v1/usage/configuring-a-run#content-specifying-the-form-factor

It also mentions that the default is Desktop, but I think that has changed to Mobile over the last few years.

Running the report always runs as Mobile, despite setting the form factor. See the attached image for the report settings.

$result = LighthouseCheck::url('https://laravel.com')
            ->formFactor(FormFactor::Desktop)
            ->run()
            ->saveHtml($localReportPath);
Screenshot 2024-03-01 at 12 39 16

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.