Working with results | 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  Working with results

 Version   v1      

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

  Working with results    
- [ 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)

 Working with results
====================

###  On this page 

1. [ Getting the category scores ](#content-getting-the-category-scores)
2. [ Getting audit results ](#content-getting-audit-results)
3. [ Performance metrics convenience methods ](#content-performance-metrics-convenience-methods)
4. [ Getting the configuration used to generate the result ](#content-getting-the-configuration-used-to-generate-the-result)
5. [ Getting the raw results ](#content-getting-the-raw-results)
6. [ Getting the benchmark index ](#content-getting-the-benchmark-index)
7. [ Restoring saved reports ](#content-restoring-saved-reports)

Running Lighthouse returns a `*lighthouse.Result`. On this page, we'll cover the methods you can call on it.

Getting the category scores
-----------------------------------------------------------------------------------------------------------------------------------------

You can use `Scores()` to get scores of the four categories Lighthouse runs audits for. Use `Score(category)` for one category.

```
scores := result.Scores()
accessibility, available := result.Score(lighthouse.Accessibility)
```

Scores are integer percentages truncated like PHP, not rounded like the Rust port. Real zero scores are retained. Missing and null scores are omitted from the map, and return `available == false` from `Score`.

Getting audit results
-----------------------------------------------------------------------------------------------------------------------

Lighthouse will run various audits on your website. You can learn more about the meaning of each audit in [the Lighthouse docs](https://developer.chrome.com/docs/lighthouse/overview).

```
names := result.AuditNames()
audit, err := result.Audit("first-contentful-paint")
audits := result.Audits()
```

`Audit` returns a `map[string]any`, or an error wrapping `lighthouse.ErrAuditNotFound` for a missing or null audit. `AuditNames` sorts identifiers alphabetically. `Audits` returns nil if no audits are present.

Performance metrics convenience methods
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

There are a few convenience methods to get the most interesting performance metrics. Those ending in `InMs` return milliseconds.

- `FirstContentfulPaintInMs()`
- `LargestContentfulPaintInMs()`
- `SpeedIndexInMs()`
- `TotalBlockingTimeInMs()`
- `TimeToInteractiveInMs()`
- `CumulativeLayoutShift()`
- `TotalPageSizeInBytes()`

Some have equivalents returning a formatted string such as `1.3 s`.

- `FormattedFirstContentfulPaint()`
- `FormattedLargestContentfulPaint()`
- `FormattedSpeedIndex()`
- `FormattedTotalBlockingTime()`
- `FormattedTimeToInteractive()`
- `FormattedCumulativeLayoutShift()`

Numeric methods return `(*float64, error)` and formatted methods return `(*string, error)`. A missing audit returns an error. An existing audit without the value returns nil without an error. A pointer to zero is a real measurement. Cumulative layout shift is unitless, and its formatted value is the original string. Time to interactive is a legacy, unscored audit whose availability depends on your Lighthouse version.

```
milliseconds, err := result.FirstContentfulPaintInMs()
if err != nil {
    return err
}
if milliseconds != nil {
    fmt.Println(*milliseconds)
}
```

`Metric(name)` and `FormattedMetric(name)` offer the same behavior for any audit.

Getting the configuration used to generate the result
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The result contains the settings used to run Lighthouse. Use dot notation for nested values.

```
settings := result.ConfigSettings("")
width := result.ConfigSettings("screenEmulation.width")
```

There are also convenience methods for interesting settings.

- `FormFactor()` returns `Desktop`, `Mobile` or the recorded custom value
- `UserAgent()` returns the recorded string, or an empty string
- `Headers()` returns recorded extra headers
- `NetworkThrottlingWasEnabled()` returns a boolean
- `CPUThrottlingWasEnabled()` returns a boolean

Absent disable flags count as enabled, matching PHP. The flags reflect submitted configuration, not independently measured throttling behavior.

Getting the raw results
-----------------------------------------------------------------------------------------------------------------------------

To get everything returned by the wrapper, use `RawResults()`. Successful live runs return `report: [json, html]` and `runtimeError: null`.

```
raw := result.RawResults()
version := result.RawResult("report.0.lighthouseVersion")
score := result.Get("categories.accessibility.score")
jsonReport := result.JSONReport()
```

`Get`, `RawResult` and `ConfigSettings` accept an empty path for the entire corresponding value. Paths support objects and array indexes, with exact object keys taking precedence. Missing paths and JSON null both return nil. For a presence distinction, inspect the corresponding map with Go's two-value lookup.

Reports use standard JSON types, including float64 numbers. Returned maps and slices belong to the result. Mutations change it, so do not mutate them concurrently with reads or replace the envelope's report slots with incompatible types.

Getting the benchmark index
-----------------------------------------------------------------------------------------------------------------------------------------

Lighthouse computes a benchmark index as a rough approximation of the host device's CPU performance. `BenchmarkIndex()` returns `(float64, bool)` so fractional values are preserved and absent values are distinguishable. `LighthouseVersion()` returns the engine version.

Restoring saved reports
-----------------------------------------------------------------------------------------------------------------------------

`Parse(data)` accepts a serialized runner envelope, including older PHP envelopes with extra fields and JSON strings in `report[0]`. `FromReports(jsonReport, htmlReport)` accepts separately stored reports. Both validate the report shape and return runtime errors recorded in it.

Use `SetJSONReport(report)` and `SetHTMLReport(html)` to replace stored reports. The JSON setter returns an error and leaves the result unchanged when validation fails. Construct a result with `FromReports(map[string]any{}, "")` rather than a zero-value `Result`.

 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)
