-
Notifications
You must be signed in to change notification settings - Fork 78
/
sw.js
30 lines (29 loc) · 867 Bytes
/
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
const cacheName = "files";
addEventListener("fetch", (fetchEvent) => {
const request = fetchEvent.request;
if (request.method !== "GET") {
return;
}
fetchEvent.respondWith(
(async function () {
const responseFromFetch = fetch(request);
fetchEvent.waitUntil(
(async function () {
const responseCopy = (await responseFromFetch).clone();
const myCache = await caches.open(cacheName);
await myCache.put(request, responseCopy);
})()
);
if (request.headers.get("Accept").includes("text/html")) {
try {
return await responseFromFetch;
} catch (error) {
return caches.match(request);
}
} else {
const responseFromCache = await caches.match(request);
return responseFromCache || responseFromFetch;
}
})()
);
});