问题描述
我正在尝试通过 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 (将#修改为@)