Skip to content

Commit 1116f78

Browse files
authored
feat!: Run plugin on live deploy URL by default (#588)
Release-As: 5.0.0
1 parent 07afa56 commit 1116f78

16 files changed

+104
-94
lines changed

README.md

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@ Then add the plugin to your `netlify.toml` configuration file:
3434
[[plugins]]
3535
package = "@netlify/plugin-lighthouse"
3636

37-
# optional, fails build when a category is below a threshold
38-
[plugins.inputs.thresholds]
39-
performance = 0.9
40-
accessibility = 0.9
41-
best-practices = 0.9
42-
seo = 0.9
43-
pwa = 0.9
44-
4537
# optional, deploy the lighthouse report to a path under your site
4638
[plugins.inputs.audits]
4739
output_path = "reports/lighthouse.html"
@@ -51,7 +43,7 @@ The lighthouse scores are automatically printed to the **Deploy log** in the Net
5143

5244
```
5345
2:35:07 PM: ────────────────────────────────────────────────────────────────
54-
2:35:07 PM: 2. onPostBuild command from @netlify/plugin-lighthouse
46+
2:35:07 PM: @netlify/plugin-lighthouse (onSuccess event)
5547
2:35:07 PM: ────────────────────────────────────────────────────────────────
5648
2:35:07 PM:
5749
2:35:07 PM: Serving and scanning site from directory dist
@@ -73,32 +65,25 @@ The lighthouse scores are automatically printed to the **Deploy log** in the Net
7365

7466
To customize how Lighthouse runs audits, you can make changes to the `netlify.toml` file.
7567

76-
By default, the plugin will serve and audit the build directory of the site, inspecting the `index.html`.
77-
You can customize the behavior via the `audits` input:
68+
By default, the plugin will run after your build is deployed on the live deploy permalink, inspecting the home path `/`.
69+
You can add additional configuration and/or inspect a different path, or multiple additional paths by adding configuration in the `netlify.toml` file:
7870

7971
```toml
8072
[[plugins]]
8173
package = "@netlify/plugin-lighthouse"
8274

75+
# Set minimum thresholds for each report area
8376
[plugins.inputs.thresholds]
8477
performance = 0.9
8578

86-
# to audit a sub path of the build directory
79+
# to audit a path other than /
8780
# route1 audit will use the top level thresholds
8881
[[plugins.inputs.audits]]
8982
path = "route1"
9083

91-
# you can specify output_path per audit, relative to the path
84+
# you can optionally specify an output_path per audit, relative to the path, where HTML report output will be saved
9285
output_path = "reports/route1.html"
9386

94-
# to audit an HTML file other than index.html in the build directory
95-
[[plugins.inputs.audits]]
96-
path = "contact.html"
97-
98-
# to audit an HTML file other than index.html in a sub path of the build directory
99-
[[plugins.inputs.audits]]
100-
path = "pages/contact.html"
101-
10287
# to audit a specific absolute url
10388
[[plugins.inputs.audits]]
10489
url = "https://www.example.com"
@@ -107,11 +92,42 @@ You can customize the behavior via the `audits` input:
10792
[plugins.inputs.audits.thresholds]
10893
performance = 0.8
10994

95+
```
96+
97+
#### Fail a deploy based on score thresholds
98+
99+
By default, the lighthouse plugin will run _after_ your deploy has been successful, auditing the live deploy content.
100+
101+
To run the plugin _before_ the deploy is live, use the `fail_deploy_on_score_thresholds` input to instead run during the `onPostBuild` event.
102+
This will statically serve your build output folder, and audit the `index.html` (or other file if specified as below). Please note that sites or site paths using SSR/ISR (server-side rendering or Incremental Static Regeneration) cannot be served and audited in this way.
103+
104+
Using this configuration, if minimum threshold scores are supplied and not met, the deploy will fail. Set the threshold based on `performance`, `accessibility`, `best-practices`, `seo`, or `pwa`.
105+
106+
```toml
107+
[[plugins]]
108+
package = "@netlify/plugin-lighthouse"
109+
110+
# Set the plugin to run prior to deploy, failing the build if minimum thresholds aren't set
111+
[plugins.inputs]
112+
fail_deploy_on_score_thresholds = "true"
113+
114+
# Set minimum thresholds for each report area
115+
[plugins.inputs.thresholds]
116+
performance = 0.9
117+
accessibility: = 0.7
118+
119+
# to audit an HTML file other than index.html in the build directory
120+
[[plugins.inputs.audits]]
121+
path = "contact.html"
122+
123+
# to audit an HTML file other than index.html in a sub path of the build directory
124+
[[plugins.inputs.audits]]
125+
path = "pages/contact.html"
126+
110127
# to serve only a sub directory of the build directory for an audit
111128
# pages/index.html will be audited, and files outside of this directory will not be served
112129
[[plugins.inputs.audits]]
113130
serveDir = "pages"
114-
115131
```
116132

117133
### Run Lighthouse audits for desktop
@@ -148,18 +164,6 @@ Updates to `netlify.toml` will take effect for new builds.
148164
locale = "es" # generates Lighthouse reports in Español
149165
```
150166

151-
### Fail Builds Based on Score Thresholds
152-
153-
By default, the Lighthouse plugin will report the findings in the deploy logs. To fail a build based on a specific score, specify the inputs thresholds in your `netlify.toml` file. Set the threshold based on `performance`, `accessibility`, `best-practices`, `seo`, or `pwa`.
154-
155-
```toml
156-
[[plugins]]
157-
package = "@netlify/plugin-lighthouse"
158-
159-
[plugins.inputs.thresholds]
160-
performance = 0.9
161-
```
162-
163167
### Run Lighthouse Locally
164168

165169
Fork and clone this repo.
@@ -173,21 +177,13 @@ yarn local
173177

174178
## Preview Lighthouse results within the Netlify UI
175179

176-
Netlify offers an experimental feature through Netlify Labs that allows you to view Lighthouse scores for each of your builds on your site's Deploy Details page with a much richer format.
177-
178-
You'll need to install the [Lighthouse build plugin](https://app.netlify.com/plugins/@netlify/plugin-lighthouse/install) on your site and then enable this experimental feature through Netlify Labs.
179-
180-
<img width="1400" alt="Deploy view with Lighthouse visualizations" src="https://user-images.githubusercontent.com/79875905/160019039-c3e529de-f389-42bc-a3d4-458c90d59e6a.png">
181-
182-
If you have multiple audits (directories, paths, etc) defined in your build, we will display a roll-up of the average Lighthouse scores for all the current build's audits plus the results for each individual audit.
180+
The Netlify UI allows you to view Lighthouse scores for each of your builds on your site's Deploy Details page with a much richer format.
183181

184-
<img width="1400" alt="Deploy details with multiple audit Lighthouse results" src="https://user-images.githubusercontent.com/79875905/160019057-d29dffab-49f3-4fbf-a1ac-1f314e0cd837.png">
182+
You'll need to first install the [Lighthouse build plugin](https://app.netlify.com/plugins/@netlify/plugin-lighthouse/install) on your site.
185183

186-
Some items of note:
184+
<img width="1400" alt="Deploy view with Lighthouse visualizations" src="https://github.com/netlify/netlify-plugin-lighthouse/assets/20773163/144d7bd3-5b7b-4a18-826e-c8d582f92fab">
187185

188-
- The [Lighthouse Build Plugin](https://app.netlify.com/plugins/@netlify/plugin-lighthouse/install) must be installed on your site(s) in order for these score visualizations to be displayed.
189-
- This Labs feature is currently only enabled at the user-level, so it will need to be enabled for each individual team member that wishes to see the Lighthouse scores displayed.
186+
If you have multiple audits (e.g. multiple paths) defined in your build, we will display a roll-up of the average Lighthouse scores for all the current build's audits plus the results for each individual audit.
190187

191-
Learn more in our official [Labs docs](https://docs.netlify.com/netlify-labs/experimental-features/lighthouse-visualization/).
188+
<img width="1400" alt="Deploy details with multiple audit Lighthouse results" src="https://github.com/netlify/netlify-plugin-lighthouse/assets/20773163/b9887c64-db03-40c0-b7e9-5acba081f87b">
192189

193-
We have a lot planned for this feature and will be adding functionality regularly, but we'd also love to hear your thoughts. Please [share your feedback](https://netlify.qualtrics.com/jfe/form/SV_1NTbTSpvEi0UzWe) about this experimental feature and tell us what you think.

manifest.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ inputs:
2121
required: false
2222
description: Lighthouse-specific settings, used to modify reporting criteria
2323

24-
- name: run_on_success
24+
- name: fail_deploy_on_score_thresholds
2525
required: false
26-
description: (Beta) Run Lighthouse against the deployed site
26+
description: Fail deploy if minimum threshold scores are not met

netlify.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ package = "./src/index.js"
1111
[plugins.inputs]
1212
output_path = "reports/lighthouse.html"
1313

14+
# Note: Required for our Cypress smoke tests
15+
fail_deploy_on_score_thresholds = "true"
16+
1417
[plugins.inputs.thresholds]
1518
performance = 0.9
1619

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"description": "Netlify Plugin to run Lighthouse on each build",
55
"main": "src/index.js",
66
"scripts": {
7-
"local": "node -e 'import(\"./src/index.js\").then(index => index.default()).then(events => events.onPostBuild());'",
8-
"local-onsuccess": "LIGHTHOUSE_RUN_ON_SUCCESS=true node -e 'import(\"./src/index.js\").then(index => index.default()).then(events => events.onSuccess());'",
7+
"local": "node -e 'import(\"./src/index.js\").then(index => index.default()).then(events => events.onSuccess());'",
8+
"local-onpostbuild": "node -e 'import(\"./src/index.js\").then(index => index.default({fail_deploy_on_score_thresholds: \"true\"})).then(events => events.onPostBuild());'",
99
"lint": "eslint 'src/**/*.js'",
1010
"format": "prettier --write 'src/**/*.js'",
1111
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --collect-coverage --maxWorkers=1",

src/e2e/fail-threshold-onpostbuild.test.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,16 @@ describe('lighthousePlugin with failed threshold run (onPostBuild)', () => {
4545
'- PWA: 30',
4646
];
4747

48-
await lighthousePlugin().onPostBuild({ utils: mockUtils });
48+
await lighthousePlugin({
49+
fail_deploy_on_score_thresholds: 'true',
50+
}).onPostBuild({ utils: mockUtils });
4951
expect(formatMockLog(console.log.mock.calls)).toEqual(logs);
5052
});
5153

5254
it('should not output expected success payload', async () => {
53-
await lighthousePlugin().onPostBuild({ utils: mockUtils });
55+
await lighthousePlugin({
56+
fail_deploy_on_score_thresholds: 'true',
57+
}).onPostBuild({ utils: mockUtils });
5458
expect(mockUtils.status.show).not.toHaveBeenCalledWith();
5559
});
5660

@@ -67,7 +71,9 @@ describe('lighthousePlugin with failed threshold run (onPostBuild)', () => {
6771
" 'Manifest doesn't have a maskable icon' received a score of 0",
6872
];
6973

70-
await lighthousePlugin().onPostBuild({ utils: mockUtils });
74+
await lighthousePlugin({
75+
fail_deploy_on_score_thresholds: 'true',
76+
}).onPostBuild({ utils: mockUtils });
7177
const resultError = console.error.mock.calls[0][0];
7278
expect(stripAnsi(resultError).split('\n').filter(Boolean)).toEqual(error);
7379
});
@@ -99,7 +105,9 @@ describe('lighthousePlugin with failed threshold run (onPostBuild)', () => {
99105
],
100106
};
101107

102-
await lighthousePlugin().onPostBuild({ utils: mockUtils });
108+
await lighthousePlugin({
109+
fail_deploy_on_score_thresholds: 'true',
110+
}).onPostBuild({ utils: mockUtils });
103111
const [resultMessage, resultPayload] =
104112
mockUtils.build.failBuild.mock.calls[0];
105113

src/e2e/fail-threshold-onsuccess.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ describe('lighthousePlugin with failed threshold run (onSuccess)', () => {
2222
beforeEach(() => {
2323
resetEnv();
2424
jest.clearAllMocks();
25-
process.env.LIGHTHOUSE_RUN_ON_SUCCESS = 'true';
2625
process.env.DEPLOY_URL = 'https://www.netlify.com';
2726
process.env.THRESHOLDS = JSON.stringify({
2827
performance: 1,

src/e2e/lib/reset-env.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const resetEnv = () => {
22
delete process.env.OUTPUT_PATH;
33
delete process.env.PUBLISH_DIR;
4-
delete process.env.RUN_ON_SUCCESS;
54
delete process.env.SETTINGS;
65
delete process.env.THRESHOLDS;
76
delete process.env.URL;

src/e2e/not-found-onpostbuild.test.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ describe('lighthousePlugin with single not-found run (onPostBuild)', () => {
3232
'Lighthouse was unable to reliably load the page you requested. Make sure you are testing the correct URL and that the server is properly responding to all requests. (Status code: 404)',
3333
];
3434

35-
await lighthousePlugin().onPostBuild({ utils: mockUtils });
35+
await lighthousePlugin({
36+
fail_deploy_on_score_thresholds: 'true',
37+
}).onPostBuild({ utils: mockUtils });
3638
expect(formatMockLog(console.log.mock.calls)).toEqual(logs);
3739
});
3840

@@ -52,14 +54,18 @@ describe('lighthousePlugin with single not-found run (onPostBuild)', () => {
5254
"Error testing 'example/this-page-does-not-exist': Lighthouse was unable to reliably load the page you requested. Make sure you are testing the correct URL and that the server is properly responding to all requests. (Status code: 404)",
5355
};
5456

55-
await lighthousePlugin().onPostBuild({ utils: mockUtils });
57+
await lighthousePlugin({
58+
fail_deploy_on_score_thresholds: 'true',
59+
}).onPostBuild({ utils: mockUtils });
5660
expect(mockUtils.status.show).toHaveBeenCalledWith(payload);
5761
});
5862

5963
it('should not output errors, or call fail events', async () => {
6064
mockConsoleError();
6165

62-
await lighthousePlugin().onPostBuild({ utils: mockUtils });
66+
await lighthousePlugin({
67+
fail_deploy_on_score_thresholds: 'true',
68+
}).onPostBuild({ utils: mockUtils });
6369
expect(console.error).not.toHaveBeenCalled();
6470
expect(mockUtils.build.failBuild).not.toHaveBeenCalled();
6571
expect(mockUtils.build.failPlugin).not.toHaveBeenCalled();

src/e2e/not-found-onsuccess.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ describe('lighthousePlugin with single not-found run (onSuccess)', () => {
1919
beforeEach(() => {
2020
resetEnv();
2121
jest.clearAllMocks();
22-
process.env.LIGHTHOUSE_RUN_ON_SUCCESS = 'true';
2322
process.env.DEPLOY_URL = 'https://www.netlify.com';
2423
process.env.AUDITS = JSON.stringify([{ path: 'this-page-does-not-exist' }]);
2524
});

src/e2e/settings-locale.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,22 @@ describe('lighthousePlugin with custom locale', () => {
3030
jest.clearAllMocks();
3131
process.env.PUBLISH_DIR = 'example';
3232
process.env.SETTINGS = JSON.stringify({ locale: 'es' });
33+
process.env.DEPLOY_URL = 'https://www.netlify.com';
3334
});
3435

3536
it('should output expected log content', async () => {
3637
const logs = [
3738
'Generating Lighthouse report. This may take a minute…',
38-
'Running Lighthouse on example/ using the “es” locale',
39-
'Serving and scanning site from directory example',
40-
'Lighthouse scores for example/',
39+
'Running Lighthouse on / using the “es” locale',
40+
'Lighthouse scores for /',
4141
'- Rendimiento: 100',
4242
'- Accesibilidad: 100',
4343
'- Prácticas recomendadas: 100',
4444
'- SEO: 91',
4545
'- PWA: 30',
4646
];
4747

48-
await lighthousePlugin().onPostBuild({ utils: mockUtils });
48+
await lighthousePlugin().onSuccess({ utils: mockUtils });
4949
expect(formatMockLog(console.log.mock.calls)).toEqual(logs);
5050
});
5151

@@ -58,7 +58,7 @@ describe('lighthousePlugin with custom locale', () => {
5858
installable: false,
5959
locale: 'es',
6060
},
61-
path: 'example/',
61+
path: '/',
6262
report: '<!DOCTYPE html><h1>Lighthouse Report (mock)</h1>',
6363
summary: {
6464
accessibility: 100,
@@ -70,17 +70,17 @@ describe('lighthousePlugin with custom locale', () => {
7070
},
7171
],
7272
summary:
73-
"Summary for path 'example/': Rendimiento: 100, Accesibilidad: 100, Prácticas recomendadas: 100, SEO: 91, PWA: 30",
73+
"Summary for path '/': Rendimiento: 100, Accesibilidad: 100, Prácticas recomendadas: 100, SEO: 91, PWA: 30",
7474
};
7575

76-
await lighthousePlugin().onPostBuild({ utils: mockUtils });
76+
await lighthousePlugin().onSuccess({ utils: mockUtils });
7777
expect(mockUtils.status.show).toHaveBeenCalledWith(payload);
7878
});
7979

8080
it('should not output errors, or call fail events', async () => {
8181
mockConsoleError();
8282

83-
await lighthousePlugin().onPostBuild({ utils: mockUtils });
83+
await lighthousePlugin().onSuccess({ utils: mockUtils });
8484
expect(console.error).not.toHaveBeenCalled();
8585
expect(mockUtils.build.failBuild).not.toHaveBeenCalled();
8686
expect(mockUtils.build.failPlugin).not.toHaveBeenCalled();

0 commit comments

Comments
 (0)