|
| 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 | + |
| 24 | + var urls = [ |
| 25 | + |
| 26 | + '/', |
| 27 | + '/images/chrome-touch-icon-192x192.png', |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + '/images/ic_menu_24px.svg', |
| 32 | + '/images/ic_add_24px.svg', |
| 33 | + '/images/ic_info_outline_24px.svg', |
| 34 | + |
| 35 | + '/scripts/core.js', |
| 36 | + '/scripts/list.js', |
| 37 | + |
| 38 | + '/styles/core.css', |
| 39 | + '/styles/list.css', |
| 40 | + |
| 41 | + '/third_party/Roboto/Roboto-400.woff', |
| 42 | + '/third_party/Roboto/Roboto-500.woff', |
| 43 | + |
| 44 | + '/favicon.ico', |
| 45 | + '/manifest.json' |
| 46 | + |
| 47 | + ]; |
| 48 | + |
| 49 | + urls = urls.map(function(url) { |
| 50 | + return new Request(url, {credentials: 'include'}); |
| 51 | + }); |
| 52 | + |
| 53 | + event.waitUntil( |
| 54 | + caches |
| 55 | + .open(CACHE_NAME + '-v' + CACHE_VERSION) |
| 56 | + .then(function(cache) { |
| 57 | + return cache.addAll(urls); |
| 58 | + }) |
| 59 | + ); |
| 60 | + |
| 61 | +}; |
| 62 | + |
| 63 | +self.onactivate = function(event) { |
| 64 | + |
| 65 | + var currentCacheName = CACHE_NAME + '-v' + CACHE_VERSION; |
| 66 | + caches.keys().then(function(cacheNames) { |
| 67 | + |
| 68 | + return Promise.all( |
| 69 | + cacheNames.map(function(cacheName) { |
| 70 | + if (cacheName.indexOf(CACHE_NAME) == -1) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + if (cacheName != currentCacheName) { |
| 75 | + return caches.delete(cacheName); |
| 76 | + } |
| 77 | + }) |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | +}; |
| 82 | + |
| 83 | +self.onfetch = function(event) { |
| 84 | + |
| 85 | + var request = event.request; |
| 86 | + var url = new URL(request.url); |
| 87 | + var validSubsections = [ |
| 88 | + 'create', 'details', 'edit', '' |
| 89 | + ]; |
| 90 | + |
| 91 | + var subsection = /^\/([^\/]*)/.exec(url.pathname)[1]; |
| 92 | + |
| 93 | + event.respondWith( |
| 94 | + |
| 95 | + // Check the cache for a hit. |
| 96 | + caches.match(request).then(function(response) { |
| 97 | + |
| 98 | + // If we have a response return it. |
| 99 | + if (response) |
| 100 | + return response; |
| 101 | + |
| 102 | + // Otherwise return index.html file. |
| 103 | + if (validSubsections.indexOf(subsection) >= 0) |
| 104 | + return caches.match('/'); |
| 105 | + |
| 106 | + // We may get requests for analytics so |
| 107 | + // do a very dumb check for that. |
| 108 | + if (url.host.indexOf('voice') === -1) |
| 109 | + return fetch(request); |
| 110 | + |
| 111 | + // And worst case we fire out a not found. |
| 112 | + return new Response('Sorry, not found'); |
| 113 | + }) |
| 114 | + ); |
| 115 | +}; |
0 commit comments