2022-12-01 00:18:18 +00:00
|
|
|
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",
|
2022-12-02 00:48:15 +00:00
|
|
|
"assets/images/create_button.png",
|
2022-12-01 00:18:18 +00:00
|
|
|
"assets/images/default_plate.png",
|
|
|
|
"assets/images/delete_icon_for_interface.png",
|
|
|
|
"assets/images/edit_button_for_interface.png",
|
|
|
|
"assets/images/home_button_for_interface.png",
|
|
|
|
"assets/images/favicon.ico",
|
|
|
|
"assets/images/Logo.png",
|
2022-12-02 00:48:15 +00:00
|
|
|
"assets/images/search_button.png",
|
2022-12-01 00:18:18 +00:00
|
|
|
"assets/scripts/CreatePage.js",
|
|
|
|
"assets/scripts/localStorage.js",
|
|
|
|
"assets/scripts/main.js",
|
|
|
|
"assets/scripts/ReviewCard.js",
|
2022-12-01 22:32:50 +00:00
|
|
|
"assets/scripts/ReviewDetails.js"
|
2022-12-01 00:18:18 +00:00
|
|
|
];
|
|
|
|
|
2022-12-01 08:37:49 +00:00
|
|
|
/**
|
|
|
|
* Adds the install listener where the app assets are added to the cache
|
|
|
|
*/
|
2022-12-01 00:18:18 +00:00
|
|
|
self.addEventListener("install", async () => {
|
2022-12-01 08:37:49 +00:00
|
|
|
// open the cace
|
2022-12-01 00:18:18 +00:00
|
|
|
const cache = await caches.open(CACHE_NAME);
|
2022-12-01 08:37:49 +00:00
|
|
|
// add all elements in ASSETS to the cache, these are all the files requried for the app to run
|
2022-12-01 00:18:18 +00:00
|
|
|
await cache.addAll(ASSETS);
|
|
|
|
});
|
|
|
|
|
2022-12-01 08:37:49 +00:00
|
|
|
/**
|
|
|
|
* Adds an event listener on fetch events to serve cached resources while offline
|
|
|
|
* Uses a network first structure to prioritize fetching from network in case of app updates.
|
|
|
|
* If there are important updates, we want the user to get those if possible.
|
|
|
|
*/
|
2022-12-01 00:18:18 +00:00
|
|
|
self.addEventListener("fetch", (event) => {
|
2022-12-01 08:37:49 +00:00
|
|
|
// add a response to the fetch event
|
2022-12-01 00:18:18 +00:00
|
|
|
event.respondWith(caches.open(CACHE_NAME).then((cache) => {
|
2022-12-01 08:37:49 +00:00
|
|
|
// try to return a network fetch response
|
2022-12-01 00:18:18 +00:00
|
|
|
return fetch(event.request).then((fetchedResponse) => {
|
2022-12-01 08:37:49 +00:00
|
|
|
// if there is a response, add it to the cache
|
2022-12-01 00:18:18 +00:00
|
|
|
cache.put(event.request, fetchedResponse.clone());
|
2022-12-01 08:37:49 +00:00
|
|
|
// return the network response
|
2022-12-01 00:18:18 +00:00
|
|
|
return fetchedResponse;
|
|
|
|
}).catch(() => {
|
2022-12-01 08:37:49 +00:00
|
|
|
// If there is not a network response, return the cached response
|
|
|
|
// The ignoreVary option is used here to fix an issue where the service worker
|
|
|
|
// would not serve certain requests unless the page was refreshed at least once
|
2022-12-02 01:01:05 +00:00
|
|
|
return cache.match(event.request, {ignoreVary: true, ignoreSearch: true});
|
2022-12-01 00:18:18 +00:00
|
|
|
});
|
|
|
|
}));
|
|
|
|
});
|