[英]How to use chrome-aws-lambda module with AWS lambda functions?
[英]chrome-aws-lambda + lighthouse always results in NO_SCREENSHOTS
我正在嘗試使用chrome-aws-lambda
在public.ecr.aws/lambda/nodejs
Docker 圖像中通過 Puppeteer 運行 Lighthouse,無論我向 Lighthouse 發送哪個網站,我總是會收到如下錯誤:
Chrome 在頁面加載期間沒有收集任何屏幕截圖。 請確保頁面上有內容可見,然后嘗試重新運行 Lighthouse。 (NO_SCREENSHOTS)
請注意,我試過像這樣向它發送加載速度非常快的頁面,因此與此錯誤相關的其他在線提示聲稱網站速度太慢對我來說似乎並不正確。
/Dockerfile
FROM public.ecr.aws/lambda/nodejs:latest
RUN cd /var/task/ & npm install puppeteer-core chrome-aws-lambda lighthouse --save-prod
RUN cd /var/task/ & npm install puppeteer --save-dev
/docker-compose.yml
version: "3.5"
services:
lighthouse:
build:
context: .
networks:
- lighthousenetwork
ports:
- "8080:8080"
volumes:
- ./task/:/var/task/:delegated
command: index.handler
networks:
lighthousenetwork:
driver: bridge
/task/index.js
const chromium = require('chrome-aws-lambda');
const lighthouse = require('lighthouse');
exports.handler = async (event, context, callback) => {
let response = null;
let browser = null;
try {
browser = await chromium.puppeteer.launch({
args: [...chromium.args, "--remote-debugging-port=9222"],
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
ignoreHTTPSErrors: true,
});
const options = {
output: "json",
preset: 'mobile',
onlyCategories: ["performance", "seo", "accessibility", "best-practices"],
port: 9222,
}
const url = 'https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event';
const result = await lighthouse(url, options);
console.log(`Audited ${url} in ${result.lhr.timing.total} ms.`);
const report = JSON.parse(result.report);
response = {
statusCode: 200,
body: {
'url': url,
'Performance': report['categories']['performance']['score'],
'Accessibility': report['categories']['accessibility']['score'],
'SEO': report['categories']['seo']['score'],
'BestPractices': report['categories']['best-practices']['score'],
'ErrorMessage': report['audits']['speed-index']['errorMessage']
}
}
} catch (error) {
return callback(error);
} finally {
if (browser !== null) {
await browser.close();
}
}
return callback(null, response);
};
啟動 Docker 容器:
$ docker-compose up
觸發 function 處理程序:
$ curl -XPOST "http://localhost:8080/2015-03-31/functions/function/invocations" -d '{"payload":"hello world!"}'
Output:
{"statusCode":200,"body":{"url":"https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event","Performance":null,"Accessibility":0.91,"SEO":0.89,"BestPractices":1,"ErrorMessage":"Chrome didn't collect any screenshots during the page load. Please make sure there is content visible on the page, and then try re-running Lighthouse. (NO_SCREENSHOTS)"}}
我遇到了完全相同的問題。 做了很多事情,比如將不同的標志傳遞給 chrome、lighthouse。 沒有工作。
然后我發現了這個https://github.com/GoogleChrome/lighthouse/pull/10483/files#diff-b745a09f9adfeb967c49c66acad7fdcaR77
似乎燈塔團隊並不真正推薦 lambda env。
我看到其他人遇到同樣的問題: https://github.com/GoogleChrome/lighthouse/issues/12101 https://github.com/GoogleChrome/lighthouse/issues/13021
是的,我建議暫時離開 lambda。 嘗試在集群或其他東西上運行燈塔。
不是真正解決您看到的錯誤的解決方案。 但只想分享我在這方面的發現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.