Configuring a run | lighthouse-go | Spatie       

 SPATIE  

lighthouse-go
=============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Lighthouse-go](https://spatie.be/docs/lighthouse-go/v1)  Usage  Configuring a run

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/lighthouse-go/v1) 

  Configuring a run    
- [ Introduction ](https://spatie.be/docs/lighthouse-go/v1/introduction)
- [ Requirements ](https://spatie.be/docs/lighthouse-go/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/lighthouse-go/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/lighthouse-go/v1/questions-issues)
- [ Differences from PHP ](https://spatie.be/docs/lighthouse-go/v1/go-differences)
- [ Testing ](https://spatie.be/docs/lighthouse-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/lighthouse-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/lighthouse-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/lighthouse-go/v1/support-us)
- [ About us ](https://spatie.be/docs/lighthouse-go/v1/about-us)

Usage
-----

- [ Generating your first report ](https://spatie.be/docs/lighthouse-go/v1/usage/generating-your-first-report)
- [ Configuring a run ](https://spatie.be/docs/lighthouse-go/v1/usage/configuring-a-run)
- [ Working with results ](https://spatie.be/docs/lighthouse-go/v1/usage/working-with-results)
- [ Saving an HTML report ](https://spatie.be/docs/lighthouse-go/v1/usage/saving-an-html-report)
- [ Using performance budgets ](https://spatie.be/docs/lighthouse-go/v1/usage/using-performance-budgets)

 Configuring a run
=================

###  On this page 

1. [ Only run audits in certain categories ](#content-only-run-audits-in-certain-categories)
2. [ Skip certain audits ](#content-skip-certain-audits)
3. [ Only run specific audits ](#content-only-run-specific-audits)
4. [ Customizing the user agent ](#content-customizing-the-user-agent)
5. [ Setting extra headers ](#content-setting-extra-headers)
6. [ Specifying the form factor ](#content-specifying-the-form-factor)
7. [ Enable throttling ](#content-enable-throttling)
8. [ Customize the Lighthouse configuration ](#content-customize-the-lighthouse-configuration)
9. [ Customize the Chrome options ](#content-customize-the-chrome-options)
10. [ Setting a timeout ](#content-setting-a-timeout)
11. [ Handling errors ](#content-handling-errors)

There are various options to configure how Lighthouse should run. Pass these to `New` before calling `Run`. Here's an example where we set a custom user agent.

```
run, err := lighthouse.New("https://example.com", lighthouse.UserAgent("my-custom-user-agent"))
```

Check `err` before using `run`. Options apply in order. A configured run can be reused, including concurrently. Application maps are copied during construction.

Only run audits in certain categories
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

By default, Lighthouse will run audits of all categories. To only run the audits of certain categories, pass one or more categories you are interested in.

```
run, err := lighthouse.New("https://example.com",
    lighthouse.Categories(lighthouse.BestPractices, lighthouse.SEO),
)
```

The other categories are `Performance` and `Accessibility`. Unknown category values return an error. Use `lighthouse.Category(value)` to convert a string before passing it to `Categories`.

Skip certain audits
-----------------------------------------------------------------------------------------------------------------

To lower Lighthouse's execution time, you can opt to skip audits by passing their names to `SkipAudits`.

```
lighthouse.SkipAudits("is-on-https", "unused-javascript")
```

Only run specific audits
--------------------------------------------------------------------------------------------------------------------------------

You can opt to run only specific audits using `OnlyAudits`.

```
lighthouse.OnlyAudits("is-on-https", "document-title")
```

You cannot use `SkipAudits` and `OnlyAudits` at the same time. Lighthouse validates this combination.

If you want to run a specific audit and an entire other category, call `Categories` after `OnlyAudits`.

```
run, err := lighthouse.New("https://example.com",
    lighthouse.OnlyAudits("is-on-https"),
    lighthouse.Categories(lighthouse.SEO),
)
```

Customizing the user agent
--------------------------------------------------------------------------------------------------------------------------------------

To use a custom user agent, pass `lighthouse.UserAgent("my-custom-user-agent")`.

Setting extra headers
-----------------------------------------------------------------------------------------------------------------------

You can specify headers that will be sent along with all requests.

```
lighthouse.Headers(map[string]string{"MyExtraHeader": "value of the header"})
```

Headers and report data can contain credentials. Do not log reports, configuration or Node diagnostics indiscriminately. Input is passed over stdin, not in process arguments or a shell command.

Specifying the form factor
--------------------------------------------------------------------------------------------------------------------------------------

By default, Lighthouse will use a desktop profile to run audits. You can change this to mobile using `WithFormFactor`.

```
run, err := lighthouse.New("https://example.com",
    lighthouse.WithFormFactor(lighthouse.Mobile),
    lighthouse.ScreenEmulation(lighthouse.Screen{
        Mobile: true, Width: 390, Height: 844, DeviceScaleFactor: 2,
    }),
)
```

Form factor and screen emulation are separate. Emulation is disabled by default. `ScreenEmulation` replaces its settings. Zero dimensions leave Lighthouse's defaults in place. `DeviceScaleFactor` uses Lighthouse's actual setting name, correcting the PHP `deviceScaleRatio` spelling.

Enable throttling
-----------------------------------------------------------------------------------------------------------

By default, Lighthouse will not throttle CPU and connection speed. To enable throttling, use these options.

```
lighthouse.ThrottleCPU(true, 4)
lighthouse.ThrottleNetwork(true)
```

The higher the CPU multiplier, the more throttling is applied. Pass zero to leave the multiplier unchanged. You'll find more information [in the Lighthouse docs](https://github.com/GoogleChrome/lighthouse/blob/main/docs/throttling.md#cpu-throttling).

Pass `false` to disable either flag. Enabling CPU throttling selects `throttlingMethod: "simulate"`. Disabling it does not reset that method. Network throttling changes only the flag, matching PHP. Modern Lighthouse uses `throttlingMethod` for actual behavior. Use full configuration for exact simulation control.

Customize the Lighthouse configuration
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

To have fine-grained control of which options will be sent to Lighthouse, pass a map to `WithConfig`.

```
config := lighthouse.DefaultLighthouseConfig()
config["settings"].(map[string]any)["locale"] = "nl"
run, err := lighthouse.New("https://example.com", lighthouse.WithConfig(config))
```

This replaces the entire configuration, not just settings. Defaults extend `lighthouse:default`, select the four categories, desktop form factor, JSON and HTML output, no throttling, `throttlingMethod: "provided"` and disabled screen emulation. `DefaultLighthouseConfig()` returns a fresh map.

Configuration must be JSON-serializable and settings must be an object. Lighthouse validates its own schema. The wrapper returns both JSON and HTML even when your custom `output` omits JSON or reorders formats.

You can also write an `Option`, a `func(*lighthouse.Config) error`, to supply application-specific configuration. There are no fixed profile classes or global registrations to replace.

Customize the Chrome options
--------------------------------------------------------------------------------------------------------------------------------------------

Under the hood, Lighthouse will run an instance of Chrome to run the audits. Customize the options given to chrome-launcher using `WithChromeOptions`.

```
chrome := lighthouse.DefaultChromeOptions()
chrome["chromePath"] = "/path/to/chrome"
run, err := lighthouse.New("https://example.com", lighthouse.WithChromeOptions(chrome))
```

This replaces the entire options object. Defaults are `{"chromeFlags": ["--headless"]}`. Sandboxing is not disabled automatically.

Setting a timeout
-----------------------------------------------------------------------------------------------------------

By default, if Lighthouse takes more than 60 seconds it will be aborted. Adjust this with `Timeout`, using Go durations.

```
run, err := lighthouse.New("https://example.com",
    lighthouse.Timeout(120*time.Second),
    lighthouse.MaxWaitForLoad(5*time.Second),
)
```

`Timeout` covers the entire execution. `MaxWaitForLoad` controls only Lighthouse's page-load wait. Zero uses its default. Context cancellation also stops a run. Use `errors.Is(err, context.DeadlineExceeded)` and `errors.Is(err, context.Canceled)` to distinguish these outcomes.

On Unix the runner gets SIGTERM and has up to two seconds to close Chrome before Go kills Node. Windows falls back to terminating Node, so detached Chrome may require external cleanup. A hung Node or interrupted Chrome startup can also leave detached Chrome processes. Run audits in isolated workers when cleanup must be guaranteed. This package is not an SSRF filter. Restrict reachable destinations when auditing untrusted URLs.

Handling errors
-----------------------------------------------------------------------------------------------------

`New` returns validation and option errors. `Run` returns context errors, `*ProcessError` with the original launch or exit error and stderr, JSON decoding errors, or `*RuntimeError` with Lighthouse's code and message. Use `errors.As` for typed failures. `Parse` also validates saved envelopes and reports runtime errors.

Set `NodeBinary("/path/to/node")` to replace the Node executable and `WorkingDirectory("/path/to/app")` to select Node module resolution and relative Chrome paths.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
