本指南適用於符合下列條件的 Lighthouse 第 2 版使用者:
- 透過 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 第 3 版傳回的 JSON 物件現在包含三個頂層屬性:
- lhr。稽核結果。是「Lighthouse 結果」的縮寫。這基本上是 v2 中的頂層物件,但 v3 也對此物件的形狀進行重大變更。請參閱「結果物件異動」。
- artifacts。稽核期間從 Chrome 收集的資料。這項屬性先前與 LHR 的屬性混淆。
- report:以字串格式呈現的 HTML/JSON/CSV 報表。
結果物件的變更
如「全新的 JSON 輸出內容頂層格式」一文所述,無法透過 lhr 屬性提供稽核結果。在第 2 版中,這個物件的內容基本上是頂層 JSON 輸出。不過,這個物件本身的形狀在第 3 版中已有所改變。下表列出所有變更。
- 如果資料列在 v2 和 v3 欄中都有值,表示您應該將程式碼中任何對 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.explanationaudits.ID.errorMessageaudits.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。 |