Skip to content

Commit 1af569d

Browse files
committed
try PUPPETEER_BROWSER_PATH
1 parent 0bfe9df commit 1af569d

File tree

1 file changed

+13
-54
lines changed

1 file changed

+13
-54
lines changed

src/run-lighthouse.js

Lines changed: 13 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -28,54 +28,26 @@ export const runLighthouse = async (url, settings) => {
2828
try {
2929
console.log('Launching Chrome with puppeteer...');
3030

31-
// Set cache directory before any Puppeteer operations
32-
process.env.PUPPETEER_CACHE_DIR = '/tmp/puppeteer';
33-
console.log('Setting Puppeteer cache directory:', process.env.PUPPETEER_CACHE_DIR);
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);
3435

36+
// Create the directory structure if it doesn't exist
3537
try {
36-
console.log('Installing Chrome...');
37-
await puppeteer.browsers().install();
38-
console.log('Chrome installation complete');
38+
await fs.promises.mkdir('/tmp/puppeteer/chrome/linux-136.0.7103.92/chrome-linux64', { recursive: true });
39+
console.log('Browser directory structure created');
3940
} catch (err) {
40-
console.log('Error installing Chrome:', err.message);
41+
console.error('Error creating browser directory:', err.message);
4142
}
4243

43-
// Check for Chrome in Netlify environment first
44-
const chromePaths = [
45-
'/opt/buildhome/.cache/puppeteer/chrome/linux-136.0.7103.92/chrome-linux64/chrome',
46-
'/opt/buildhome/.cache/puppeteer/chrome/linux-119.0.6045.105/chrome-linux64/chrome',
47-
'/usr/bin/google-chrome',
48-
'/usr/bin/chromium-browser'
49-
];
50-
51-
let executablePath;
52-
for (const path of chromePaths) {
53-
try {
54-
await fs.promises.access(path);
55-
executablePath = path;
56-
console.log('Found Chrome at:', path);
57-
break;
58-
} catch (err) {
59-
console.log(`Chrome not found at ${path}`);
60-
}
61-
}
62-
63-
if (!executablePath) {
64-
console.log('No Chrome installation found, using bundled Chromium');
65-
// Let puppeteer use its bundled version
66-
executablePath = undefined;
67-
}
68-
69-
// Use /tmp directory which should be writable in Netlify
70-
const cacheDirectory = '/tmp/puppeteer';
71-
console.log('Using cache directory:', cacheDirectory);
72-
73-
// Ensure cache directory exists
44+
// Verify the browser path exists
7445
try {
75-
await fs.promises.mkdir(cacheDirectory, { recursive: true });
76-
console.log('Cache directory created/verified');
46+
await fs.promises.access(browserPath);
47+
console.log('Browser exists at:', browserPath);
48+
launchOptions.chromePath = browserPath;
7749
} catch (err) {
78-
console.warn('Could not create cache directory:', err.message);
50+
console.log('Browser not found at configured path, will use default');
7951
}
8052

8153
// Launch browser for Lighthouse with specific configuration for Netlify
@@ -94,23 +66,11 @@ export const runLighthouse = async (url, settings) => {
9466
};
9567

9668
console.log('Launching browser with config:', launchConfig);
97-
98-
console.log('Final launch config:', launchConfig);
9969
browser = await puppeteer.launch(launchConfig);
100-
101-
// Get browser information
10270
console.log('Browser launched successfully');
10371

10472
const wsEndpoint = browser.wsEndpoint();
10573
console.log('Browser WebSocket endpoint:', wsEndpoint);
106-
107-
// Use the launch config's executable path for chrome-launcher
108-
if (launchConfig.executablePath) {
109-
console.log(`Using Chrome at: ${launchConfig.executablePath}`);
110-
launchOptions.chromePath = launchConfig.executablePath;
111-
} else {
112-
console.log('Using default Chrome path');
113-
}
11474
} finally {
11575
if (browser) {
11676
try {
@@ -123,7 +83,6 @@ export const runLighthouse = async (url, settings) => {
12383
}
12484
} catch (error) {
12585
console.error('Error launching Chrome with puppeteer:', error);
126-
throw error;
12786
}
12887

12988
chrome = await chromeLauncher.launch(launchOptions);

0 commit comments

Comments
 (0)