1+ import { join } from 'path' ;
2+ import { homedir } from 'os' ;
3+
14import * as dotenv from 'dotenv' ;
25
36import runEvent from './lib/run-event/index.js' ;
47import getUtils from './lib/get-utils/index.js' ;
58
69dotenv . config ( ) ;
710
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+
826export default function lighthousePlugin ( inputs ) {
927 // Run onSuccess by default, unless inputs specify we should fail_deploy_on_score_thresholds
1028 const defaultEvent =
@@ -14,6 +32,8 @@ export default function lighthousePlugin(inputs) {
1432
1533 if ( defaultEvent === 'onSuccess' ) {
1634 return {
35+ onPreBuild,
36+ onPostBuild : ( { utils } ) => persistCache ( { utils } ) ,
1737 onSuccess : async ( { constants, utils, inputs } = { } ) => {
1838 // Mock the required `utils` functions if running locally
1939 const { failPlugin, show } = getUtils ( { utils } ) ;
@@ -29,10 +49,12 @@ export default function lighthousePlugin(inputs) {
2949 } ;
3050 } else {
3151 return {
52+ onPreBuild,
3253 onPostBuild : async ( { constants, utils, inputs } = { } ) => {
3354 // Mock the required `utils` functions if running locally
3455 const { failBuild, show } = getUtils ( { utils } ) ;
3556
57+ await persistCache ( { utils } ) ;
3658 await runEvent ( {
3759 event : 'onPostBuild' ,
3860 constants,
0 commit comments