|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2016 Google Inc. All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/* eslint-env worker, serviceworker */ |
| 19 | + |
| 20 | +// This service-worker courtesy of googlechrome.github.io/samples/service-worker/basic/index.html |
| 21 | + |
| 22 | +// A list of local resources we always want to be cached. |
| 23 | +const PRECACHE_URLS = [ |
| 24 | + './offline-ready.html', |
| 25 | + './offline-ready-sw.js', |
| 26 | + './smoketest-offline-config.json' |
| 27 | +]; |
| 28 | + |
| 29 | +// Names of the two caches used in this version of the service worker. |
| 30 | +// Change to v2, etc. when you update any of the local resources, which will |
| 31 | +// in turn trigger the install event again. |
| 32 | +const PRECACHE = 'precache-v1'; |
| 33 | +const RUNTIME = 'runtime'; |
| 34 | + |
| 35 | +// The install handler takes care of precaching the resources we always need. |
| 36 | +self.addEventListener('install', event => { |
| 37 | + self.skipWaiting(); |
| 38 | + |
| 39 | + const populateCaches = caches.open(PRECACHE) |
| 40 | + .then(cache => cache.addAll(PRECACHE_URLS)); |
| 41 | + |
| 42 | + event.waitUntil(populateCaches); |
| 43 | +}); |
| 44 | + |
| 45 | +// The activate handler takes care of cleaning up old caches. |
| 46 | +self.addEventListener('activate', event => { |
| 47 | + const currentCaches = [PRECACHE, RUNTIME]; |
| 48 | + event.waitUntil( |
| 49 | + caches.keys().then(cacheNames => { |
| 50 | + return cacheNames.filter(cacheName => !currentCaches.includes(cacheName)); |
| 51 | + }).then(cachesToDelete => { |
| 52 | + return Promise.all(cachesToDelete.map(cacheToDelete => { |
| 53 | + return caches.delete(cacheToDelete); |
| 54 | + })); |
| 55 | + }).then(() => self.clients.claim()) |
| 56 | + ); |
| 57 | +}); |
| 58 | + |
| 59 | +// The fetch handler serves responses for same-origin resources from a cache. |
| 60 | +// If no response is found, it populates the runtime cache with the response |
| 61 | +// from the network before returning it to the page. |
| 62 | +self.addEventListener('fetch', event => { |
| 63 | + // Skip cross-origin requests, like those for Google Analytics. |
| 64 | + if (!event.request.url.startsWith(self.location.origin)) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + event.respondWith( |
| 69 | + caches.match(event.request).then(cachedResponse => { |
| 70 | + if (cachedResponse) { |
| 71 | + return cachedResponse; |
| 72 | + } |
| 73 | + |
| 74 | + return caches.open(RUNTIME).then(cache => { |
| 75 | + return fetch(event.request).then(response => { |
| 76 | + // Put a copy of the response in the runtime cache. |
| 77 | + return cache.put(event.request, response.clone()).then(_ => response); |
| 78 | + }); |
| 79 | + }); |
| 80 | + }) |
| 81 | + ); |
| 82 | +}); |
0 commit comments