-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
39 lines (35 loc) · 1.18 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
const cacheName = "nasPrviPWA-v1";
const OFFLINE_URL = "/PWAfrontend2022/offline.html";
self.addEventListener("install", (e) => {
console.log("[Service Worker] Install");
e.waitUntil(
(async () => {
const cache = await caches.open(cacheName);
console.log("[Service Worker] Caching all: app shell and content");
await cache.add(new Request(OFFLINE_URL, { cache: "reload" }));
})()
);
});
self.addEventListener("fetch", (e) => {
e.respondWith(
(async () => {
try {
const r = await caches.match(e.request);
console.log(`[Service Worker] Fetching resource: ${e.request.url}`);
if (r) {
return r;
}
const response = await fetch(e.request);
const cache = await caches.open(cacheName);
console.log(`[Service Worker] Caching new resource: ${e.request.url}`);
cache.put(e.request, response.clone());
return response;
} catch (error) {
console.log("Fetch failed; returning offline page instead.", error);
const cache = await caches.open(cacheName);
const cachedResponse = await cache.match(OFFLINE_URL);
return cachedResponse;
}
})()
);
});