1
- import fs from 'fs' ;
2
-
3
1
import lighthouse from 'lighthouse' ;
4
2
import chromeLauncher from 'chrome-launcher' ;
5
3
import log from 'lighthouse-logger' ;
@@ -11,79 +9,47 @@ export const runLighthouse = async (url, settings) => {
11
9
const logLevel = settings ?. logLevel || 'error' ;
12
10
log . setLevel ( logLevel ) ;
13
11
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
16
37
const launchOptions = {
17
38
chromeFlags : [
18
39
'--headless=new' ,
19
40
'--no-sandbox' ,
20
41
'--disable-gpu' ,
21
42
'--disable-dev-shm-usage' ,
43
+ '--disable-software-rasterizer' ,
44
+ '--disable-setuid-sandbox' ,
45
+ '--no-zygote'
22
46
] ,
23
47
logLevel,
24
48
handleSIGINT : true ,
49
+ chromePath : executablePath
25
50
} ;
26
51
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 ) ;
87
53
88
54
chrome = await chromeLauncher . launch ( launchOptions ) ;
89
55
console . log ( 'Chrome launched on port:' , chrome . port ) ;
0 commit comments