1+ // tick this to make the cache invalidate and update 
2+ const  CACHE_VERSION  =  1 ; 
3+ const  CURRENT_CACHES  =  { 
4+  'read-through' : 'read-through-cache-v'  +  CACHE_VERSION 
5+ } ; 
6+ 
7+ self . addEventListener ( 'activate' ,  ( event )  =>  { 
8+  // Delete all caches that aren't named in CURRENT_CACHES. 
9+  // While there is only one cache in this example, the same logic will handle the case where 
10+  // there are multiple versioned caches. 
11+  const  expectedCacheNames  =  Object . keys ( CURRENT_CACHES ) . map ( ( key )  =>  { 
12+  return  CURRENT_CACHES [ key ] ; 
13+  } ) ; 
14+ 
15+  event . waitUntil ( 
16+  caches . keys ( ) . then ( ( cacheNames )  =>  { 
17+  return  Promise . all ( 
18+  cacheNames . map ( ( cacheName )  =>  { 
19+  if  ( expectedCacheNames . indexOf ( cacheName )  ===  - 1 )  { 
20+  // If this cache name isn't present in the array of "expected" cache names, then delete it. 
21+  console . log ( 'Deleting out of date cache:' ,  cacheName ) ; 
22+  return  caches . delete ( cacheName ) ; 
23+  } 
24+  } ) 
25+  ) ; 
26+  } ) 
27+  ) ; 
28+ } ) ; 
29+ 
30+ // This sample illustrates an aggressive approach to caching, in which every valid response is 
31+ // cached and every request is first checked against the cache. 
32+ // This may not be an appropriate approach if your web application makes requests for 
33+ // arbitrary URLs as part of its normal operation (e.g. a RSS client or a news aggregator), 
34+ // as the cache could end up containing large responses that might not end up ever being accessed. 
35+ // Other approaches, like selectively caching based on response headers or only caching 
36+ // responses served from a specific domain, might be more appropriate for those use cases. 
37+ self . addEventListener ( 'fetch' ,  ( event )  =>  { 
38+ 
39+  event . respondWith ( 
40+  caches . open ( CURRENT_CACHES [ 'read-through' ] ) . then ( ( cache )  =>  { 
41+  return  cache . match ( event . request ) . then ( ( response )  =>  { 
42+  if  ( response )  { 
43+  // If there is an entry in the cache for event.request, then response will be defined 
44+  // and we can just return it. 
45+ 
46+  return  response ; 
47+  } 
48+ 
49+  // Otherwise, if there is no entry in the cache for event.request, response will be 
50+  // undefined, and we need to fetch() the resource. 
51+  console . log ( ' No response for %s found in cache. '  + 
52+  'About to fetch from network...' ,  event . request . url ) ; 
53+ 
54+  // We call .clone() on the request since we might use it in the call to cache.put() later on. 
55+  // Both fetch() and cache.put() "consume" the request, so we need to make a copy. 
56+  // (see https://fetch.spec.whatwg.org/#dom-request-clone) 
57+  return  fetch ( event . request . clone ( ) ) . then ( ( response )  =>  { 
58+ 
59+  // Optional: add in extra conditions here, e.g. response.type == 'basic' to only cache 
60+  // responses from the same domain. See https://fetch.spec.whatwg.org/#concept-response-type 
61+  if  ( response . status  <  400  &&  response . type  ===  'basic' )  { 
62+  // We need to call .clone() on the response object to save a copy of it to the cache. 
63+  // (https://fetch.spec.whatwg.org/#dom-request-clone) 
64+  cache . put ( event . request ,  response . clone ( ) ) ; 
65+  } 
66+ 
67+  // Return the original response object, which will be used to fulfill the resource request. 
68+  return  response ; 
69+  } ) ; 
70+  } ) . catch ( ( error )  =>  { 
71+  // This catch() will handle exceptions that arise from the match() or fetch() operations. 
72+  // Note that a HTTP error response (e.g. 404) will NOT trigger an exception. 
73+  // It will return a normal response object that has the appropriate error code set. 
74+  console . error ( ' Read-through caching failed:' ,  error ) ; 
75+ 
76+  throw  error ; 
77+  } ) ; 
78+  } ) 
79+  ) ; 
80+ } ) ; 
0 commit comments