Performance · CI/CD · Lighthouse · Web

Performance Budgets with Lighthouse CI: Catching Slow Pages Before They Merge

Setting up Lighthouse CI and a performance budget: lighthouserc settings, score and metric thresholds, a JavaScript size limit, reducing measurement noise and GitHub Actions.

Sander Yazıcıoğlu Published: 3 min read Türkçe oku

Performance Budgets with Lighthouse CI: Catching Slow Pages Before They Merge

Getting a site's Lighthouse score to 100 can take a week; keeping it at 100 six months later is a separate discipline. On corporate websites I kept seeing the same pattern: a page that is fast on launch day slowly gets heavier with a slider added here, a new analytics tag there and a few uncompressed images. Nobody breaks anything; nobody measures either.

On this site, validate-seo.js, the last step of the build, stops the build when a rule such as description length or a broken link is violated. Lighthouse CI applies the same idea to performance: if the budget you set is exceeded, the pull request turns red.

Setup

npm install --save-dev @lhci/cli

A lighthouserc.json in the project root:

{
  "ci": {
    "collect": {
      "staticDistDir": "./dist",
      "url": ["/", "/blog/", "/contact/"],
      "numberOfRuns": 3
    },
    "assert": {
      "assertions": {
        "categories:performance": ["error", { "minScore": 0.9, "aggregationMethod": "median-run" }],
        "categories:accessibility": ["error", { "minScore": 0.95 }],
        "categories:seo": ["error", { "minScore": 0.95 }],
        "largest-contentful-paint": ["warn", { "maxNumericValue": 2500, "aggregationMethod": "median-run" }],
        "cumulative-layout-shift": ["error", { "maxNumericValue": 0.1 }],
        "resource-summary:script:size": ["error", { "maxNumericValue": 250000 }],
        "resource-summary:image:size": ["warn", { "maxNumericValue": 400000 }],
        "resource-summary:third-party:count": ["warn", { "maxNumericValue": 5 }]
      }
    },
    "upload": { "target": "temporary-public-storage" }
  }
}

Run it:

npm run build
npx lhci autorun

With staticDistDir, LHCI serves the build output with its own small server, so you do not need to start one. For a server-rendered app, use startServerCommand instead.

What should the budget be based on?

Lighthouse metrics fall into two groups, and they should not be treated the same way:

TypeExampleRun-to-run varianceSuggested level
DeterministicJavaScript size, request count, CLSVery lowerror
Timing-basedLCP, TBT, performance scoreNoticeable, depends on the CI machinewarn or a loose error

My preference is to tie the rules that hard-block a PR to deterministic metrics. "The JavaScript bundle may not exceed 250 KB" gives the same result on every run and immediately catches a developer adding a heavy library without noticing. Timing metrics show the trend as warnings.

Set the starting values by measuring the current state, not by guessing: put them slightly above today's numbers and tighten them as you improve. A budget should be a line that prevents regressions, not a target the team never meets.

Reducing the noise

CI machines are shared, so the same page can score differently on two runs.

  • numberOfRuns: 3 or 5: do not trust a single measurement.
  • aggregationMethod: "median-run": takes the score from the median run, so one lucky or unlucky run does not decide the result.
  • Few but representative URLs: measure each template once rather than every page: home, listing page, detail page.
  • Consistent settings: the mobile profile is the default; if you also want desktop, use a separate configuration with "settings": { "preset": "desktop" }.

GitHub Actions integration

lighthouse:
  runs-on: ubuntu-latest
  steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-node@v4
      with: { node-version: 22, cache: npm }
    - run: npm ci
    - run: npm run build
    - run: npx lhci autorun
      env:
        LHCI_GITHUB_APP_TOKEN: ${{ secrets.LHCI_GITHUB_APP_TOKEN }}

LHCI_GITHUB_APP_TOKEN is optional; if you install the Lighthouse CI GitHub app, results show up as a separate status check on the PR. temporary-public-storage uploads the report to a link that stays available for a few days; for a permanent history you need to run your own LHCI server. For the rest of the pipeline, see the GitHub Actions frontend CI post.

When the budget is exceeded

A red check only helps if it is clear what to do next. The violations I see most often and where to look first:

  • Script size: a newly added dependency. Move it to the relevant page with a dynamic import, or find a lighter alternative.
  • Image size: an unoptimised hero image. AVIF/WebP and a correct srcset; details in the image optimisation post.
  • CLS: an image without dimensions or a banner that loads late.
  • Third-party count: a new script added through the tag manager. Is it really needed, and can it load later?

Lab data is not enough

Lighthouse is a lab tool: it measures on a fixed device and network profile. It does not show what real users experience, especially interaction latency (INP); in the lab you look at TBT instead. Lighthouse CI catches regressions before merge, while field data tells you what users actually go through. For how to combine the two, see the Core Web Vitals post.

Conclusion

Performance is not a one-off task; it is a feature that has to be protected. With Lighthouse CI, put a strict budget on deterministic metrics and a flexible one on timing metrics, repeat each measurement a few times, and start the budget from where you are today and tighten it over time. That way the site's speed does not slowly erode through small additions nobody notices.

All articles →