Lighthouse

repository·main·Indexed 12 days ago

https://github.com/googlechrome/lighthouse

An automated tool for improving web page quality by analyzing web apps and pages to collect performance metrics and provide insights on developer best practices. Version 13.4.1 supports programmatic use as a Node module, CLI auditing, trace processing, and integration with Puppeteer for authenticated page testing.

Tokens
49.5K
Snippets
137
Records
278
Agent score
90%

What's inside Lighthouse

  1. Generate reports from Lighthouse Results (LHR)

    main

    The Lighthouse Report Generator is the entry point for transforming a Lighthouse Result object (LHR) into human-readable or machine-readable formats. It supports generating reports in HTML, JSON, and CSV formats.

    Runtime Environments

    • Node.js: Runs natively.
    • Browser: Can be run in a browser environment after a compilation step using inline-fs. This step replaces fs.readFileSync() calls with the actual stringified file content to allow for file access in environments without a filesystem.
  2. Available Lighthouse Docs & Recipes

    main

    Lighthouse provides several specialized guides and recipes for advanced usage:

    Documentation Topics

    • Variability: Dealing with variance in results.
    • Programmatic Usage: Using Lighthouse within your own code.
    • Authentication: Testing sites that require login.
    • Plugins: Developing custom Lighthouse plugins.
    • Audits: Making a new audit.
    • Mobile Testing: Testing on mobile devices.
    • Architecture: Understanding how Lighthouse works internally.

    Recipes

    • Plugin Recipe: An example of a Lighthouse plugin.
    • Custom Audit Recipe: How to extend Lighthouse and run your own audits.
  3. What is a Lighthouse Plugin?

    main

    A Lighthouse plugin is a Node module that extends Lighthouse functionality by implementing a set of custom checks (audits) and adding them to the report as a new category. Plugins are ideal for sharing domain-specific insights via NPM and provide a semver-stable API.

    Plugin vs. Custom Config

    Use a Plugin if you need to:

    • Include custom audits.
    • Add a custom category.
    • Share functionality easily via NPM.
    • Ensure a stable API across Lighthouse versions.

    Use a Custom Config if you need to:

    • Gather custom data from the page (artifacts).
    • Modify core Lighthouse categories.
    • Modify config.settings properties.
  4. Control Lighthouse lifecycle with Gather and Audit modes

    main

    You can run specific subsets of the Lighthouse lifecycle using the --gather-mode (-G) and --audit-mode (-A) flags. This is useful for separating the browser interaction/artifact collection phase from the analysis phase.

    • Gather Mode (-G): Launches the browser, collects artifacts (traces, screenshots, etc.), and saves them to disk (defaults to ./latest-run/), then quits.
    • Audit Mode (-A): Skips browser interaction, loads artifacts from disk, runs audits on them, and generates a report.
    • Combined (-GA): Performs both gathering and auditing in one run, but also saves the artifacts to disk for later use with -A.

    You can provide a custom directory for these artifacts by passing a path to the flag (e.g., -GA=./my-artifacts).

    # 1. Collect artifacts only
    lighthouse http://example.com -G
    
    # 2. Run audits using previously collected artifacts
    lighthouse http://example.com -A
    
    # 3. Collect and save artifacts, then immediately audit
    lighthouse http://example.com -GA
    
    # 4. Use a custom directory for artifacts
    lighthouse -GA=./custom-dir https://example.com
  5. Auditing User Flows (Fraggle Rock)

    main

    Lighthouse supports auditing user flows, which involves a specialized audit phase and specific configuration context. Key features introduced in version 9.5.0 include:

    • A separate audit phase specifically for flows.
    • The ability to audit a flow from an artifacts.json file.
    • Use of frame url in the gather context.
    • The addition of logLevel to the config context.
  6. Use Lighthouse as a trace processor

    main

    Lighthouse can analyze trace and performance data (like .trace.json and .devtoolslog.json) collected from other tools like WebPageTest or ChromeDriver. You can provide absolute paths to these files in your configuration to perform trace-only runs.

    ```json
    {
      "settings": {
        "auditMode": "/User/me/lighthouse/core/test/fixtures/artifacts/perflog/",
      },
      "audits": [
        "user-timings",
        "critical-request-chains"
      ],
      "categories": {
        "performance": {
          "name": "Performance Metrics",
          "description": "These encapsulate your web app's performance.",
          "audits": [
            {"id": "user-timings", "weight": 1},
            {"id": "critical-request-chains", "weight": 1}
          ]
        }
      }
    }

    Run with: lighthouse --config-path=config.json http://www.random.url

  7. Performance scoring changes in Lighthouse v8.0.0

    main

    In version 8.0.0, the Performance Category underwent significant scoring changes to better reflect the current state of the web. These changes include:

    • Reweighted Performance Score: The overall weighting of the Performance score was adjusted.
    • Updated Score Curves: The curves for Total Blocking Time (TBT) and First Contentful Paint (FCP) were updated.
    • Updated CLS Definition: Cumulative Layout Shift (CLS) was updated to use its new, windowed definition.

    For a detailed breakdown of how these changes affect your scores, refer to the v8.0 Performance FAQ.

  8. Understand the LHR Round Trip Flow

    main

    The LHR (Lighthouse Result) round trip flow describes how a Lighthouse JSON report is processed through protocol buffers to ensure data integrity. The flow consists of two main stages:

    1. Compiling the Proto: The lighthouse_result.proto file is compiled using protoc (e.g., --python_out) to generate the lighthouse_result.pb2 file.
    2. Making a Round Trip JSON: A raw lhr.json is passed through proto_preprocessor.js to create lhr_processed.json, which is then processed by json_roundtrip_via_proto.py to produce the final lhr.round_trip.json.

    Note: During round trips, the order of JSON keys and lists may be jumbled.

  9. Understand Total Blocking Time (TBT) in Lighthouse v5.2.0

    main

    Lighthouse v5.2.0 introduced Total Blocking Time (TBT) as an experimental metric. TBT serves as a companion to Time to Interactive (TTI) to help prioritize unblocking the main thread.

    Note: Because it is experimental, TBT is unscored and appears only in the raw Lighthouse Result (LHR) JSON, not in the HTML report.