Browser Performance Budget Enforcement: CI Checks, Budgets, and Alerts
I'll guide you through building a complete performance budget enforcement system for your web app. This uses Lighthouse CI (LHCI), which audits Core Web Vitals and custom metrics, enforces budgets in CI (e.g., GitHub Actions), and triggers alerts.
Why This System?
- Budgets: Hard thresholds for metrics like LCP, CLS, FID, TTI.
- CI Checks: Blocks PR merges if budgets fail.
- Alerts: Slack/email notifications on regressions.
- Zero-config start: Works with any static/SPA site (Next.js, React, etc.).
Step 1: Define Your Performance Budgets
Budgets are JSON thresholds. Start conservative; iterate based on baselines.
Create .lighthouserc.js (or lighthouserc.json) in your repo root:
module.exports = {
ci: {
collect: {
url: ['http://localhost:3000'], // Your dev server URL (override in CI)
numberOfRuns: 3, // Average over runs for stability
},
assert: {
assertions: {
'categories:performance': ['warn', { minScore: 0.9 }], // 90+ perf score
'largest-contentful-paint': ['warn', { maxNumericValue: 2500 }], // LCP ≤2.5s
'cumulative-layout-shift': ['warn', { maxNumericValue: 0.1 }], // CLS ≤0.1
'first-input-delay': ['warn', { maxNumericValue: 100 }], // FID ≤100ms
'total-blocking-time': ['warn', { maxNumericValue: 300 }], // TBT ≤300ms
'speed-index': ['warn', { maxNumericValue: 4000 }], // SI ≤4s
// Custom: Add e.g., 'uses-webp-images', 'offscreen-images'
},
},
upload: {
target: 'temporary-public-storage', // Or GitHub/LHCI server
},
},
};
Tune budgets:
- Run
npx lighthouse https://your-site.com locally to baseline.
- PRs:
warn → fails CI softly (status check).
- Main:
error → hard fail.
Step 2: Install & Setup Lighthouse CI
npm install --save-dev @lhci/cli
# Or yarn add -D @lhci/cli
Add scripts to package.json:
{
"scripts": {
"lighthouse": "lhci autorun",
"lighthouse:collect": "lhci collect",
"lighthouse:assert": "lhci assert"
}
}
Step 3: CI Integration (GitHub Actions)
Create .github/workflows/perf-budget.yml:
name: Performance Budgets
on:
pull_request:
branches: [main, develop]
push:
branches: [main]
jobs:
lhci:
name: Lighthouse CI
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- name: Install deps
run: npm ci
- name: Build app
run: npm run build # e.g., next build, vite build
- name: Start server
run: |
npx serve build -l 3000 -s & # Static serve; adapt for SPA/SSR
sleep 5
- name: Run Lighthouse CI
run: npm run lighthouse
env:
LHCI_GITHUB_APP_TOKEN: ${{ secrets.LHCI_GITHUB_APP_TOKEN }} # Optional for GitHub integration
- name: Assert budgets
if: github.event_name == 'pull_request'
run: npm run lighthouse:assert
Key Features:
- Runs on PRs/pushes to
main.
- Serves your built app (adapt
serve for Next.js: npm start).
- Fails PR if budgets exceeded (add to GitHub branch protection: Require "Performance Budgets" status).
- View reports: LHCI auto-uploads to temporary storage or lighthouserc.net (free).
Branch Protection (GitHub Settings > Branches):
- Require status checks:
Performance Budgets.
Step 4: Alerts & Notifications
Slack Alerts
Add to workflow (after lhci step):
- name: Slack Alert on Failure
if: failure()
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
fields: repo,message,commit,author,action,eventName,ref,workflow
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
Email/Discord (via LHCI Server)
For persistent storage + alerts:
- Deploy free LHCI server:
npx @lhci/cli server --port=3001.
- Or use GitHub App integration.
- Config: Set
LHCI_SERVER_BASE_URL env + token.
- Webhooks: LHCI dashboard → Slack/Discord on regressions.
Advanced Alerts: Custom Script
Add alert.js for metric-specific Slack:
// Run post-LHCI: node alert.js
const fs = require('fs');
const lighthouseResults = JSON.parse(fs.readFileSync('.lighthouseci/lhr-run-1.json'));
const lcp = lighthouseResults.audits['largest-contentful-paint'].numericValue;
if (lcp > 2500) {
// POST to Slack webhook
console.log('🚨 LCP exceeded: ' + lcp + 'ms');
}
Step 5: Production Monitoring & Extras
Testing It
- Push
.lighthouserc.js + workflow.
- Open PR → See "Perf Budgets" check.
- Simulate failure: Worsen budgets → CI fails + Slack alert.
Troubleshooting
| Issue | Fix |
|---|
url unreachable | Ensure server starts (e.g., wait-on http://localhost:3000) |
| Flaky runs | Increase numberOfRuns: 5, use staticDistDir. |
| SSR/Next.js | startServerAndTest start http://localhost:3000 "lhci autorun" |
| Custom metrics | Add audits via plugins. |
This enforces perf like a pro! Track regressions, block deploys. Questions? Paste your setup/error. 🔍