2424// cache, then increment the CACHE_VERSION value. It will kick off the service worker update
2525// flow and the old cache(s) will be purged as part of the activate event handler when the
2626// updated service worker is activated.
27- var CACHE_VERSION = '2019-01-12T10:50:18.350Z ' ;
27+ var CACHE_VERSION = '2019-01-12T14:29:56.355Z ' ;
2828var CURRENT_CACHES = {
2929 prefetch : 'prefetch-cache-v' + CACHE_VERSION
3030} ;
3131
32- var CACHE_HOSTS = [ "searchcode.com" ,
32+ var INCLUDED = [ "searchcode.com" ,
3333"fanyi.youdao.com" ] ;
3434
35- var EXCLUDED_PATHS = [ ] ;
35+ var CACHE_ONLY = [ "fanyi.youdao.com" ] ;
3636
37- var isExcluded = function ( path ) {
38- return EXCLUDED_PATHS . find ( function ( p ) {
39- return path . indexOf ( p ) !== - 1 ;
37+ var NETWORK_ONLY = [ ] ;
38+
39+ var EXCLUDED = [ "youdao" ] ;
40+
41+ function matchLocation ( url , caches ) {
42+ return caches . find ( function ( l ) {
43+ return url . indexOf ( l ) !== - 1 ;
4044 } ) ;
4145}
4246
43- self . addEventListener ( 'install' , function ( event ) {
47+ self . addEventListener ( 'install' , function ( event ) {
4448 self . skipWaiting ( ) ; // make new service worker activate ASAP
4549 var now = Date . now ( ) ;
4650
@@ -62,6 +66,10 @@ self.addEventListener('install', function(event) {
6266"fonts/fontawesome-webfont.ttf" ,
6367"fonts/fontawesome-webfont.woff" ,
6468"fonts/fontawesome-webfont.woff2" ,
69+ "js/app.05c07287.js" ,
70+ "js/app.js" ,
71+ "js/lib.2ef380a5.js" ,
72+ "js/lib.js" ,
6573"images/404_dribbble.ae94d03c.gif" ,
6674"images/404_dribbble.gif" ,
6775"images/codelf_logo.f4ae25bd.png" ,
@@ -74,11 +82,6 @@ self.addEventListener('install', function(event) {
7482"images/wechatpay.jpg" ,
7583"images/zhifubao.70c19370.png" ,
7684"images/zhifubao.png" ,
77- "js/app.05c07287.js" ,
78- "js/app.js" ,
79- "js/lib.2ef380a5.js" ,
80- "js/lib.js" ,
81- "css/themes/default/assets/images/flags.png" ,
8285"css/themes/default/assets/fonts/brand-icons.eot" ,
8386"css/themes/default/assets/fonts/brand-icons.svg" ,
8487"css/themes/default/assets/fonts/brand-icons.ttf" ,
@@ -94,15 +97,16 @@ self.addEventListener('install', function(event) {
9497"css/themes/default/assets/fonts/outline-icons.svg" ,
9598"css/themes/default/assets/fonts/outline-icons.ttf" ,
9699"css/themes/default/assets/fonts/outline-icons.woff" ,
97- "css/themes/default/assets/fonts/outline-icons.woff2" ] ;
100+ "css/themes/default/assets/fonts/outline-icons.woff2" ,
101+ "css/themes/default/assets/images/flags.png" ] ;
98102
99103 // All of these logging statements should be visible via the "Inspect" interface
100104 // for the relevant SW accessed via chrome://serviceworker-internals
101105 console . log ( 'Handling install event. Resources to prefetch:' , urlsToPrefetch ) ;
102106
103107 event . waitUntil (
104- caches . open ( CURRENT_CACHES . prefetch ) . then ( function ( cache ) {
105- var cachePromises = urlsToPrefetch . map ( function ( urlToPrefetch ) {
108+ caches . open ( CURRENT_CACHES . prefetch ) . then ( function ( cache ) {
109+ var cachePromises = urlsToPrefetch . map ( function ( urlToPrefetch ) {
106110 // This constructs a new URL object using the service worker's script location as the base
107111 // for relative URLs.
108112 var url = new URL ( urlToPrefetch , location . href ) ;
@@ -125,40 +129,40 @@ self.addEventListener('install', function(event) {
125129 // and it is not possible to determine whether an opaque response represents a success or failure
126130 // (https://github.com/whatwg/fetch/issues/14).
127131 var request = new Request ( url , { mode : 'no-cors' } ) ;
128- return fetch ( request ) . then ( function ( response ) {
132+ return fetch ( request ) . then ( function ( response ) {
129133 if ( response . status >= 400 ) {
130134 throw new Error ( 'request for ' + urlToPrefetch +
131135 ' failed with status ' + response . statusText ) ;
132136 }
133137
134138 // Use the original URL without the cache-busting parameter as the key for cache.put().
135139 return cache . put ( urlToPrefetch , response ) ;
136- } ) . catch ( function ( error ) {
140+ } ) . catch ( function ( error ) {
137141 console . error ( 'Not caching ' + urlToPrefetch + ' due to ' + error ) ;
138142 } ) ;
139143 } ) ;
140144
141- return Promise . all ( cachePromises ) . then ( function ( ) {
145+ return Promise . all ( cachePromises ) . then ( function ( ) {
142146 console . log ( 'Pre-fetching complete.' ) ;
143147 } ) ;
144- } ) . catch ( function ( error ) {
148+ } ) . catch ( function ( error ) {
145149 console . error ( 'Pre-fetching failed:' , error ) ;
146150 } )
147151 ) ;
148152} ) ;
149153
150- self . addEventListener ( 'activate' , function ( event ) {
154+ self . addEventListener ( 'activate' , function ( event ) {
151155 // Delete all caches that aren't named in CURRENT_CACHES.
152156 // While there is only one cache in this example, the same logic will handle the case where
153157 // there are multiple versioned caches.
154- var expectedCacheNames = Object . keys ( CURRENT_CACHES ) . map ( function ( key ) {
158+ var expectedCacheNames = Object . keys ( CURRENT_CACHES ) . map ( function ( key ) {
155159 return CURRENT_CACHES [ key ] ;
156160 } ) ;
157161
158162 event . waitUntil (
159- caches . keys ( ) . then ( function ( cacheNames ) {
163+ caches . keys ( ) . then ( function ( cacheNames ) {
160164 return Promise . all (
161- cacheNames . map ( function ( cacheName ) {
165+ cacheNames . map ( function ( cacheName ) {
162166 if ( expectedCacheNames . indexOf ( cacheName ) === - 1 ) {
163167 // If this cache name isn't present in the array of "expected" cache names, then delete it.
164168 console . log ( 'Deleting out of date cache:' , cacheName ) ;
@@ -170,20 +174,22 @@ self.addEventListener('activate', function(event) {
170174 ) ;
171175} ) ;
172176
173- self . addEventListener ( 'fetch' , function ( event ) {
177+ self . addEventListener ( 'fetch' , function ( event ) {
174178 console . log ( 'Handling fetch event for' , event . request . url ) ;
179+ var requestURL = new URL ( event . request . url ) ;
175180 // https://developers.google.com/web/fundamentals/primers/service-workers/high-performance-loading
176181 if ( event . request . mode === 'navigate' ) {
182+ console . log ( 'request mode:' , event . request . mode ) ;
177183 // See /web/fundamentals/getting-started/primers/async-functions
178184 // for an async/await primer.
179- event . respondWith ( async function ( ) {
185+ event . respondWith ( async function ( ) {
180186 // Optional: Normalize the incoming URL by removing query parameters.
181187 // Instead of https://example.com/page?key=value,
182188 // use https://example.com/page when reading and writing to the cache.
183189 // For static HTML documents, it's unlikely your query parameters will
184190 // affect the HTML returned. But if you do use query parameters that
185191 // uniquely determine your HTML, modify this code to retain them.
186- const normalizedUrl = new URL ( event . request . url ) ;
192+ const normalizedUrl = requestURL ;
187193 normalizedUrl . search = '' ;
188194
189195 // Create promises for both the network response,
@@ -193,70 +199,56 @@ self.addEventListener('fetch', function(event) {
193199
194200 // event.waitUntil() ensures that the service worker is kept alive
195201 // long enough to complete the cache update.
196- event . waitUntil ( async function ( ) {
202+ event . waitUntil ( async function ( ) {
197203 const cache = await caches . open ( CURRENT_CACHES . prefetch ) ;
198204 await cache . put ( normalizedUrl , await fetchResponseCloneP ) ;
199205 } ( ) ) ;
200206
201207 // Prefer the cached response, falling back to the fetch response.
202208 return ( await caches . match ( normalizedUrl ) ) || fetchResponseP ;
203209 } ( ) ) ;
204- return ;
205- }
206- var requestURL = new URL ( event . request . url ) ;
207- if ( requestURL . origin == location . origin ) {
208- event . respondWith (
209- // caches.match() will look for a cache entry in all of the caches available to the service worker.
210- // It's an alternative to first opening a specific named cache and then matching on that.
211- caches . match ( event . request ) . then ( function ( response ) {
212- if ( response ) {
213- console . log ( 'Found response in cache:' , response ) ;
214-
215- return response ;
216- }
217-
218- console . log ( 'No response found in cache. About to fetch from network...' ) ;
219-
220- // event.request will always have the proper mode set ('cors, 'no-cors', etc.) so we don't
221- // have to hardcode 'no-cors' like we do when fetch()ing in the install handler.
222- return fetch ( event . request ) . then ( function ( response ) {
223- console . log ( 'Response from network is:' , response ) ;
224-
225- return response ;
226- } ) . catch ( function ( error ) {
227- // This catch() will handle exceptions thrown from the fetch() operation.
228- // Note that a HTTP error response (e.g. 404) will NOT trigger an exception.
229- // It will return a normal response object that has the appropriate error code set.
230- console . error ( 'Fetching failed:' , error ) ;
231-
232- throw error ;
233- } ) ;
234- } )
235- ) ;
236- } else if ( CACHE_HOSTS . indexOf ( requestURL . host ) != - 1 && / g e t / i. test ( event . request . method ) ) {
237- if ( isExcluded ( requestURL . pathname ) ) {
210+ } else if (
211+ matchLocation ( requestURL . href , INCLUDED )
212+ && ! matchLocation ( requestURL . href , EXCLUDED )
213+ && / g e t / i. test ( event . request . method )
214+ ) {
215+ if ( matchLocation ( requestURL . href , NETWORK_ONLY ) ) {
238216 console . log ( 'network-falling-back-to-caches:' , event . request . url ) ;
239217 // https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#network-falling-back-to-cache
240218 event . respondWith (
241- caches . open ( CURRENT_CACHES . prefetch ) . then ( function ( cache ) {
242- return fetch ( event . request ) . then ( function ( networkResponse ) {
219+ caches . open ( CURRENT_CACHES . prefetch ) . then ( function ( cache ) {
220+ return fetch ( event . request ) . then ( function ( networkResponse ) {
243221 // save to cache
244222 cache . put ( event . request , networkResponse . clone ( ) ) ;
245223 return networkResponse ;
246224 } ) . catch ( function ( ) {
247- return cache . match ( event . request ) . then ( function ( response ) {
225+ return cache . match ( event . request ) . then ( function ( response ) {
248226 return response ;
249227 } ) ;
250228 } ) ;
251229 } )
252230 ) ;
231+ } else if ( matchLocation ( requestURL . href , CACHE_ONLY ) ) {
232+ console . log ( 'cache-falling-back-to-network:' , event . request . url ) ;
233+ // https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#cache-falling-back-to-network
234+ event . respondWith (
235+ caches . open ( CURRENT_CACHES . prefetch ) . then ( function ( cache ) {
236+ return cache . match ( event . request ) . then ( function ( response ) {
237+ return response || fetch ( event . request ) . then ( function ( networkResponse ) {
238+ // save to cache
239+ cache . put ( event . request , networkResponse . clone ( ) ) ;
240+ return networkResponse ;
241+ } ) ;
242+ } ) ;
243+ } )
244+ ) ;
253245 } else {
254246 console . log ( 'cache-then-network:' , event . request . url ) ;
255247 // https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#cache-then-network
256248 event . respondWith (
257- caches . open ( CURRENT_CACHES . prefetch ) . then ( function ( cache ) {
258- return cache . match ( event . request ) . then ( function ( response ) {
259- var fetchPromise = fetch ( event . request ) . then ( function ( networkResponse ) {
249+ caches . open ( CURRENT_CACHES . prefetch ) . then ( function ( cache ) {
250+ return cache . match ( event . request ) . then ( function ( response ) {
251+ var fetchPromise = fetch ( event . request ) . then ( function ( networkResponse ) {
260252 // save to cache
261253 cache . put ( event . request , networkResponse . clone ( ) ) ;
262254 return networkResponse ;
@@ -266,6 +258,36 @@ self.addEventListener('fetch', function(event) {
266258 } )
267259 ) ;
268260 }
261+ } else if ( requestURL . origin == location . origin ) {
262+ console . log ( 'request origin:' , requestURL . origin ) ;
263+ event . respondWith (
264+ // caches.match() will look for a cache entry in all of the caches available to the service worker.
265+ // It's an alternative to first opening a specific named cache and then matching on that.
266+ caches . match ( event . request ) . then ( function ( response ) {
267+ if ( response ) {
268+ console . log ( 'Found response in cache:' , response ) ;
269+
270+ return response ;
271+ }
272+
273+ console . log ( 'No response found in cache. About to fetch from network...' ) ;
274+
275+ // event.request will always have the proper mode set ('cors, 'no-cors', etc.) so we don't
276+ // have to hardcode 'no-cors' like we do when fetch()ing in the install handler.
277+ return fetch ( event . request ) . then ( function ( response ) {
278+ console . log ( 'Response from network is:' , response ) ;
279+
280+ return response ;
281+ } ) . catch ( function ( error ) {
282+ // This catch() will handle exceptions thrown from the fetch() operation.
283+ // Note that a HTTP error response (e.g. 404) will NOT trigger an exception.
284+ // It will return a normal response object that has the appropriate error code set.
285+ console . error ( 'Fetching failed:' , error ) ;
286+
287+ throw error ;
288+ } ) ;
289+ } )
290+ ) ;
269291 }
270292} ) ;
271293if ( 'storage' in navigator && 'estimate' in navigator . storage ) {
0 commit comments