Skip to content

Commit

Permalink
service worker caching files successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
rajnidua committed Oct 15, 2021
1 parent e2093f2 commit 3efdd7c
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
24 changes: 24 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,29 @@
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
<script src="index.js"></script>
<script src="db.js"></script>
<!--
<script>
if ("serviceWorker" in navigator) {
window.addEventListener("load", () => {
navigator.serviceWorker
.register("./service-worker.js")
.then((reg) => {
console.log("We found your service worker file!", reg);
});
});
}
</script> -->
<script type="text/javascript">
(function () {
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("./service-worker.js", { scope: "/" })
.then(() => console.log("Service Worker registered successfully."))
.catch((error) =>
console.log("Service Worker registration failed:", error)
);
}
})();
</script>
</body>
</html>
82 changes: 82 additions & 0 deletions public/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const FILES_TO_CACHE = [
"/",
"/index.html",
"/styles.css",
"/index.js",
"/db.js",

"/manifest.webmanifest",

"/icons/icon-192x192.png",

"/icons/icon-512x512.png",
];

const CACHE_NAME = "static-cache-v2";
const DATA_CACHE_NAME = "data-cache-v1";

// install
self.addEventListener("install", function (evt) {
evt.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
console.log("Your files were pre-cached successfully!");
return cache.addAll(FILES_TO_CACHE);
})
);

self.skipWaiting();
});

self.addEventListener("activate", function (evt) {
evt.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(
keyList.map((key) => {
if (key !== CACHE_NAME && key !== DATA_CACHE_NAME) {
console.log("Removing old cache data", key);
return caches.delete(key);
}
})
);
})
);

self.clients.claim();
});

// fetch
self.addEventListener("fetch", function (evt) {
// cache successful requests to the API
if (evt.request.url.includes("/api/")) {
evt.respondWith(
caches
.open(DATA_CACHE_NAME)
.then((cache) => {
return fetch(evt.request)
.then((response) => {
// If the response was good, clone it and store it in the cache.
if (response.status === 200) {
cache.put(evt.request.url, response.clone());
}

return response;
})
.catch((err) => {
// Network request failed, try to get it from the cache.
return cache.match(evt.request);
});
})
.catch((err) => console.log(err))
);

return;
}

// if the request is not for the API, serve static assets using "offline-first" approach.
// see https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook#cache-falling-back-to-network
evt.respondWith(
caches.match(evt.request).then(function (response) {
return response || fetch(evt.request);
})
);
});

0 comments on commit 3efdd7c

Please sign in to comment.