-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
64 lines (60 loc) · 2.1 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
55
56
57
58
59
60
61
62
63
64
const cacheName = "cache2"; // Change value to force update
const urls = [
"./",
"image/icons/android-chrome-192x192.png", // Favicon, Android Chrome M39+ with 4.0 screen density
"image/icons/android-chrome-512x512.png", // Favicon, Android Chrome M47+ Splash screen with 4.0 screen density
"image/icons/apple-touch-icon.png", // Favicon, Apple default
"browserconfig.xml", // IE11 icon configuration file
"image/icons/favicon.ico", // Favicon, IE and fallback for other browsers
"image/icons/favicon-16x16.png", // Favicon, default
"image/icons/favicon-32x32.png", // Favicon, Safari on Mac OS
"index.html", // Main HTML file
"js/main.js", // Main Javascript file
"manifest.json", // Manifest file
"image/icons/mstile-150x150.png", // Favicon, Windows 8 / IE11
"image/icons/safari-pinned-tab.svg", // Favicon, Safari pinned tab
"css/index.css",
"js/nfc.js",
"worker.js",
];
self.addEventListener("install", (event) => {
// Kick out the old service worker
self.skipWaiting();
event.waitUntil(
caches.open(cacheName).then((cache) => {
return cache.addAll(urls);
})
);
});
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(async (cache) => {
const response = await cache.match(event.request);
return (
response ||
fetch(event.request).then((networkResponse) => {
cache.put(event.request, networkResponse.clone());
return networkResponse;
})
);
})
);
});