|
| 1 | +/** |
| 2 | + * Copyright 2015 Google Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +importScripts('third_party/serviceworker-cache-polyfill.js'); |
| 18 | + |
| 19 | +var CACHE_NAME = 'appshell'; |
| 20 | +var CACHE_VERSION = '@VERSION@'; |
| 21 | + |
| 22 | +self.oninstall = function(event) { |
| 23 | + var urls = [ |
| 24 | + '/app-shell', |
| 25 | + '/images/chrome-touch-icon-192x192.png', |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + '/images/ic_menu_24px.svg', |
| 30 | + '/images/ic_add_24px.svg', |
| 31 | + '/images/ic_info_outline_24px.svg', |
| 32 | + |
| 33 | + '/scripts/core.js', |
| 34 | + '/styles/core.css', |
| 35 | + |
| 36 | + '/favicon.ico', |
| 37 | + '/manifest.json', |
| 38 | + |
| 39 | + '/partials/', |
| 40 | + '/partials/url-1', |
| 41 | + '/partials/url-2', |
| 42 | + '/partials/index' |
| 43 | + ]; |
| 44 | + |
| 45 | + urls = urls.map(function(url) { |
| 46 | + return new Request(url, {credentials: 'include'}); |
| 47 | + }); |
| 48 | + |
| 49 | + event.waitUntil( |
| 50 | + caches |
| 51 | + .open(CACHE_NAME + '-v' + CACHE_VERSION) |
| 52 | + .then(function(cache) { |
| 53 | + return cache.addAll(urls); |
| 54 | + }) |
| 55 | + ); |
| 56 | +}; |
| 57 | + |
| 58 | +self.onactivate = function(event) { |
| 59 | + var currentCacheName = CACHE_NAME + '-v' + CACHE_VERSION; |
| 60 | + caches.keys().then(function(cacheNames) { |
| 61 | + return Promise.all( |
| 62 | + cacheNames.map(function(cacheName) { |
| 63 | + // TODO: This should never get called |
| 64 | + // can we drop this check? |
| 65 | + if (cacheName.indexOf(CACHE_NAME) === -1) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + if (cacheName !== currentCacheName) { |
| 70 | + return caches.delete(cacheName); |
| 71 | + } |
| 72 | + }) |
| 73 | + ); |
| 74 | + }); |
| 75 | +}; |
| 76 | + |
| 77 | +self.onfetch = function(event) { |
| 78 | + var request = event.request; |
| 79 | + event.respondWith( |
| 80 | + // Check the cache for a hit of the asset as is. |
| 81 | + caches.match(request).then((response) => { |
| 82 | + // If we have a response return it. |
| 83 | + if (response) { |
| 84 | + console.log(' sw: [cached] ' + request.url); |
| 85 | + return response; |
| 86 | + } |
| 87 | + |
| 88 | + // For other requests on our domain, return the app shell |
| 89 | + var url = new URL(request.url); |
| 90 | + if (url.host === this.location.host) { |
| 91 | + if ( |
| 92 | + url.pathname.indexOf('.') === -1 && |
| 93 | + url.pathname.indexOf('/partials') !== 0 |
| 94 | + ) { |
| 95 | + console.log(' sw: [app-shell redirect] ' + request.url); |
| 96 | + return caches.match('/app-shell'); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // If here, then it should be a request for external url |
| 101 | + // analytics or web fonts for example. |
| 102 | + console.log(' sw: [fetch] ' + request.url); |
| 103 | + return fetch(request); |
| 104 | + }) |
| 105 | + ); |
| 106 | +}; |
0 commit comments