How To Call The Service Worker From The Web App Context

Image for post
Image for post
Photo by Arindam Saha on Unsplash

How To Call The Service Worker From The App Context

workbox.routing.registerRoute(
/\.(?:png|gif|jpg|jpeg|webp|svg)$/,
new workbox.strategies.CacheFirst({
cacheName: 'images',
plugins: [
new workbox.expiration.Plugin({
maxAgeSeconds: 30 * 24 * 60 * 60,
maxEntries: 60
})
]
})
);
async function addToCache() {
const list = ['/assets/img1.svg', '/assets/img2.svg'];

const myCache = await window.caches.open('images');
await myCache.addAll(list);
}
document.addEventListener('DOMContentLoaded', ($event) => {
addToCache();
});
<button onclick="addToCache()">Go images offline</button>

Freelancer by day | Creator of DeckDeckGo by night | Organizer of the Ionic and IndieHackers Zürich Meetup

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store