-
Notifications
You must be signed in to change notification settings - Fork 1
/
service-worker.js
38 lines (33 loc) · 877 Bytes
/
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
const cacheName = "cache-v1";
const precacheResources = [
"/",
"/index.html",
"/styles.css",
"/src/index.js",
"/src/tempo-detector.js",
];
self.addEventListener("install", async (event) => {
event.waitUntil(async () => {
try {
const cache = await caches.open(cacheName);
cache.addAll(precacheResources);
} catch (e) {
console.error("Failed to add precache resources", e.message);
throw e;
}
});
});
self.addEventListener("fetch", (event) => {
if (event.request.method !== "GET") return;
event.respondWith(
(async function () {
const cache = await caches.open("dynamic-v1");
const cachedResponse = await cache.match(event.request);
if (cachedResponse) {
event.waitUntil(cache.add(event.request));
return cachedResponse;
}
return fetch(event.request);
})()
);
});