Skip to content

Commit c0b7a75

Browse files
committed
try getting executable path from puppeteer
1 parent 1af569d commit c0b7a75

File tree

1 file changed

+30
-64
lines changed

1 file changed

+30
-64
lines changed

src/run-lighthouse.js

Lines changed: 30 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import fs from 'fs';
2-
31
import lighthouse from 'lighthouse';
42
import chromeLauncher from 'chrome-launcher';
53
import log from 'lighthouse-logger';
@@ -11,79 +9,47 @@ export const runLighthouse = async (url, settings) => {
119
const logLevel = settings?.logLevel || 'error';
1210
log.setLevel(logLevel);
1311

14-
console.log('Launching Chrome...');
15-
// Launch Chrome with minimal flags
12+
// Let Puppeteer handle Chrome installation with its defaults
13+
let executablePath;
14+
try {
15+
const browser = await puppeteer.launch({
16+
headless: 'new',
17+
args: [
18+
'--no-sandbox',
19+
'--disable-gpu',
20+
'--disable-dev-shm-usage',
21+
'--disable-software-rasterizer',
22+
'--disable-setuid-sandbox',
23+
'--no-zygote'
24+
]
25+
});
26+
27+
// Get the executable path from Puppeteer's browser instance
28+
executablePath = browser.process().spawnArgs[0];
29+
console.log('Using Chrome at:', executablePath);
30+
await browser.close();
31+
} catch (err) {
32+
console.error('Error launching Chrome:', err.message);
33+
throw err; // We need Chrome to continue
34+
}
35+
36+
// Configure chrome-launcher
1637
const launchOptions = {
1738
chromeFlags: [
1839
'--headless=new',
1940
'--no-sandbox',
2041
'--disable-gpu',
2142
'--disable-dev-shm-usage',
43+
'--disable-software-rasterizer',
44+
'--disable-setuid-sandbox',
45+
'--no-zygote'
2246
],
2347
logLevel,
2448
handleSIGINT: true,
49+
chromePath: executablePath
2550
};
2651

27-
// Launch Chrome using puppeteer
28-
try {
29-
console.log('Launching Chrome with puppeteer...');
30-
31-
// Set Puppeteer browser path to a writable location
32-
const browserPath = '/tmp/puppeteer/chrome/linux-136.0.7103.92/chrome-linux64/chrome';
33-
process.env.PUPPETEER_BROWSER_PATH = browserPath;
34-
console.log('Setting Puppeteer browser path:', process.env.PUPPETEER_BROWSER_PATH);
35-
36-
// Create the directory structure if it doesn't exist
37-
try {
38-
await fs.promises.mkdir('/tmp/puppeteer/chrome/linux-136.0.7103.92/chrome-linux64', { recursive: true });
39-
console.log('Browser directory structure created');
40-
} catch (err) {
41-
console.error('Error creating browser directory:', err.message);
42-
}
43-
44-
// Verify the browser path exists
45-
try {
46-
await fs.promises.access(browserPath);
47-
console.log('Browser exists at:', browserPath);
48-
launchOptions.chromePath = browserPath;
49-
} catch (err) {
50-
console.log('Browser not found at configured path, will use default');
51-
}
52-
53-
// Launch browser for Lighthouse with specific configuration for Netlify
54-
let browser;
55-
try {
56-
const launchConfig = {
57-
headless: 'new',
58-
args: [
59-
'--no-sandbox',
60-
'--disable-gpu',
61-
'--disable-dev-shm-usage',
62-
'--disable-software-rasterizer',
63-
'--disable-setuid-sandbox',
64-
'--no-zygote'
65-
]
66-
};
67-
68-
console.log('Launching browser with config:', launchConfig);
69-
browser = await puppeteer.launch(launchConfig);
70-
console.log('Browser launched successfully');
71-
72-
const wsEndpoint = browser.wsEndpoint();
73-
console.log('Browser WebSocket endpoint:', wsEndpoint);
74-
} finally {
75-
if (browser) {
76-
try {
77-
await browser.close();
78-
console.log('Browser closed successfully');
79-
} catch (err) {
80-
console.warn('Error closing browser:', err);
81-
}
82-
}
83-
}
84-
} catch (error) {
85-
console.error('Error launching Chrome with puppeteer:', error);
86-
}
52+
console.log('Chrome launch options:', launchOptions);
8753

8854
chrome = await chromeLauncher.launch(launchOptions);
8955
console.log('Chrome launched on port:', chrome.port);

0 commit comments

Comments
 (0)