|
| 1 | +import fs from 'fs'; |
| 2 | + |
1 | 3 | import lighthouse from 'lighthouse';
|
2 | 4 | import chromeLauncher from 'chrome-launcher';
|
3 | 5 | import log from 'lighthouse-logger';
|
@@ -25,16 +27,82 @@ export const runLighthouse = async (url, settings) => {
|
25 | 27 | // Launch Chrome using puppeteer
|
26 | 28 | try {
|
27 | 29 | console.log('Launching Chrome with puppeteer...');
|
28 |
| - const browser = await puppeteer.launch({ |
29 |
| - headless: 'new', |
30 |
| - args: ['--no-sandbox', '--disable-gpu', '--disable-dev-shm-usage'] |
31 |
| - }); |
32 |
| - |
33 |
| - const browserPath = browser.executablePath(); |
34 |
| - await browser.close(); |
| 30 | + // Check for Chrome in Netlify environment first |
| 31 | + const chromePaths = [ |
| 32 | + '/opt/buildhome/.cache/puppeteer/chrome/linux-119.0.6045.105/chrome-linux64/chrome', |
| 33 | + '/usr/bin/google-chrome', |
| 34 | + '/usr/bin/chromium-browser' |
| 35 | + ]; |
| 36 | + |
| 37 | + let executablePath; |
| 38 | + for (const path of chromePaths) { |
| 39 | + try { |
| 40 | + await fs.promises.access(path); |
| 41 | + executablePath = path; |
| 42 | + console.log('Found Chrome at:', path); |
| 43 | + break; |
| 44 | + } catch (err) { |
| 45 | + console.log(`Chrome not found at ${path}`); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if (!executablePath) { |
| 50 | + console.log('No Chrome installation found, using bundled Chromium'); |
| 51 | + // Let puppeteer use its bundled version |
| 52 | + executablePath = undefined; |
| 53 | + } |
| 54 | + |
| 55 | + // Use /tmp directory which should be writable in Netlify |
| 56 | + const cacheDirectory = '/tmp/puppeteer'; |
| 57 | + console.log('Using cache directory:', cacheDirectory); |
| 58 | + |
| 59 | + // Ensure cache directory exists |
| 60 | + try { |
| 61 | + await fs.promises.mkdir(cacheDirectory, { recursive: true }); |
| 62 | + console.log('Cache directory created/verified'); |
| 63 | + } catch (err) { |
| 64 | + console.warn('Could not create cache directory:', err.message); |
| 65 | + } |
| 66 | + |
| 67 | + // Launch browser for Lighthouse with specific configuration for Netlify |
| 68 | + let browser; |
| 69 | + try { |
| 70 | + const launchConfig = { |
| 71 | + headless: 'new', |
| 72 | + ...(executablePath ? { executablePath } : {}), |
| 73 | + args: [ |
| 74 | + '--no-sandbox', |
| 75 | + '--disable-gpu', |
| 76 | + '--disable-dev-shm-usage', |
| 77 | + '--disable-software-rasterizer', |
| 78 | + '--disable-setuid-sandbox', |
| 79 | + '--no-zygote' |
| 80 | + ], |
| 81 | + cacheDirectory |
| 82 | + }; |
| 83 | + |
| 84 | + console.log('Launching browser with config:', launchConfig); |
| 85 | + browser = await puppeteer.launch(launchConfig); |
35 | 86 |
|
36 |
| - console.log(`Found Chrome at: ${browserPath}`); |
37 |
| - launchOptions.chromePath = browserPath; |
| 87 | + // Get browser information |
| 88 | + const browserPath = browser.executablePath(); |
| 89 | + const wsEndpoint = browser.wsEndpoint(); |
| 90 | + |
| 91 | + console.log('Browser launched successfully'); |
| 92 | + console.log('Browser WebSocket endpoint:', wsEndpoint); |
| 93 | + console.log(`Found Chrome at: ${browserPath}`); |
| 94 | + |
| 95 | + launchOptions.chromePath = browserPath; |
| 96 | + } finally { |
| 97 | + if (browser) { |
| 98 | + try { |
| 99 | + await browser.close(); |
| 100 | + console.log('Browser closed successfully'); |
| 101 | + } catch (err) { |
| 102 | + console.warn('Error closing browser:', err); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
38 | 106 | } catch (error) {
|
39 | 107 | console.error('Error launching Chrome with puppeteer:', error);
|
40 | 108 | throw error;
|
|
0 commit comments