|
| 1 | +var CURRENT_CACHES = ["/"] |
| 2 | + |
| 3 | +const self = this |
| 4 | + |
| 5 | +self.addEventListener("activate", (event) => { |
| 6 | + // Delete all caches that aren't named in CURRENT_CACHES. |
| 7 | + // While there is only one cache in this example, the same logic will handle the case where |
| 8 | + // there are multiple versioned caches. |
| 9 | + var expectedCacheNamesSet = new Set(Object.values(CURRENT_CACHES)) |
| 10 | + event.waitUntil( |
| 11 | + caches.keys().then((cacheNames) => { |
| 12 | + return Promise.all( |
| 13 | + cacheNames.map((cacheName) => { |
| 14 | + if (!expectedCacheNamesSet.has(cacheName)) { |
| 15 | + // If this cache name isn't present in the set of "expected" cache names, then delete it. |
| 16 | + console.log("Deleting out of date cache:", cacheName) |
| 17 | + return caches.delete(cacheName) |
| 18 | + } |
| 19 | + }) |
| 20 | + ) |
| 21 | + }) |
| 22 | + ) |
| 23 | +}) |
| 24 | + |
| 25 | +self.addEventListener("fetch", (event) => { |
| 26 | + console.log("Handling fetch event for", event.request.url) |
| 27 | + |
| 28 | + event.respondWith( |
| 29 | + caches.open(CURRENT_CACHES).then((cache) => { |
| 30 | + return cache |
| 31 | + .match(event.request) |
| 32 | + .then((response) => { |
| 33 | + if (response) { |
| 34 | + // If there is an entry in the cache for event.request, then response will be defined |
| 35 | + // and we can just return it. Note that in this example, only font resources are cached. |
| 36 | + console.log(" Found response in cache:", response) |
| 37 | + |
| 38 | + return response |
| 39 | + } |
| 40 | + |
| 41 | + // Otherwise, if there is no entry in the cache for event.request, response will be |
| 42 | + // undefined, and we need to fetch() the resource. |
| 43 | + console.log( |
| 44 | + " No response for %s found in cache. About to fetch " + |
| 45 | + "from network...", |
| 46 | + event.request.url |
| 47 | + ) |
| 48 | + |
| 49 | + // We call .clone() on the request since we might use it in a call to cache.put() later on. |
| 50 | + // Both fetch() and cache.put() "consume" the request, so we need to make a copy. |
| 51 | + // (see https://developer.mozilla.org/en-US/docs/Web/API/Request/clone) |
| 52 | + return fetch(event.request.clone()).then((response) => { |
| 53 | + console.log( |
| 54 | + " Response for %s from network is: %O", |
| 55 | + event.request.url, |
| 56 | + response |
| 57 | + ) |
| 58 | + |
| 59 | + if ( |
| 60 | + response.status < 400 && |
| 61 | + response.headers.has("content-type") && |
| 62 | + response.headers.get("content-type").match(event.request) |
| 63 | + ) { |
| 64 | + // This avoids caching responses that we know are errors (i.e. HTTP status code of 4xx or 5xx). |
| 65 | + // We also only want to cache responses that correspond to fonts, |
| 66 | + // i.e. have a Content-Type response header that starts with "font/". |
| 67 | + // Note that for opaque filtered responses (https://fetch.spec.whatwg.org/#concept-filtered-response-opaque) |
| 68 | + // we can't access to the response headers, so this check will always fail and the font won't be cached. |
| 69 | + // All of the Google Web Fonts are served off of a domain that supports CORS, so that isn't an issue here. |
| 70 | + // It is something to keep in mind if you're attempting to cache other resources from a cross-origin |
| 71 | + // domain that doesn't support CORS, though! |
| 72 | + // We call .clone() on the response to save a copy of it to the cache. By doing so, we get to keep |
| 73 | + // the original response object which we will return back to the controlled page. |
| 74 | + // (see https://developer.mozilla.org/en-US/docs/Web/API/Request/clone) |
| 75 | + console.log(" Caching the response to", event.request.url) |
| 76 | + cache.put(event.request, response.clone()) |
| 77 | + } else { |
| 78 | + console.log(" Not caching the response to", event.request.url) |
| 79 | + } |
| 80 | + |
| 81 | + // Return the original response object, which will be used to fulfill the resource request. |
| 82 | + return response |
| 83 | + }) |
| 84 | + }) |
| 85 | + .catch((error) => { |
| 86 | + // This catch() will handle exceptions that arise from the match() or fetch() operations. |
| 87 | + // Note that a HTTP error response (e.g. 404) will NOT trigger an exception. |
| 88 | + // It will return a normal response object that has the appropriate error code set. |
| 89 | + console.error(" Error in fetch handler:", error) |
| 90 | + |
| 91 | + throw error |
| 92 | + }) |
| 93 | + }) |
| 94 | + ) |
| 95 | +}) |
0 commit comments