-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
54 lines (47 loc) · 1.52 KB
/
sw.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
const cacheName = "25foods"; // Change value to force update
self.addEventListener("install", event => {
// Kick out the old service worker
self.skipWaiting();
event.waitUntil(
caches.open(cacheName).then(cache => {
return cache.addAll([
"/",
"favicon.ico", // Favicon, IE and fallback for other browsers
"https://www.25foods.com",
"https://cdn.jsdelivr.net/gh/go69/icon@svg/25-foods-96.png", // Logo
"https://cdn.jsdelivr.net/gh/go69/icon@svg/25-foods-512.png", // Logo
"https://cdn.jsdelivr.net/gh/go69/icon@svg/manifest.webmanifest" // Manifest file
]);
})
);
});
self.addEventListener("activate", event => {
// Delete any non-current cache
event.waitUntil(
caches.keys().then(keys => {
Promise.all(
keys.map(key => {
if (![cacheName].includes(key)) {
return caches.delete(key);
}
})
)
})
);
});
// Offline-first, cache-first strategy
// Kick off two asynchronous requests, one to the cache and one to the network
// If there's a cached version available, use it, but fetch an update for next time.
// Gets data on screen as quickly as possible, then updates once the network has returned the latest data.
self.addEventListener("fetch", event => {
event.respondWith(
caches.open(cacheName).then(cache => {
return cache.match(event.request).then(response => {
return response || fetch(event.request).then(networkResponse => {
cache.put(event.request, networkResponse.clone());
return networkResponse;
});
})
})
);
});