blob: baa07fd6d8f1996868847f18f40022d762b69a7e (
plain)
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
|
const files = ["serviceworker_page.html", "firefox-logo-nightly.svg"];
const cacheName = "v1";
self.addEventListener("install", event => {
performance.mark("__serviceworker_event");
console.log("[SW]:", "Install event");
event.waitUntil(cacheAssets());
});
async function cacheAssets() {
const cache = await caches.open(cacheName);
await cache.addAll(files);
}
self.addEventListener("fetch", event => {
performance.mark("__serviceworker_event");
console.log("Handling fetch event for", event.request.url);
event.respondWith(handleFetch(event.request));
});
async function handleFetch(request) {
const cachedResponse = await caches.match(request);
if (cachedResponse) {
console.log("Found response in cache:", cachedResponse);
return cachedResponse;
}
console.log("No response found in cache. About to fetch from network...");
const networkResponse = await fetch(request);
console.log("Response from network is:", networkResponse);
return networkResponse;
}
|