NashTech Blog

Getting Started with Lighthouse CI: A Comprehensive Guide

Table of Contents

Introduction

In the digital age, performance is paramount. Slow, inefficient websites drive users away, impacting engagement, conversion rates, and search engine rankings. To address this, web developers employ various tools to monitor and enhance site performance. One such tool is Lighthouse CI (Continuous Integration), a powerful utility that automates performance testing and reporting. This guide will walk you through getting started with Lighthouse CI, from understanding its importance to setting it up and integrating it into your development workflow.

What is Lighthouse CI?

getting

Lighthouse is an open-source, automated tool for improving the quality of web pages. It audits performance, accessibility, SEO, and more, providing developers with actionable insights. Lighthouse CI takes this a step further by integrating Lighthouse audits into your CI/CD pipeline, ensuring that your web performance remains optimal throughout the development lifecycle. It allows you to catch regressions early and maintain a high-quality user experience.

Why Use Lighthouse CI?

  1. Automated Performance Monitoring: Continuously monitor your website’s performance, ensuring that every new change maintains or improves site quality.
  2. Catch Regressions Early: Detect performance regressions before they reach production, saving time and resources.
  3. Comprehensive Reports: Generate detailed reports that highlight areas for improvement, making it easier to optimize your site.
  4. Integrates with CI/CD Pipelines: Seamlessly integrates with popular CI/CD tools like Jenkins, Travis CI, and GitHub Actions.

Prerequisites

Before you begin, ensure you have the following:

  • Node.js and npm installed on your machine.
  • Basic knowledge of CI/CD pipelines.
  • A GitHub account (if you plan to use GitHub Actions).

Setting Up Lighthouse CI

Step 1: Install Lighthouse CI

First, install Lighthouse CI globally on your machine using npm:

npm install -g @lhci/cli

Step 2: Configure Lighthouse CI

Create a .lighthouserc.json configuration file in the root of your project. This file tells Lighthouse CI how to run Lighthouse and where to store the reports.

{
  "ci": {
    "collect": {
      "startServerCommand": "npm start",
      "url": ["http://localhost:3000"],
      "numberOfRuns": 3
    },
    "assert": {
      "assertions": {
        "categories:performance": ["error", {"minScore": 0.9}]
      }
    },
    "upload": {
      "target": "temporary-public-storage"
    }
  }
}

Step 3: Run Lighthouse CI Locally

To ensure everything is set up correctly, run Lighthouse CI locally:

lhci autorun

This command will start your server, run Lighthouse audits, and upload the results to temporary public storage. You should see a summary of the results in your terminal.

Integrating Lighthouse CI with CI/CD Pipelines

GitHub Actions

Integrating Lighthouse CI with GitHub Actions is straightforward. Create a .github/workflows/lighthouse-ci.yml file in your repository with the following content:

name: Lighthouse CI

on:
  push:
    branches:
      - main
  pull_request:

jobs:
  lhci:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Install Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '14'

      - name: Install dependencies
        run: npm install

      - name: Build the project
        run: npm run build

      - name: Install Lighthouse CI
        run: npm install -g @lhci/cli

      - name: Run Lighthouse CI
        run: lhci autorun

This workflow will run Lighthouse CI on every push to the main branch and every pull request, ensuring that your website’s performance remains top-notch.

Jenkins

To integrate Lighthouse CI with Jenkins, add the following steps to your Jenkins pipeline:

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Install Dependencies') {
            steps {
                sh 'npm install'
            }
        }

        stage('Build') {
            steps {
                sh 'npm run build'
            }
        }

        stage('Run Lighthouse CI') {
            steps {
                sh 'npm install -g @lhci/cli'
                sh 'lhci autorun'
            }
        }
    }
}

This Jenkins pipeline will execute Lighthouse CI tests as part of your build process, ensuring continuous performance monitoring.

Analyzing Lighthouse CI Reports

After running Lighthouse CI, you will receive detailed reports highlighting various performance metrics and recommendations. Here are some key aspects to focus on:

  1. Performance Score: This is a numeric representation of your site’s performance. Aim for a score of 90 or above.
  2. Opportunities: These are suggestions to improve your performance score, such as optimizing images, reducing JavaScript execution time, and more.
  3. Diagnostics: Detailed insights into how your site is performing, including metrics like First Contentful Paint (FCP), Largest Contentful Paint (LCP), and Time to Interactive (TTI).

Regularly reviewing and acting on these reports will help you maintain an optimized, high-performing website.

Best Practices for Using Lighthouse CI

  1. Run Multiple Audits: Perform multiple runs to get a more accurate representation of your site’s performance.
  2. Set Performance Budgets: Define performance budgets for your project and configure Lighthouse CI to alert you when these budgets are exceeded.
  3. Integrate Early: Integrate Lighthouse CI early in your development process to catch regressions before they become costly.
  4. Use Temporary Public Storage: Use the temporary public storage feature for quick and easy sharing of Lighthouse reports.
  5. Automate with Webhooks: Use webhooks to automate actions based on Lighthouse CI results, such as posting reports to Slack or creating GitHub issues for failed assertions.

Conclusion

Lighthouse CI is an invaluable tool for web developers aiming to maintain high-performance websites. By integrating it into your CI/CD pipeline, you can continuously monitor and improve your site’s performance, ensuring a seamless and fast user experience. This guide has provided you with the foundational steps to get started with Lighthouse CI. As you become more familiar with the tool, you can explore its advanced features and further tailor it to your project’s needs.

Incorporate Lighthouse CI into your workflow today and take a proactive approach to web performance optimization. Happy auditing!

I hope this gave you some useful insights. Please feel free to drop any comments, questions or suggestions. Thank You !!!

Picture of Riya

Riya

Riya is a DevOps Engineer with a passion for new technologies. She is a programmer by heart trying to learn something about everything. On a personal front, she loves traveling, listening to music, and binge-watching web series.

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

Scroll to Top