/* for more info visit: https://developer.chrome.com/docs/workbox/caching-strategies-overview/ */ const cacheName = 'rext_cache' const fileTypeDestination = ['image', 'font', 'style'] const requestUrls = [ 'api/translations?path=', 'app-cache-chunk', '.svg' ] function cacheStrategy (event) { event.respondWith(caches.open(cacheName).then((cache) => { // Go to the cache first return cache.match(event.request.url).then((cachedResponse) => { // Return a cached response if we have one if (cachedResponse) { return cachedResponse } // Otherwise, hit the network return fetch(event.request).then((fetchedResponse) => { // Add the network response to the cache for later visits if (fetchedResponse.ok) { cache.put(event.request, fetchedResponse.clone()) } // Return the network response return fetchedResponse }) }) })) } function doesUrlContainsRequestUrls (url) { let result = false requestUrls.forEach((reqUrl) => { if (result) return true else if (url.includes(reqUrl)) result = true }) return result } self.addEventListener('fetch', (event) => { // Check if this is a request for an image if (event.request.method !== 'GET') return if (!(event.request.url.indexOf('http') === 0)) return if (fileTypeDestination.includes(event.request.destination)) { cacheStrategy(event) } else if (doesUrlContainsRequestUrls(event.request.url)) { cacheStrategy(event) } })