This repository was archived by the owner on May 2, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed
Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 8181 }
8282 })
8383 }
84+
85+ if (' serviceWorker' in navigator ) {
86+ navigator .serviceWorker .register (' /service-worker.js' );
87+ }
8488 </script >
Original file line number Diff line number Diff line change 1+ /* eslint-disable no-undef */
2+ const cacheName = 'fevr-v2-cache' ;
3+
4+ // import workbox
5+ importScripts (
6+ 'https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js' ,
7+ ) ;
8+ const { routing, strategies } = workbox ;
9+
10+ const listRegExp = / ^ \/ ( i n d e x \. h t m l | e v e n t i \/ i n d e x \. h t m l ) / ;
11+
12+ routing . registerRoute (
13+ ( { url } ) => ! listRegExp . test ( url . pathname ) ,
14+ new strategies . StaleWhileRevalidate ( { cacheName } ) ,
15+ ) ;
16+
17+ routing . registerRoute (
18+ ( { url } ) => listRegExp . test ( url . pathname ) ,
19+ new strategies . NetworkFirst ( { cacheName : cacheName + '-list' } ) ,
20+ ) ;
21+
22+ // removes all caches not named <cacheName>
23+ const invalidateOldCache = async ( ) => {
24+ const keys = await caches . keys ( ) ;
25+ const isOldCache = ( key ) => ! key . startsWith ( cacheName ) ;
26+ const oldKeys = keys . filter ( isOldCache ) ;
27+
28+ return Promise . all ( oldKeys . map ( ( key ) => caches . delete ( key ) ) ) ;
29+ } ;
30+
31+ // runs invalidateOldCache on activation
32+ self . addEventListener ( 'activate' , ( e ) => e . waitUntil ( invalidateOldCache ( ) ) ) ;
You can’t perform that action at this time.
0 commit comments