Serving Images with Service Worker
Let's create a service worker file sw.js
in the root folder of our project. The code will look like this:
self.addEventListener("fetch", async event => {
event.respondWith(
(async () => {
const cachedResponse = await caches.match(event.request);
if (cachedResponse) return cachedResponse;
return fetch(event.request);
})()
);
});
Also, in app.js
we have to register the service worker file.
navigator.serviceWorker.register("/serviceworker.js");