Skip to content

Commit ec6272d

Browse files
committed
try chrome launcher
1 parent 3603d42 commit ec6272d

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed

.puppeteerrc.cjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
// We use the system Chrome instead
3+
skipDownload: true,
4+
};

src/index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { join } from 'path';
1+
import { existsSync } from 'fs';
22
import { homedir } from 'os';
3+
import { join } from 'path';
34

45
import * as dotenv from 'dotenv';
56

@@ -13,14 +14,26 @@ const puppeteerCacheDir = join(homedir(), '.cache', 'puppeteer');
1314
const restorePuppeteerCache = async ({ utils } = {}) => {
1415
console.log('Restoring Lighthouse cache...');
1516
// Puppeteer relies on a global cache since v19.x, which otherwise would not be persisted in Netlify builds
17+
// Note: We use system Chrome now (skipDownload: true), so this cache may be empty
1618
await utils?.cache.restore(puppeteerCacheDir);
1719
console.log('Lighthouse cache restored');
1820
};
1921

2022
const persistPuppeteerCache = async ({ utils } = {}) => {
2123
console.log('Persisting Lighthouse cache...');
22-
await utils?.cache.save(puppeteerCacheDir);
23-
console.log('Lighthouse cache persisted');
24+
// Only cache if the directory exists and is not empty
25+
// Since we use system Chrome (skipDownload: true), this may not exist
26+
if (existsSync(puppeteerCacheDir)) {
27+
try {
28+
await utils?.cache.save(puppeteerCacheDir);
29+
console.log('Lighthouse cache persisted');
30+
} catch (error) {
31+
// Non-critical error - cache persistence failed but build can continue
32+
console.log('Note: Lighthouse cache persistence skipped');
33+
}
34+
} else {
35+
console.log('Note: Lighthouse cache not found (using system Chrome)');
36+
}
2437
};
2538

2639
export default function lighthousePlugin(inputs) {

src/run-lighthouse.js

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,52 @@
1-
import puppeteer from 'puppeteer';
1+
import { execSync } from 'child_process';
2+
3+
import chromeLauncher from 'chrome-launcher';
24
import lighthouse from 'lighthouse';
35
import log from 'lighthouse-logger';
4-
import chromeLauncher from 'chrome-launcher';
6+
import puppeteer from 'puppeteer';
57

68
export const getBrowserPath = async () => {
7-
const browser = await puppeteer.launch({
8-
headless: 'new',
9-
args: ['--no-sandbox', '--disable-gpu', '--disable-dev-shm-usage'],
10-
});
11-
const path = browser.process().spawnfile;
12-
await browser.close();
13-
return path;
9+
// First, try to find Chrome using chromeLauncher
10+
// This works well on CI environments like Netlify
11+
try {
12+
const installations = chromeLauncher.getChromePath();
13+
if (installations) {
14+
return installations;
15+
}
16+
} catch (error) {
17+
// chromeLauncher.getChromePath() failed, continue to next method
18+
}
19+
20+
// Try to find Chrome using common paths
21+
try {
22+
const chromePath = execSync(
23+
'which google-chrome || which chromium-browser || which chromium',
24+
{
25+
encoding: 'utf8',
26+
stdio: ['pipe', 'pipe', 'ignore'],
27+
},
28+
).trim();
29+
if (chromePath) {
30+
return chromePath;
31+
}
32+
} catch (error) {
33+
// which command failed, continue to next method
34+
}
35+
36+
// Fall back to using puppeteer's bundled Chrome (if available)
37+
try {
38+
const browser = await puppeteer.launch({
39+
headless: 'new',
40+
args: ['--no-sandbox', '--disable-gpu', '--disable-dev-shm-usage'],
41+
});
42+
const path = browser.process().spawnfile;
43+
await browser.close();
44+
return path;
45+
} catch (error) {
46+
throw new Error(
47+
'Could not find Chrome. Please ensure Chrome is installed on the system.',
48+
);
49+
}
1450
};
1551

1652
export const runLighthouse = async (browserPath, url, settings) => {

0 commit comments

Comments
 (0)