如何在发布前自动运行 Lighthouse 性能评分?
解读
面试官真正想确认的是:
- 你是否理解 Vaadin 应用最终仍是一个运行在浏览器里的 SPA,其首屏、TTI、CLS 等指标对用户体验同样关键;
- 你是否能把“前端性能”纳入 Java 后端的 CI/CD 流程,而不是等上线后由测试同学手动跑 Chrome;
- 你是否熟悉国内常用的流水线(GitLab-CI、Jenkins、阿里云云效、腾讯蓝盾、GitHub Actions 镜像加速等)以及 Lighthouse 在无头环境下的运行方式;
- 你是否能把 Lighthouse 评分失败(如 performance < 80)当成阻塞发布的质量门,而不是一个“参考报告”。
一句话:让 Lighthouse 像单元测试一样,在“mvn deploy”或“docker build”之前必须绿灯,否则版本进不了制品库。
知识点
- Lighthouse CLI 与 Chrome --no-sandbox --headless 在国内 CentOS/Ubuntu 容器中的安装要点;
- Maven 生命周期扩展:maven-antrun-plugin、exec-maven-plugin 或 frontend-maven-plugin 调本地 Node;
- Vaadin 生产构建:vaadin-maven-plugin 的 build-frontend 目标,以及生成的 build/webapp 目录结构;
- 国内镜像源:npm --registry=https://registry.npmmirror.com、chrome-launcher 下载 Chromium 的淘宝 CDN;
- CI 质量门:Jenkins 的 pipeline step “recordIssues”、GitLab-CI 的 artifacts:reports:performance、GitHub Actions 的 actions/upload-artifact;
- Lighthouse-CI(LHCI)与 Node 版本兼容性,以及断言文件 lighthouserc.json 的写法(assertions: performance > 0.8, first-contentful-paint < 2000);
- 多环境评分策略:dev 环境只跑 PWA+Best Practices,staging 跑完整 5 项并落库,生产只做采样;
- 失败回滚:CI 中把 Lighthouse 得分作为“测试通过率”一项,低于阈值自动终止后续 deploy 阶段,并发送企业微信/飞书机器人告警。
答案
以国内最常见的“GitLab-CI + Maven + 私有 Harbor”为例,给出可直接落地的最小闭环:
-
在 pom.xml 中新增 profile“lighthouse”,绑定到 verify 阶段:
- 先用 vaadin-maven-plugin 完成生产构建;
- 再用 exec-maven-plugin 调用已装好的 Lighthouse CLI;
- 把报告输出成 json 与 html 两份,放在 target/lighthouse 目录。
-
准备 .gitlab-ci.yml:
stage: performance
image: maven:3.9-eclipse-temurin-17
services:- docker:24-dind
before_script: - curl -fsSL https://deb.nodesource.com/setup_18.x | bash
- apt-get install -y nodejs chromium
- npm install -g lighthouse@latest --registry=https://registry.npmmirror.com
script: - mvn clean package -Plighthouse
- lighthouse http://localhost:8080 --chrome-flags='--headless --no-sandbox --disable-gpu' --output-path=./target/lighthouse/report.json --output=json
- node scripts/lighthouse-assert.js # 自定义断言,performance>=80
artifacts:
when: always
paths:- target/lighthouse/
expire_in: 7 days
only:
- target/lighthouse/
- tags # 只在发版标签时触发
allow_failure: false # 评分不过,发布终止
- docker:24-dind
-
lighthouse-assert.js 核心逻辑:
const report = require('./target/lighthouse/report.json');
const score = report.categories.performance.score * 100;
if (score < 80) {
console.error(Performance ${score} < 80, release blocked!);
process.exit(1);
} -
私有 Nexus/NPM 镜像加速、Chrome 下载失败重试、容器内存不足等常见坑,提前在 CI 日志里给出 fallback 脚本,保证面试官知道你“踩过坑”。
这样,每次打正式版本标签时,GitLab Runner 会自动拉起容器,构建 Vaadin 生产包,启动 jar,跑 Lighthouse,若性能分数不过关,Maven 返回非 0,CI 直接失败,镜像推不到 Harbor,实现“发布前强制性能门禁”。
拓展思考
- 如果项目采用“前后端分离”模式,Vaadin 仅作为 BFF,是否还需要跑 Lighthouse?——需要,因为 Vaadin 的 bootstrap.js 与 Web Component 仍会影响首屏,此时可把 Lighthouse 的 URL 指向由 Vaadin 提供的 server-side 渲染入口,而不是纯静态 index.html。
- 微前端场景下,多个 Vaadin 子应用聚合到同一门户,如何拆分性能指标?——可在 CI 中为每个子应用单独跑 Lighthouse,再使用 lhci upload 把各子报告汇总到同一仓库,门户层只做“总体评分加权”。
- 国内客户常要求“信创环境”无外网,Chromium 下载失败怎么办?——提前把 chromium 离线包做成自定义 Docker 镜像,推送到私有仓库,CI 直接引用该镜像,避免每次现装。
- 评分波动:同一版本连续两次跑分可能差 5 分,怎样防止误杀?——引入“三次平均”策略,CI 里循环跑 3 次 Lighthouse,去掉最高最低取中间值,再与阈值比较。
- 与业务挂钩:把 Lighthouse 的“首屏截图”自动上传到 Confluence 版本纪要,让产品经理也看到性能变化,形成“技术-业务”共同关注的质量文化。