const doCache = process . env . NODE_ENV === 'production' ; const CACHE_NAME = `dictionary-v${ appVersion }` ; self . addEventListener ( 'activate' , ( event ) => { const cacheWhitelist = [ CACHE_NAME ] ; event . waitUntil ( caches . keys ( ) . then ( ( keyList ) => Promise . all ( keyList . map ( ( key ) => { if ( ! cacheWhitelist . includes ( key ) ) { console . log ( 'Deleting cache: ' + key ) ; return caches . delete ( key ) ; } } ) ) ) ) ; } ) ; self . addEventListener ( 'install' , ( event ) => { console . log ( 'install called' ) ; if ( ! doCache ) return ; event . waitUntil ( caches . open ( CACHE_NAME ) . then ( ( cache ) => { console . log ( serviceWorkerOption . assets ) ; cache . addAll ( serviceWorkerOption . assets ) ; } ) . then ( self . skipWaiting ( ) ) ) ; } ) ; self . addEventListener ( 'fetch' , ( event ) => { if ( ! doCache ) return ; event . respondWith ( caches . match ( event . request ) . then ( ( response ) => response || fetch ( event . request ) ) ) ; } ) ;