Skip to content

Commit

Permalink
fix: lighthouse memory leak fix: now only 1 chrome instance for light…
Browse files Browse the repository at this point in the history
…house
  • Loading branch information
popstas committed Aug 14, 2020
1 parent 9b4496f commit 2ebf375
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/scrap-site.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ module.exports = async (baseUrl, options = {}) => {
SKIP_IMAGES = SKIP_CSS = SKIP_JS = options.skipStatic;
}

// open second chrome for lighthouse
let lighthouseChrome;
if(options.lighthouse) {
const chromeFlags = ['--no-sandbox'];
if (options.headless) chromeFlags.push('--headless');
lighthouseChrome = await chromeLauncher.launch({chromeFlags});
}

const exporter = new CSVExporter({
file: csvPath,
fields: fields,
Expand Down Expand Up @@ -308,7 +316,6 @@ module.exports = async (baseUrl, options = {}) => {
const result = await crawl();

if(options.lighthouse) {
const chrome = await chromeLauncher.launch({chromeFlags: ['--headless', "--no-sandbox"]});
const opts = {
// extends: 'lighthouse:default',
/*onlyAudits: [
Expand All @@ -318,7 +325,7 @@ module.exports = async (baseUrl, options = {}) => {
'interactive',
],*/
onlyCategories : [ 'performance', 'pwa', 'accessibility', 'best-practices', 'seo' ],
port: chrome.port
port: lighthouseChrome.port
};
const res = await lighthouse(crawler._options.url, opts);
const data = JSON.parse(res.report);
Expand Down Expand Up @@ -402,6 +409,9 @@ module.exports = async (baseUrl, options = {}) => {
const t = Math.round((Date.now() - start) / 1000);
const perPage = Math.round((t / requestedCount) * 100) / 100;

// close lighthouse's chrome
await chromeLauncher.killAll();;

const finishScan = () => {
if(options.removeCsv) {
fs.unlinkSync(csvPath);
Expand Down

0 comments on commit 2ebf375

Please sign in to comment.