# Lighthouse CI使用指南 ## 概述 Lighthouse CI是一个用于自动化性能测试的工具,它可以在CI/CD流程中运行Lighthouse审计,跟踪性能指标变化,并设置性能预算。 ## 安装 ```bash npm install --save-dev @lhci/cli ``` ## 配置 ### 配置文件 项目根目录下的`lighthouserc.json`文件包含所有配置: ```json { "ci": { "collect": { "numberOfRuns": 3, "startServerCommand": "npm run start", "startServerReadyPattern": "Local:", "url": [ "http://localhost:3000/", "http://localhost:3000/about", "http://localhost:3000/services" ], "settings": { "preset": "desktop", "onlyCategories": [ "performance", "accessibility", "best-practices", "seo" ] } }, "assert": { "assertions": { "categories:performance": ["error", {"minScore": 0.9}], "categories:accessibility": ["error", {"minScore": 0.9}], "categories:best-practices": ["error", {"minScore": 0.9}], "categories:seo": ["error", {"minScore": 0.9}] } }, "upload": { "target": "temporary-public-storage" } } } ``` ### 配置说明 #### collect配置 - **numberOfRuns**: 每个URL运行的次数(默认3次) - **startServerCommand**: 启动服务器的命令 - **startServerReadyPattern**: 服务器就绪的匹配模式 - **url**: 要测试的URL列表 - **settings**: Lighthouse设置 - **preset**: 测试预设(desktop/mobile) - **onlyCategories**: 只测试指定类别 #### assert配置 - **assertions**: 性能断言 - **categories:performance**: 性能分数最低0.9 - **categories:accessibility**: 可访问性分数最低0.9 - **categories:best-practices**: 最佳实践分数最低0.9 - **categories:seo**: SEO分数最低0.9 #### upload配置 - **target**: 上传目标 - **temporary-public-storage**: 临时公共存储 - **lhci**: Lighthouse CI服务器 - **filesystem**: 本地文件系统 ## 使用方法 ### 本地运行 #### 运行完整测试 ```bash npm run lighthouse ``` 这将执行以下步骤: 1. 启动开发服务器 2. 收集性能数据 3. 运行断言检查 4. 上传结果 #### 分步运行 ```bash # 1. 收集性能数据 npm run lighthouse:collect # 2. 运行断言检查 npm run lighthouse:assert # 3. 上传结果 npm run lighthouse:upload ``` #### 指定设备类型 ```bash # 桌面端测试 npm run lighthouse:desktop # 移动端测试 npm run lighthouse:mobile ``` ### CI/CD集成 #### GitHub Actions 创建`.github/workflows/lighthouse.yml`: ```yaml name: Lighthouse CI on: push: branches: [main] pull_request: branches: [main] jobs: lighthouse: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup Node.js uses: actions/setup-node@v3 with: node-version: '18' cache: 'npm' - name: Install dependencies run: npm ci - name: Build application run: npm run build - name: Run Lighthouse CI run: npm run lighthouse env: LHCI_GITHUB_APP_TOKEN: ${{ secrets.LHCI_GITHUB_APP_TOKEN }} - name: Upload Lighthouse results uses: actions/upload-artifact@v3 if: always() with: name: lighthouse-results path: lighthouse-reports ``` #### GitLab CI 创建`.gitlab-ci.yml`: ```yaml lighthouse: stage: test image: node:18 script: - npm ci - npm run build - npm run lighthouse artifacts: paths: - lighthouse-reports/ expire_in: 1 week only: - main - merge_requests ``` ## 性能指标 ### Core Web Vitals Lighthouse CI重点监控以下Core Web Vitals指标: | 指标 | 名称 | 目标值 | 说明 | |------|------|--------|------| | **LCP** | Largest Contentful Paint | < 2.5s | 最大内容绘制时间 | | **FID** | First Input Delay | < 100ms | 首次输入延迟 | | **CLS** | Cumulative Layout Shift | < 0.1 | 累积布局偏移 | ### 其他重要指标 | 指标 | 名称 | 目标值 | 说明 | |------|------|--------|------| | **FCP** | First Contentful Paint | < 1.8s | 首次内容绘制时间 | | **SI** | Speed Index | < 3.4s | 速度指数 | | **TBT** | Total Blocking Time | < 200ms | 总阻塞时间 | ### 性能预算 在`lighthouserc.json`中设置性能预算: ```json { "ci": { "assert": { "assertions": { "first-contentful-paint": ["error", {"maxNumericValue": 2000}], "largest-contentful-paint": ["error", {"maxNumericValue": 3000}], "cumulative-layout-shift": ["error", {"maxNumericValue": 0.1}], "total-blocking-time": ["error", {"maxNumericValue": 300}], "speed-index": ["error", {"maxNumericValue": 3000}] } } } } ``` ## 性能优化建议 ### 1. 减少LCP时间 **问题**:LCP > 2.5s **解决方案**: ```typescript // 优化图片加载 Hero image // 使用Next.js Image组件 Hero image ``` ### 2. 减少CLS **问题**:CLS > 0.1 **解决方案**: ```typescript // 为图片预留空间 Description // 避免内容跳动
{loading ? : }
``` ### 3. 减少TBT **问题**:TBT > 200ms **解决方案**: ```typescript // 代码分割 const HeavyComponent = dynamic(() => import('./HeavyComponent'), { loading: () => , }); // 延迟加载非关键脚本 useEffect(() => { import('heavy-library').then(module => { // 使用库 }); }, []); ``` ### 4. 优化FCP **问题**:FCP > 1.8s **解决方案**: ```typescript // 内联关键CSS