-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
64 lines (56 loc) · 1.12 KB
/
service-worker.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
---
layout: null
---
'use strict';
const CACHE_NAME = 'offline-cache-{{'now' | date: '%s' }}';
const FILES_TO_CACHE = [
'/',
'/turbolinks.js',
'/favicon.png',
'/icon-5124.png',
'/poppins-700.subset.woff2',
{%- for recipe in site.recipes %}
'{{ recipe.url | relative_url }}',
{%- endfor %}
];
self.addEventListener('install', evt => {
evt.waitUntil(
caches.open(CACHE_NAME).then(cache => {
return cache.addAll(FILES_TO_CACHE);
})
);
self.skipWaiting();
});
self.addEventListener('activate', evt => {
evt.waitUntil(
caches.keys().then(keyList => {
return Promise.all(
keyList.map(key => {
if (key !== CACHE_NAME) {
return caches.delete(key);
}
})
);
})
);
self.clients.claim();
});
self.addEventListener('fetch', evt => {
if (evt.request.method !== 'GET') {
// Not a page navigation, bail.
return;
}
evt.respondWith(
caches.match(evt.request).then(r => {
return (
r ||
fetch(evt.request).then(response => {
return caches.open(CACHE_NAME).then(cache => {
cache.put(evt.request, response.clone());
return response;
});
})
);
})
);
});