I am using sw-precache for adding offline support to a web app.
My setup:
- Static website with CSS/JS/HTML Dynamic content is handled via custom indexedDB solution.
- Grunt task used generate the service-worker.
- sw-precache (4.0.0) used to cache ALL the static assets.
- cache headers on service-worker.js set on apache as follows:
<LocationMatch "service-worker\.js.*"> Header set Cache-Control "max-age=0, no-cache" </LocationMatch>
Things seem to work fine in usual cases. But if for some reason, the user clears the cache (e.g In chrome dev tools -> Applications -> Clear Storage (Select everything) -> click 'clear selected'), my web pages don't load even If I am online and I see following error in console:
The FetchEvent for "https://my.somedomain.com/somepath/somepage.html" resulted in a network error response: an object that was not a Response was passed to respondWith().
When I look at the generated service-worker.js, I notice the following code.
// If shouldRespond was set to true at any point, then call
// event.respondWith(), using the appropriate cache key.
if (shouldRespond) {
event.respondWith(
caches.open(cacheName).then(function(cache) {
return cache.match(urlsToCacheKeys.get(url));
}).catch(function(e) {
// Fall back to just fetch()ing the request if some unexpected error
// prevented the cached response from being valid.
console.warn('Couldn\'t serve response for "%s" from cache: %O', event.request.url, e);
return fetch(event.request);
})
);
When debugging, I don't see the catch block getting executed. Also see the attached debug output of the object returned catch.match that is passed to respondWith()
So my questions:
- Why does sw-precache not fallback to network when it does not find the entry in cache?
- Is there anyway to force network request when a request entry is not found in cache?