- How do I install spatie/lighthouse-php in a Laravel project?
- Use Composer to install the package with `composer require spatie/lighthouse-php`. The package integrates seamlessly with Laravel’s service container, so no additional configuration is needed beyond requiring it. For Laravel 5.5+, auto-discovery handles the service provider registration automatically.
- Which Laravel versions does spatie/lighthouse-php support?
- The package supports Laravel 8.x, 9.x, and 10.x. Check the [GitHub repository](https://github.com/spatie/lighthouse-php) for the latest compatibility details, as minor updates may align with newer Laravel releases. It leverages Laravel’s container and facade systems, ensuring smooth integration.
- Can I run Lighthouse audits asynchronously in Laravel?
- Yes, you can wrap Lighthouse runs in a Laravel job by extending `ShouldQueue`. This is ideal for production use to avoid timeouts or resource exhaustion. Use Laravel’s queue system (e.g., Redis or database queues) to process audits in the background.
- What are the system requirements for running Lighthouse audits?
- You need a headless Chrome or Chromium installation (e.g., via Docker or system-level setup). The package uses `exec()` to launch Chrome, so ensure the binary path is accessible. For CI/CD environments, containerized Chrome (e.g., `chromium:latest`) is recommended to avoid dependency conflicts.
- How do I customize Lighthouse audits (e.g., throttling, headers, user agent)?
- Use the fluent API to configure audits: `Lighthouse::url('example.com')->throttleCpu()->userAgent('MyBot')->headers(['X-Custom' => 'Header'])`. You can also select specific categories (e.g., `Category::Performance`) or adjust runtime options like `maxWaitForLoad` for timeouts.
- How do I store or process Lighthouse results in Laravel?
- The `LighthouseResult` object returns structured data (scores, audits, etc.) that can be serialized into JSON or stored in a database. For example, create a Laravel model to log results and use `result->toArray()` to extract data. You can also trigger events or notifications based on audit outcomes.
- Is there a way to fail a CI/CD pipeline if Lighthouse scores fall below a threshold?
- Yes, you can integrate Lighthouse into your CI pipeline by running audits in a script and checking scores. For example, use `php artisan lighthouse:audit` (custom command) and exit with a non-zero status if scores (e.g., `result->scores()['performance'] < 90`) are below your threshold. Tools like GitHub Actions or GitLab CI can then fail the pipeline.
- Can I run Lighthouse audits on multiple URLs concurrently?
- The package itself doesn’t support parallel audits, but you can achieve concurrency by dispatching multiple queued jobs (e.g., `LighthouseJob::dispatch('url1')->dispatch('url2')`). Use Laravel’s queue workers with a distributed system (e.g., Redis) to handle concurrent executions efficiently.
- How do I handle Chrome version compatibility issues in CI/CD?
- Pin the Chrome version in your CI environment (e.g., Docker image with `chromium:114.0`) to avoid compatibility breaks. The package requires a specific Chrome version for Lighthouse to work, so testing locally with the same version as your CI setup is critical. Use environment variables to specify the Chrome binary path if needed.
- Are there alternatives to spatie/lighthouse-php for running Lighthouse in PHP?
- Alternatives include using the official Lighthouse CLI via `shell_exec()` or a Node.js wrapper (e.g., `lighthouse-ci`). However, spatie/lighthouse-php is the most Laravel-native solution, offering a clean PHP API, queue integration, and seamless result parsing. For advanced use cases, you might combine it with tools like Laravel Nova for UI-driven audits.