3

I am using sw-precache for adding offline support to a web app.

My setup:

  1. Static website with CSS/JS/HTML Dynamic content is handled via custom indexedDB solution.
  2. Grunt task used generate the service-worker.
  3. sw-precache (4.0.0) used to cache ALL the static assets.
  4. 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() Console debug output of promise object passed to respondWith

So my questions:

  1. Why does sw-precache not fallback to network when it does not find the entry in cache?
  2. Is there anyway to force network request when a request entry is not found in cache?
Ravi Gidwani
  • 198
  • 12
  • 1
    I'm voting to close this question as off-topic because this should be reported via the `sw-precache` issue tracker. I've moved it to https://github.com/GoogleChrome/sw-precache/issues/185 – Jeff Posnick Sep 19 '16 at 18:02
  • 1
    For future reference, this is a bug that will be fixed with https://github.com/GoogleChrome/sw-precache/pull/186 – Jeff Posnick Sep 19 '16 at 18:54
  • 1
    @JeffPosnick Thanks for reporting in sw-precache. Normally I have seen module owners asking users to use forums for questions/issues rather than the issue tracker. Hence the post. I will followup in tracker. Thanks again! – Ravi Gidwani Sep 20 '16 at 19:58
  • In this case it was an actual bug, so having a record of it in the `sw-precache` issue tracker was important. – Jeff Posnick Sep 20 '16 at 20:04

0 Answers0