本指南適用於符合下列條件的 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 |
Node 模組
在 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
屬性取得稽核結果。在 v2 中,這個物件的內容基本上是頂層 JSON 輸出內容。不過,這個物件本身的形狀在 v3 中已有所變更。下表列出所有變更。
- 如果資料列在 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.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 。 |