Lighthouse
Lighthouse
Lighthouse CLI
Use Lighthouse Web Tool via CLI, you can check the documentation.
Setup
- Install the Lighthouse CLI.
1
npm install -g lighthouse
Run it
Normal run
- Use the terminal or bash script.
1
lighthouse https://example.com/ --output html --output-path ./lighthouse/report.html
Authenticated run
You need to add the extra headers.
1
2lighthouse http://www.example.com --view \
--extra-headers="{\"Authorization\":\"...\"}"How to create a basic header.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#!/usr/bin/env python3.7
import urllib3
import requests
from base64 import b64encode
username = 'admin'
password = 'password'
encoded_credentials = b64encode(bytes(f'{username}:{password}',
encoding='ascii')).decode('ascii')
auth_header = f'Basic {encoded_credentials}'
# the auth_header above can now be used in our API request
# we'll look at that shortly
# let's print the auth_header variable for testing
print(f'Auth header: {auth_header}')
Performance budgets
Assert thresholds for performance metrics.
Timing budgets: time-based performance metrics like First Contentful Paint, Maximum First Input Delay, and Speed Index.
Resource counts: quantity of resources on a page. These thresholds can be defined per resource type or for the page overall.
Resource sizes: transfer size of resources on a page. These thresholds can be defined per resource type or for the page overall.
Define
budget.json
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[
{
"path": "/*",
"options": {
"firstPartyHostnames": ["*.my-site.com", "my-site.cdn.com"]
},
"timings": [
{
"metric": "interactive",
"budget": 5000
},
{
"metric": "first-meaningful-paint",
"budget": 2000
}
],
"resourceSizes": [
{
"resourceType": "total",
"budget": 500
},
{
"resourceType": "script",
"budget": 150
}
],
"resourceCounts": [
{
"resourceType": "third-party",
"budget": 100
}
]
},
{
"options": {
"firstPartyHostnames": ["*.my-site.com", "my-site.cdn.com"]
},
"path": "/checkout",
"resourceSizes": [
{
"resourceType": "script",
"budget": 200
}
]
}
]Usage
1
lighthouse https://example.com --budget-path=budget.json
Run it on headless Chrome
You need a Chromium instance
1
2lighthouse --chrome-flags="--headless" https://angelesbroullon-codenotepad.statichost.eu/
lighthouse --chrome-flags="--headless" https://angelesbroullon-codenotepad.statichost.eu/ --preset=desktopIn case you are on development mode on a corporate environment, you may need to disable the certificates
1
lighthouse --chrome-flags="--headless --ignore-certificate-errors" https://angelesbroullon-codenotepad.statichost.eu/
Lighthouse CI
Setup
Install the Lighthouse CI CLI.
1
npm install -g @lhci/cli
In the root of your repository, create a
lighthouserc.js
configuration file.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
27module.exports = {
ci: {
collect: {
/* Add configuration here */
// If your site is static, Lighthouse can serve it itself
staticDistDir: './public',
// add the URL if you are running your instance
url: ['http://localhost:8080'],
// you may run it several times
numberOfRuns: 5
},
upload: {
/* Add configuration here */
// if your site is not static: start your own server
startServerCommand: 'npm run start',
// delete it in 7 days
target: 'filesystem',
},
assert: {
assertions: {
// off - ignore assertions
'categories:performance': ['warn', {minScore: 1}],
'categories:accessibility': ['error', {minScore: 1}]
}
},
},
};
Run it
- Run Lighthouse CI from terminal.
1
lhci autorun
Lighthouse CI Server
Dashboard for exploring historical Lighthouse reporting.
Local version
Set up
Use it as a node project.
Install the package
1
npm install @lhci/server sqlite3
Configure the server
app.js
1
2
3
4
5
6
7
8
9
10
11const {createServer} = require('@lhci/server');
console.log('Starting server...');
createServer({
port: process.env.PORT,
storage: {
storageMethod: 'sql',
sqlDialect: 'sqlite',
sqlDatabasePath: '/path/to/db.sql',
},
}).then(({port}) => console.log('LHCI listening on port', port));Run it locally via the CLI.
1
2
3npm install -D @lhci/cli @lhci/server sqlite3
npx lhci server --storage.storageMethod=sql \
--storage.sqlDialect=sqlite --storage.sqlDatabasePath=./db.sql
Basic Authorization
- Configure properties.
1
2
3lhci server --basicAuth.username=myusername --basicAuth.password=mypassword
lhci autorun --upload.basicAuth.username=myusername \
--upload.basicAuth.password=mypassword
❗ Note
Be sure to set the same credentials in your upload step to be able to continue sending builds to your server.
Docker
- Docker on local.
1
2
3docker volume create lhci-data
docker container run --publish 9001:9001 --mount='source=lhci-data,target=/data' \
--detach patrickhulce/lhci-server