Lighthouse v3 遷移指南

本指南適用於符合以下條件的 Lighthouse v2 使用者:

  • 透過節點或指令列執行 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 物件現在包含 3 個頂層屬性:

  • lhr:稽核結果。「Lighthouse 結果」的簡稱。這基本上就是 v2 中的頂層物件,但第 3 版也會為此物件形狀導入破壞性變更。請參閱結果物件變更
  • artifacts。在稽核時從 Chrome 收集的資料。這個代碼先前與 LHR 的屬性互動。
  • report。格式化的報表 HTML/JSON/CSV (以字串表示)。

結果物件變更

如「JSON 輸出內容中的全新頂層格式」一文所述,您現在可以透過 lhr 屬性查看稽核結果。在 v2 中,這個物件的內容基本上是頂層 JSON 輸出。不過,這個物件本身的形狀在 v3 中已經變更。下表列出所有變更。

  • 如果資料列同時在 v2v3 資料欄都含有值,代表您應該將程式碼中 v2 屬性的所有參照替換為對等的 v3。
  • 當資料列的 v3 資料欄沒有值時,「Notes」欄會說明您的選項。
  • 請注意,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 值已根據其意圖,轉換為上述三種屬性之一。
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