|
1 | | -import puppeteer from 'puppeteer'; |
| 1 | +import { execSync } from 'child_process'; |
| 2 | + |
| 3 | +import chromeLauncher from 'chrome-launcher'; |
2 | 4 | import lighthouse from 'lighthouse'; |
3 | 5 | import log from 'lighthouse-logger'; |
4 | | -import chromeLauncher from 'chrome-launcher'; |
| 6 | +import puppeteer from 'puppeteer'; |
5 | 7 |
|
6 | 8 | 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 | + } |
14 | 50 | }; |
15 | 51 |
|
16 | 52 | export const runLighthouse = async (browserPath, url, settings) => { |
|
0 commit comments