I am trying to add links to my assets in workbox service worker to cache. And I want them to load from cache then look to the network. I am new to workbox so I dont know what to do. Please help. Here's the workbox code.
importScripts('/local-res/comps/wb-ox-sw.js');
'use strict';
var cacheVersion = 1,
currentCache = {
offline: 'OFFSERVE' + cacheVersion
},
offlineUrl = '/mes_srv_off.html';
this.addEventListener('install', event => {
event.waitUntil(
caches.open(currentCache.offline).then(function(cache) {
return cache.addAll([
offlineUrl
]);
})
);
});
this.addEventListener('fetch', event => {
if (event.request.mode === 'navigate' || (event.request.method === 'GET' && event.request.headers.get('accept').includes('text/html'))) {
event.respondWith(
fetch(event.request.url).catch(error => {
return caches.match(offlineUrl);
})
);
}
else{
event.respondWith(caches.match(event.request)
.then(function (response) {
return response || fetch(event.request);
})
);
}
});
workbox.routing.registerRoute(
({request}) => request.destination === 'image',
new workbox.strategies.CacheFirst()
);
workbox.routing.registerRoute(
({request}) => request.destination === 'style' ||
request.destination === 'script',
new workbox.strategies.CacheFirst()
);