Add functional service worker caching towards local first functionality,

fix favicon link issue in ReviewDetails.html
Co-authored-by: rheabhutada02 <rheabhutada02@users.noreply.github.com>

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2022-11-30 16:18:18 -08:00
parent fa7b8dc7f5
commit 3580941207
4 changed files with 60 additions and 2 deletions

View File

@ -7,7 +7,7 @@
<title>Food Journal</title>
<!--Add Favicon-->
<link rel="icon" type="image/x-icon" href="./assets/images/icons/favicon.ico">
<link rel="icon" type="image/x-icon" href="./assets/images/favicon.ico">
<!-- Recipe Card Custom Element -->
<script src="assets/scripts/ReviewCard.js" type="module"></script>

View File

@ -99,7 +99,7 @@ function setupUpdate(){
while (tagContainer.firstChild) {
tagContainer.removeChild(tagContainer.firstChild);
}
let tagSetVal = currReview["tags"][i].toLowerCase()
let tagSetVal = currReview["tags"][i].toLowerCase();
for (let i = 0; i < currReview["tags"].length; i++) {
tagSet.add(tagSetVal);
let newTag = document.createElement("label");

View File

@ -40,3 +40,16 @@ function initFormHandler() {
window.location.assign("./CreatePage.html");
});
}
const registerServiceWorker = async () => {
if ("serviceWorker" in navigator) {
try {
const registration = await navigator.serviceWorker.register("./sw.js", {
scope: "./",
});
} catch (error) {
console.error(`Registration failed with ${error}`);
}
}
};
registerServiceWorker();

45
source/sw.js Normal file
View File

@ -0,0 +1,45 @@
const CACHE_NAME = "food-journal-v1";
const ASSETS = [
"index.html",
"ReviewDetails.html",
"CreatePage.html",
"static/CoveredByYourGrace-Regular.ttf",
"static/CreatePage.css",
"static/Form.css",
"static/homepage.css",
"static/ReviewDetails.css",
"assets/images/0-star.svg",
"assets/images/1-star.svg",
"assets/images/2-star.svg",
"assets/images/3-star.svg",
"assets/images/4-star.svg",
"assets/images/5-star.svg",
"assets/images/default_plate.png",
"assets/images/delete_icon_for_interface.png",
"assets/images/edit_button_for_interface.png",
"assets/images/Grouppink.png",
"assets/images/home_button_for_interface.png",
"assets/images/favicon.ico",
"assets/images/Logo.png",
"assets/scripts/CreatePage.js",
"assets/scripts/localStorage.js",
"assets/scripts/main.js",
"assets/scripts/ReviewCard.js",
"assets/scripts/ReviewDetails.js",
];
self.addEventListener("install", async () => {
const cache = await caches.open(CACHE_NAME);
await cache.addAll(ASSETS);
});
self.addEventListener("fetch", (event) => {
event.respondWith(caches.open(CACHE_NAME).then((cache) => {
return fetch(event.request).then((fetchedResponse) => {
cache.put(event.request, fetchedResponse.clone());
return fetchedResponse;
}).catch(() => {
return cache.match(event.request);
});
}));
});