-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlighthouse-audit.js
More file actions
123 lines (104 loc) · 4.03 KB
/
lighthouse-audit.js
File metadata and controls
123 lines (104 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const { lighthouse, prepareLabData } = require('lighthouse/core/index.cjs');
const chromeLauncher = require('chrome-launcher');
const fs = require('fs');
const path = require('path');
/**
* Lighthouse Audit Script
*
* This script runs a Lighthouse audit on the specified URLs and saves the results
* to JSON and HTML files for analysis.
*/
// URLs to audit
const urls = [
'http://localhost:3001', // Home page
'http://localhost:3001/about', // About page
'http://localhost:3001/products', // Products page
'http://localhost:3001/chat' // Chat page
];
// Lighthouse configuration
const lighthouseConfig = {
extends: 'lighthouse:default',
settings: {
formFactor: 'mobile',
throttling: {
rttMs: 150,
throughputKbps: 1638.4,
cpuSlowdownMultiplier: 4,
},
screenEmulation: {
mobile: true,
width: 375,
height: 667,
deviceScaleFactor: 2,
disabled: false,
},
emulatedUserAgent: 'Mozilla/5.0 (Linux; Android 11; Pixel 5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.91 Mobile Safari/537.36',
},
};
// Create results directory if it doesn't exist
const resultsDir = path.join(__dirname, 'lighthouse-results');
if (!fs.existsSync(resultsDir)) {
fs.mkdirSync(resultsDir);
}
// Function to run Lighthouse audit
async function runLighthouse(url) {
// Launch Chrome
const chrome = await chromeLauncher.launch({
chromeFlags: ['--headless', '--disable-gpu', '--no-sandbox']
});
// Run Lighthouse
const options = {
logLevel: 'info',
output: 'html',
port: chrome.port,
onlyCategories: ['performance', 'accessibility', 'best-practices', 'seo'],
};
try {
const runnerResult = await lighthouse(url, options, lighthouseConfig);
// Extract page name from URL for filename
const pageName = url.replace('http://localhost:3001', '').replace(/\//g, '-') || 'home';
// Save results
const jsonPath = path.join(resultsDir, `lighthouse-${pageName}.json`);
const htmlPath = path.join(resultsDir, `lighthouse-${pageName}.html`);
fs.writeFileSync(jsonPath, JSON.stringify(runnerResult.lhr, null, 2));
fs.writeFileSync(htmlPath, runnerResult.report);
// Log scores
console.log(`\n---- Lighthouse Scores for ${url} ----`);
console.log(`Performance: ${Math.round(runnerResult.lhr.categories.performance.score * 100)}`);
console.log(`Accessibility: ${Math.round(runnerResult.lhr.categories.accessibility.score * 100)}`);
console.log(`Best Practices: ${Math.round(runnerResult.lhr.categories['best-practices'].score * 100)}`);
console.log(`SEO: ${Math.round(runnerResult.lhr.categories.seo.score * 100)}`);
// Extract key performance metrics
const metrics = runnerResult.lhr.audits;
console.log('\nKey Performance Metrics:');
console.log(`First Contentful Paint: ${metrics['first-contentful-paint'].displayValue}`);
console.log(`Largest Contentful Paint: ${metrics['largest-contentful-paint'].displayValue}`);
console.log(`Total Blocking Time: ${metrics['total-blocking-time'].displayValue}`);
console.log(`Cumulative Layout Shift: ${metrics['cumulative-layout-shift'].displayValue}`);
// Extract SEO issues if any
const seoAudits = Object.values(runnerResult.lhr.audits)
.filter(audit => audit.group === 'seo' && audit.score !== 1);
if (seoAudits.length > 0) {
console.log('\nSEO Issues:');
seoAudits.forEach(audit => {
console.log(`- ${audit.title}: ${audit.displayValue || 'Failed'}`);
});
}
return runnerResult;
} catch (error) {
console.error(`Error running Lighthouse for ${url}:`, error);
} finally {
// Close Chrome
await chrome.kill();
}
}
// Run Lighthouse for all URLs
async function runAllAudits() {
console.log('Starting Lighthouse audits...');
for (const url of urls) {
await runLighthouse(url);
}
console.log(`\nAll audits complete. Results saved to ${resultsDir}`);
console.log('To view the reports, open the HTML files in your browser.');
}
runAllAudits();