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