Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Deleted cache, FetchEvent error response #185

Closed
jeffposnick opened this issue Sep 19, 2016 · 2 comments · Fixed by #186
Closed

Deleted cache, FetchEvent error response #185

jeffposnick opened this issue Sep 19, 2016 · 2 comments · Fixed by #186
Assignees
Labels

Comments

@jeffposnick
Copy link
Contributor

This was originally asked at http://stackoverflow.com/questions/39563666, but it makes more sense in the sw-precache issue tracker, not on Stack Overflow.

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?
@gravi2
Copy link

gravi2 commented Sep 20, 2016

@jeffposnick Thanks for the opening the issue and for the fix. I do have one question on the fix though. It seems like the fix will fallback to network request in cases where the cache.match() fails return a proper response. Which is good, but it seems it will not update the cache? So if the user goes offline he is still missing that resource?

@jeffposnick
Copy link
Contributor Author

That's right. If the user explicitly chooses to delete their caches, that's an exceptional condition that sw-precache doesn't attempt to undo by repopulating the caches. Obviously the service worker should continue allowing the requests to go through via the network, though, which was the intended behavior and what that fix accomplishes.

(In practice, I think it's much more likely that a user who clears all their caches will also end up unregistering all service workers, which is what happens by default. Keeping a service worker registration but clearing out caches is an edge case.)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants