-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const cacheName = "offline-cache-v1"; | ||
const cacheUrls = ["index.html","index.js","manifest.json","sw.js","index.css","node_modules\\dexie\\","pages\\"]; | ||
|
||
// Installing the Service Worker | ||
self.addEventListener("install", async (event) => { | ||
try { | ||
const cache = await caches.open(cacheName); | ||
await cache.addAll(cacheUrls); | ||
} catch (error) { | ||
console.error("Service Worker installation failed:", error); | ||
} | ||
}); | ||
|
||
// Fetching resources | ||
self.addEventListener("fetch", (event) => { | ||
event.respondWith( | ||
(async () => { | ||
const cache = await caches.open(cacheName); | ||
|
||
try { | ||
const cachedResponse = await cache.match(event.request); | ||
if (cachedResponse) { | ||
console.log("cachedResponse: ", event.request.url); | ||
return cachedResponse; | ||
} | ||
|
||
const fetchResponse = await fetch(event.request); | ||
if (fetchResponse) { | ||
console.log("fetchResponse: ", event.request.url); | ||
await cache.put(event.request, fetchResponse.clone()); | ||
return fetchResponse; | ||
} | ||
} catch (error) { | ||
console.log("Fetch failed: ", error); | ||
const cachedResponse = await cache.match("index.html"); | ||
return cachedResponse; | ||
} | ||
})() | ||
); | ||
}); |