11/* eslint-disable @typescript-eslint/no-use-before-define */
2- // This is the service worker with the combined offline experience (Offline page + Offline copy of pages)
2+ //This is the service worker with the Advanced caching
33
4- const CACHE = "pwabuilder-offline-page" ;
4+ const CACHE = "pwabuilder-adv-cache" ;
5+ const precacheFiles = [
6+ /* Add an array of files to precache for your app */
7+ ] ;
58
69// TODO: replace the following with the correct offline fallback page i.e.: const offlineFallbackPage = "offline.html";
7- const offlineFallbackPage = "/" ;
10+ const offlineFallbackPage = "ToDo-replace-this-name.html" ;
11+
12+ const networkFirstPaths = [
13+ /* Add an array of regex of paths that should go network first */
14+ // Example: /\/api\/.*/
15+ ] ;
16+
17+ const avoidCachingPaths = [
18+ /* Add an array of regex of paths that shouldn't be cached */
19+ // Example: /\/api\/.*/
20+ / \/ I m a g e s \/ .* / ,
21+ / \/ f r e e \/ .* / ,
22+ / \/ b o o t s t r a p \/ .* / ,
23+ / \/ u s e r \/ .* / ,
24+ / \/ a j a x \/ .* / ,
25+ / \/ r e p o s \/ .* / ,
26+ / \/ r e c a p t c h a \/ .* / ,
27+ / \/ a u t h \/ .* / ,
28+ / \/ s o c k e t .i o \/ .* / ,
29+ / \/ a v a t a r \/ .* / ,
30+
31+ "https://source.unsplash.com" ,
32+ "https://code.jquery.com/jquery-3.2.1.slim.min.js" ,
33+ "https://buttons.github.io/buttons.js" ,
34+ ] ;
35+
36+ function pathComparer ( requestUrl , pathRegEx ) {
37+ return requestUrl . match ( new RegExp ( pathRegEx ) ) ;
38+ }
39+
40+ function comparePaths ( requestUrl , pathsArray ) {
41+ if ( requestUrl ) {
42+ for ( let index = 0 ; index < pathsArray . length ; index ++ ) {
43+ const pathRegEx = pathsArray [ index ] ;
44+ if ( pathComparer ( requestUrl , pathRegEx ) ) {
45+ return true ;
46+ }
47+ }
48+ }
49+
50+ return false ;
51+ }
852
9- // Install stage sets up the offline page in the cache and opens a new cache
1053self . addEventListener ( "install" , function ( event ) {
1154 console . log ( "[PWA Builder] Install Event processing" ) ;
1255
56+ console . log ( "[PWA Builder] Skip waiting on install" ) ;
57+ self . skipWaiting ( ) ;
58+
1359 event . waitUntil (
1460 caches . open ( CACHE ) . then ( function ( cache ) {
15- console . log ( "[PWA Builder] Cached offline page during install" ) ;
16-
17- if ( offlineFallbackPage === "ToDo-replace-this-name.html" ) {
18- return cache . add (
19- new Response ( "TODO: Update the value of the offlineFallbackPage constant in the serviceworker." ) ,
20- ) ;
21- }
61+ console . log ( "[PWA Builder] Caching pages during install" ) ;
62+
63+ return cache . addAll ( precacheFiles ) . then ( function ( ) {
64+ if ( offlineFallbackPage === "ToDo-replace-this-name.html" ) {
65+ return cache . add (
66+ new Response (
67+ "TODO: Update the value of the offlineFallbackPage constant in the serviceworker." ,
68+ ) ,
69+ ) ;
70+ }
2271
23- return cache . add ( offlineFallbackPage ) ;
72+ return cache . add ( offlineFallbackPage ) ;
73+ } ) ;
2474 } ) ,
2575 ) ;
2676} ) ;
2777
78+ // Allow sw to control of current page
79+ self . addEventListener ( "activate" , function ( event ) {
80+ console . log ( "[PWA Builder] Claiming clients for current page" ) ;
81+ event . waitUntil ( self . clients . claim ( ) ) ;
82+ } ) ;
83+
2884// If any fetch fails, it will look for the request in the cache and serve it from there first
2985self . addEventListener ( "fetch" , function ( event ) {
3086 if ( event . request . method !== "GET" ) return ;
3187
88+ if ( comparePaths ( event . request . url , networkFirstPaths ) ) {
89+ networkFirstFetch ( event ) ;
90+ } else {
91+ cacheFirstFetch ( event ) ;
92+ }
93+ } ) ;
94+
95+ function cacheFirstFetch ( event ) {
96+ event . respondWith (
97+ fromCache ( event . request ) . then (
98+ function ( response ) {
99+ // The response was found in the cache so we responde with it and update the entry
100+
101+ // This is where we call the server to get the newest version of the
102+ // file to use the next time we show view
103+ event . waitUntil (
104+ fetch ( event . request ) . then ( function ( response ) {
105+ return updateCache ( event . request , response ) ;
106+ } ) ,
107+ ) ;
108+
109+ return response ;
110+ } ,
111+ function ( ) {
112+ // The response was not found in the cache so we look for it on the server
113+ return fetch ( event . request )
114+ . then ( function ( response ) {
115+ // If request was success, add or update it in the cache
116+ event . waitUntil ( updateCache ( event . request , response . clone ( ) ) ) ;
117+
118+ return response ;
119+ } )
120+ . catch ( function ( error ) {
121+ // The following validates that the request was for a navigation to a new document
122+ if ( event . request . destination !== "document" || event . request . mode !== "navigate" ) {
123+ return ;
124+ }
125+
126+ console . log ( "[PWA Builder] Network request failed and no cache." + error ) ;
127+ // Use the precached offline page as fallback
128+ return caches . open ( CACHE ) . then ( function ( cache ) {
129+ cache . match ( offlineFallbackPage ) ;
130+ } ) ;
131+ } ) ;
132+ } ,
133+ ) ,
134+ ) ;
135+ }
136+
137+ function networkFirstFetch ( event ) {
32138 event . respondWith (
33139 fetch ( event . request )
34140 . then ( function ( response ) {
35- console . log ( "[PWA Builder] add page to offline cache: " + response . url ) ;
36-
37141 // If request was success, add or update it in the cache
38142 event . waitUntil ( updateCache ( event . request , response . clone ( ) ) ) ;
39-
40143 return response ;
41144 } )
42145 . catch ( function ( error ) {
43146 console . log ( "[PWA Builder] Network request Failed. Serving content from cache: " + error ) ;
44147 return fromCache ( event . request ) ;
45148 } ) ,
46149 ) ;
47- } ) ;
150+ }
48151
49152function fromCache ( request ) {
50153 // Check to see if you have it in the cache
51154 // Return response
52- // If not in the cache, then return the offline page
155+ // If not in the cache, then return error page
53156 return caches . open ( CACHE ) . then ( function ( cache ) {
54157 return cache . match ( request ) . then ( function ( matching ) {
55158 if ( ! matching || matching . status === 404 ) {
56- // The following validates that the request was for a navigation to a new document
57- if ( request . destination !== "document" || request . mode !== "navigate" ) {
58- return Promise . reject ( "no-match" ) ;
59- }
60-
61- return cache . match ( offlineFallbackPage ) ;
159+ return Promise . reject ( "no-match" ) ;
62160 }
63161
64162 return matching ;
@@ -67,9 +165,13 @@ function fromCache(request) {
67165}
68166
69167function updateCache ( request , response ) {
70- return caches . open ( CACHE ) . then ( function ( cache ) {
71- return cache . put ( request , response ) ;
72- } ) ;
168+ if ( ! comparePaths ( request . url , avoidCachingPaths ) ) {
169+ return caches . open ( CACHE ) . then ( function ( cache ) {
170+ return cache . put ( request , response ) ;
171+ } ) ;
172+ }
173+
174+ return Promise . resolve ( ) ;
73175}
74176
75177// This is an event that can be fired from your page to tell the SW to update the offline page
0 commit comments