1
+ import { join } from 'path' ;
2
+ import { homedir } from 'os' ;
3
+
1
4
import * as dotenv from 'dotenv' ;
2
5
3
6
import runEvent from './lib/run-event/index.js' ;
4
7
import getUtils from './lib/get-utils/index.js' ;
5
8
6
9
dotenv . config ( ) ;
7
10
11
+ const puppeteerCacheDir = join ( homedir ( ) , '.cache' , 'puppeteer' ) ;
12
+
13
+ const onPreBuild = async ( { utils } = { } ) => {
14
+ console . log ( 'Restoring Lighthouse cache...' ) ;
15
+ // Puppeteer relies on a global cache since v19.x, which otherwise would not be persisted in Netlify builds
16
+ await utils ?. cache . restore ( puppeteerCacheDir ) ;
17
+ console . log ( 'Lighthouse cache restored' ) ;
18
+ } ;
19
+
20
+ const persistCache = async ( { utils } = { } ) => {
21
+ console . log ( 'Persisting Lighthouse cache...' ) ;
22
+ await utils ?. cache . save ( puppeteerCacheDir ) ;
23
+ console . log ( 'Lighthouse cache persisted' ) ;
24
+ } ;
25
+
8
26
export default function lighthousePlugin ( inputs ) {
9
27
// Run onSuccess by default, unless inputs specify we should fail_deploy_on_score_thresholds
10
28
const defaultEvent =
@@ -14,6 +32,8 @@ export default function lighthousePlugin(inputs) {
14
32
15
33
if ( defaultEvent === 'onSuccess' ) {
16
34
return {
35
+ onPreBuild,
36
+ onPostBuild : ( { utils } ) => persistCache ( { utils } ) ,
17
37
onSuccess : async ( { constants, utils, inputs } = { } ) => {
18
38
// Mock the required `utils` functions if running locally
19
39
const { failPlugin, show } = getUtils ( { utils } ) ;
@@ -29,10 +49,12 @@ export default function lighthousePlugin(inputs) {
29
49
} ;
30
50
} else {
31
51
return {
52
+ onPreBuild,
32
53
onPostBuild : async ( { constants, utils, inputs } = { } ) => {
33
54
// Mock the required `utils` functions if running locally
34
55
const { failBuild, show } = getUtils ( { utils } ) ;
35
56
57
+ await persistCache ( { utils } ) ;
36
58
await runEvent ( {
37
59
event : 'onPostBuild' ,
38
60
constants,
0 commit comments