From 3580941207b48295badb5e53f962a01f2670472c Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Wed, 30 Nov 2022 16:18:18 -0800 Subject: [PATCH] Add functional service worker caching towards local first functionality, fix favicon link issue in ReviewDetails.html Co-authored-by: rheabhutada02 Signed-off-by: Arthur Lu --- source/ReviewDetails.html | 2 +- source/assets/scripts/ReviewDetails.js | 2 +- source/assets/scripts/main.js | 13 ++++++++ source/sw.js | 45 ++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 source/sw.js diff --git a/source/ReviewDetails.html b/source/ReviewDetails.html index 3e71c32..b526516 100644 --- a/source/ReviewDetails.html +++ b/source/ReviewDetails.html @@ -7,7 +7,7 @@ Food Journal - + diff --git a/source/assets/scripts/ReviewDetails.js b/source/assets/scripts/ReviewDetails.js index c9e05a0..7908fde 100644 --- a/source/assets/scripts/ReviewDetails.js +++ b/source/assets/scripts/ReviewDetails.js @@ -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"); diff --git a/source/assets/scripts/main.js b/source/assets/scripts/main.js index f012cb9..710cb16 100644 --- a/source/assets/scripts/main.js +++ b/source/assets/scripts/main.js @@ -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(); \ No newline at end of file diff --git a/source/sw.js b/source/sw.js new file mode 100644 index 0000000..2f9f21a --- /dev/null +++ b/source/sw.js @@ -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); + }); + })); +});