-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsw.js
More file actions
executable file
·31 lines (27 loc) · 1.07 KB
/
sw.js
File metadata and controls
executable file
·31 lines (27 loc) · 1.07 KB
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
self.addEventListener('install', function(event) {
event.waitUntil(self.skipWaiting());
});
self.addEventListener('activate', (event) => {
event.waitUntil(self.clients.claim());
});
self.addEventListener('fetch', function(event) {
// Chrome will apparently check for chrome-extension:// URLs...who knew?!
if (event.request.url.startsWith('chrome-extension')) return;
event.respondWith(caches.match(event.request).then(function(response) {
let fetchPromise = fetch(event.request).then(function (response) {
// response may be used only once
// we need to save clone to put one copy in cache
// and serve second one
let responseClone = response.clone();
let scope_url = new URL(self.registration.scope);
// we use the scope_url to determine the same pathname that the page has
caches.open(scope_url.pathname).then(function (cache) {
cache.put(event.request, responseClone);
});
return response;
}).catch(function () {
return caches.match('/404.jpg');
});
return response || fetchPromise;
}));
});