-
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.
feat: add service-worker for caching
- Loading branch information
Showing
5 changed files
with
95 additions
and
8 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,65 @@ | ||
const cacheName = "v1.0.0"; | ||
const cacheAssets = [ | ||
"index.html", | ||
"script.js", | ||
"style.js", | ||
"https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js", | ||
"https://cdnjs.cloudflare.com/ajax/libs/html5-qrcode/2.3.8/html5-qrcode.min.js", | ||
"https://cdnjs.cloudflare.com/ajax/libs/autosize.js/6.0.1/autosize.min.js", | ||
"https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined", | ||
]; | ||
|
||
// Call Install Event | ||
self.addEventListener("install", (e) => { | ||
console.log("Service Worker: Installed"); | ||
|
||
e.waitUntil( | ||
caches | ||
.open(cacheName) | ||
.then((cache) => { | ||
console.log("Service Worker: Caching Files"); | ||
cache.addAll(cacheAssets); | ||
}) | ||
.then(() => self.skipWaiting()), | ||
); | ||
}); | ||
|
||
// Call Activate Event | ||
self.addEventListener("activate", (e) => { | ||
console.log("Service Worker: Activated"); | ||
// Remove unwanted caches | ||
e.waitUntil( | ||
caches.keys().then((cacheNames) => { | ||
return Promise.all( | ||
cacheNames.map((cache) => { | ||
if (cache !== cacheName) { | ||
console.log("Service Worker: Clearing Old Cache"); | ||
return caches.delete(cache); | ||
} | ||
}), | ||
); | ||
}), | ||
); | ||
}); | ||
|
||
// Call Fetch Event | ||
self.addEventListener("fetch", (e) => { | ||
console.log("Service Worker: Fetching"); | ||
e.respondWith( | ||
fetch(e.request) | ||
.then((res) => { | ||
// Make clone of response | ||
const resClone = res.clone(); | ||
// Open cache | ||
caches.open(cacheName).then((cache) => { | ||
// Add response to cache | ||
cache.put(e.request, resClone); | ||
}); | ||
return res; | ||
}) | ||
.catch(async (err) => { | ||
console.error("Fetch failed; returning cached resource instead.", err); | ||
return await caches.match(e.request); | ||
}), | ||
); | ||
}); |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
body, | ||
html { | ||
height: 100%; | ||
height: 100vh; | ||
margin: 0; | ||
display: flex; | ||
justify-content: center; | ||
|