Lighthouse v3 迁移指南

本指南适用于符合以下条件的 Lighthouse v2 用户:

  • 从 Node 或命令行运行 Lighthouse。
  • 依赖于 Lighthouse 的 JSON 输出。

如果这些都不适合您,那么运行 Lighthouse 的工作流程大致相同。如需简要了解新功能和变更,请参阅发布 Lighthouse 3.0

调用更改

Lighthouse 现在默认计算模拟性能,并且节流设置已发生重大变化。

CLI 标志

场景 v2 标志 v3 标志
开发者工具 3G 节流 无(默认行为) --throttling-method=devtools
无节流 --disable-network-throttling --disable-cpu-throttling --throttling-method=provided
网络节流,不节流 CPU --disable-cpu-throttling --throttling-method=devtools --throttling.cpuSlowdownMultiplier=1
运行效果审核 --perf --preset=perf
对混合内容进行审核 --mixed-content --preset=mixed-content

节点模块

在 Lighthouse v3 中,Node 模块接受与 CLI 相同的配置选项。这是一项重大变更,因为许多选项在 v2 中被忽略了,而现在它们实际上会影响 Lighthouse 的运行方式。

const fs = require('fs');
const lighthouse = require('lighthouse');
async function run() {
  // `onlyCategories` was previously only available as a config setting.
  // `output` was previously only available in CLI.
  const flags = {onlyCategories: ['performance'], output: 'html'};
  const html = (await lighthouse('https://google.com/', flags)).report;
  fs.writeFileSync('report.html', html);
}

输出更改

JSON 输出中的新顶级格式

Lighthouse v3 返回的 JSON 对象现在包含三个顶级属性:

  • lhr。审核结果。是“Lighthouse 结果”的缩写。这在 v2 中本质上是顶级对象,但 v3 也对此对象的形状做出了重大更改。请参阅对结果对象的更改
  • artifacts。审核时从 Chrome 收集的数据。此属性之前与 LHR 的属性混杂在一起。
  • report。采用字符串格式的 HTML/JSON/CSV 格式报告。

对结果对象的更改

JSON 输出中的新顶级格式中所述,您无法通过 lhr 属性获取审核结果。在 v2 中,此对象的内容本质上是顶级 JSON 输出。不过,此对象本身的形状在 v3 中已发生变化。下表列出了所有更改。

  • 如果某行同时在 v2v3 列中都有值,则表示您应将代码中对 v2 属性的所有引用替换为 v3 等效项。
  • 如果某行在 v3 列中没有值,备注列会说明您的选项。
  • 请注意,ID 等项表示占位符文本。
v2 媒体资源 v3-等效项 备注
initialUrl requestedUrl
url finalUrl
generatedTime fetchedTime
reportCategories categories 已从数组更改为键值对对象。
reportGroups categoryGroups
audits.ID.name audits.ID.id
audits.ID.description audits.ID.title
audits.ID.helpText audits.ID.description
audits.ID.scoringMode audits.ID.scoreDisplayMode 可能的值已扩展为 numeric|binary|manual|informative|not-applicable|error
audits.ID.score audits.ID.score scoreDisplayMode 为数字或二进制时,得分始终是介于 0 到 1 之间的数字(而非 0-100)。对于其他显示模式,得分始终为 null,因为没有通过/失败的概念。
audits.ID.displayValue audits.ID.displayValue 现在可以是用于字符串插值的 printf 格式参数数组。
audits.ID.debugString audits.ID.explanation audits.ID.errorMessage audits.ID.warnings debugString 值已根据其 intent 转换为上述三个属性之一。
audits.ID.details audits.ID.details 细节结构已经转向更易于消耗。.items 中的每个条目都是一个具有可靠键的对象,而不是 any[]
audits.ID.error audits.ID.scoreDisplayMode === 'error'
audits.ID.notApplicable audits.ID.scoreDisplayMode === 'not-applicable'
audits.ID.informative audits.ID.scoreDisplayMode === 'informative'
audits.ID.manual audits.ID.scoreDisplayMode === 'manual'
audits.ID.extendedInfo 已移除。请改用 details