1
1
//This is the service worker with the Cache-first network
2
2
3
+ var CACHE = 'pwabuilder-precache' ;
4
+ var precacheFiles = [
5
+ /* Add an array of files to precache for your app */
6
+ ] ;
7
+
3
8
//Install stage sets up the cache-array to configure pre-cache content
4
- self . addEventListener ( 'install' , function ( event ) {
5
- event . waitUntil ( preLoad ( ) ) ;
9
+ self . addEventListener ( 'install' , function ( evt ) {
10
+ console . log ( 'The service worker is being installed.' ) ;
11
+ evt . waitUntil ( precache ( ) . then ( function ( ) {
12
+ console . log ( '[ServiceWorker] Skip waiting on install' ) ;
13
+ return self . skipWaiting ( ) ;
14
+
15
+ } )
16
+ ) ;
17
+ } ) ;
18
+
19
+
20
+ //allow sw to control of current page
21
+ self . addEventListener ( 'activate' , function ( event ) {
22
+ console . log ( '[ServiceWorker] Claiming clients for current page' ) ;
23
+ return self . clients . claim ( ) ;
24
+
25
+ } ) ;
26
+
27
+ self . addEventListener ( 'fetch' , function ( evt ) {
28
+ console . log ( 'The service worker is serving the asset.' + evt . request . url ) ;
29
+ evt . respondWith ( fromCache ( evt . request ) . catch ( fromServer ( evt . request ) ) ) ;
30
+ evt . waitUntil ( update ( evt . request ) ) ;
6
31
} ) ;
7
32
8
- var preLoad = function ( ) {
9
- console . log ( '[Manifoldjs] Install Event processing' ) ;
10
- return caches . open ( 'manifoldjs-offline' ) . then ( function ( cache ) {
11
- console . log ( '[Manifoldjs] Cached index and offline page during Install' ) ;
12
- return cache . addAll ( [ '/offline.html' , '/index.html' ] ) ;
33
+
34
+ function precache ( ) {
35
+ return caches . open ( CACHE ) . then ( function ( cache ) {
36
+ return cache . addAll ( precacheFiles ) ;
13
37
} ) ;
14
38
}
15
39
16
- self . addEventListener ( 'fetch' , function ( event ) {
17
- console . log ( 'The service worker is serving the asset.' ) ;
18
- event . respondWith ( checkResponse ( event . request ) . catch ( function ( ) {
19
- return returnFromCache ( event . request ) }
20
- ) ) ;
21
- event . waitUntil ( addToCache ( event . request ) ) ;
22
- } ) ;
23
40
24
- var checkResponse = function ( request ) {
25
- return new Promise ( function ( fulfill , reject ) {
26
- fetch ( request ) . then ( function ( response ) {
27
- if ( response . status !== 404 ) {
28
- fulfill ( response )
29
- } else {
30
- reject ( )
31
- }
32
- } , reject )
41
+ function fromCache ( request ) {
42
+ //we pull files from the cache first thing so we can show them fast
43
+ return caches . open ( CACHE ) . then ( function ( cache ) {
44
+ return cache . match ( request ) . then ( function ( matching ) {
45
+ return matching || Promise . reject ( 'no-match' ) ;
46
+ } ) ;
33
47
} ) ;
34
- } ;
48
+ }
35
49
36
- var addToCache = function ( request ) {
37
- return caches . open ( 'manifoldjs-offline' ) . then ( function ( cache ) {
50
+
51
+ function update ( request ) {
52
+ //this is where we call the server to get the newest version of the
53
+ //file to use the next time we show view
54
+ return caches . open ( CACHE ) . then ( function ( cache ) {
38
55
return fetch ( request ) . then ( function ( response ) {
39
- console . log ( '[manifoldjs] add page to offline' + response . url )
40
56
return cache . put ( request , response ) ;
41
57
} ) ;
42
58
} ) ;
43
- } ;
59
+ }
44
60
45
- var returnFromCache = function ( request ) {
46
- return caches . open ( 'manifoldjs-offline' ) . then ( function ( cache ) {
47
- return cache . match ( request ) . then ( function ( matching ) {
48
- if ( ! matching || matching . status == 404 ) {
49
- return cache . match ( 'offline.html' )
50
- } else {
51
- return matching
52
- }
53
- } ) ;
54
- } ) ;
55
- } ;
61
+ function fromServer ( request ) {
62
+ //this is the fallback if it is not in the cahche to go to the server and get it
63
+ return fetch ( request ) . then ( function ( response ) { return response } )
64
+ }
0 commit comments