如何解决Lighthouse Node CLI 返回错误的数字
我正在尝试通过 Lighthouse Node CLI 在 Node js 中访问网站的性能评级。但是我遇到了这个问题,输出的分数远低于 Chrome DevTools 或 Google PageSpeed 中 Lighthouse 的分数。
例如:
在 Chrome 中,我获得了 https://google.com 的 97 性能评分,并具有以下指标:
在使用 Node 时,我只得到 66.05 的分数,其中包含以下指标:
我在 MacOS 11.2.3 和 Windows 上都试过了。
-
Chrome 版本:90.0.4430.85
-
灯塔:7.2.0
-
斧头版本:4.1.2
运行时设置是两者的标准设置并且是相同的。
提前谢谢你的帮助!
这是我基于此 blogpost 的代码:
const lighthouse = require("lighthouse");
const chromeLauncher = require("chrome-launcher");
const lighthouseAudit = url => {
return chromeLauncher.launch({chromeFlags: ['--headless']}).then(chrome => {
const opts = {port: chrome.port};
return lighthouse(url,opts).then(results => {
return chrome.kill().then(() => results.report);
});
});
};
lighthouseAudit("https://google.com").then(resultsJSON => {
var results = JSON.parse(resultsJSON);
const metricFilter = [
"first-contentful-paint","interactive","largest-contentful-paint","speed-index","cumulative-layout-shift","total-blocking-time"
];
let resultsscore = [];
for (let auditObj in results["audits"]) {
if (metricFilter.includes(auditObj)) {
resultsscore.push(new Array(auditObj,results.audits[auditObj].score));
}
}
console.log(resultsscore);
let score = (resultsscore[0][1]*0.15)+(resultsscore[1][1]*0.15)+(resultsscore[2][1]*0.25)+(resultsscore[3][1]*0.15)+(resultsscore[4][1]*0.25)+(resultsscore[5][1]*0.05);
console.log("score: " + score*100);
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。