Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to fetch() if the precached response is missing. #1302

Merged
merged 1 commit into from
Feb 14, 2018

Conversation

jeffposnick
Copy link
Contributor

R: @addyosmani @gauntface

This is the last 1/3 of #1188

If the browser doesn't have a cache match for a URL that should have been precached, this will cause it to go to the network instead of returning an error.

`Falling back to the network instead.`);
}

return fetch(precachedUrl);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we cache the file as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. The downside of caching is that we could end up in a state where we have a response cached that doesn't match the revision information inlined in the manifest/stored in IDB.

I don't know if that's preferable to having nothing cached at all.

One advantage to just logging rather than also attempting to "repair" the cache is that a developer who sees this message would know that their cache is in an inconsistent state. If we attempted to repair the cache but end up caching a different revision of a given URL, that won't end up logging anything the next time the same resource is requested, and the inconsistency would be hidden.

When implementing something similar in sw-precache, I just went with the network + logging route:

https://github.com/GoogleChromeLabs/sw-precache/blob/2ee24a96bb4812e83f47f6859361ba5e1bd535f5/service-worker.tmpl#L171-L176

@jeffposnick jeffposnick merged commit 8a1f659 into v3 Feb 14, 2018
@jeffposnick jeffposnick deleted the precaching-fetch-fallback branch February 14, 2018 19:59
@th3hunt
Copy link

th3hunt commented Mar 4, 2018

Oh thank you!

After reading this about Safari,

To keep only the stored information that is useful to the user, WebKit will remove unused service worker registrations after a period of a few weeks. Caches that do not get opened after a few weeks will also be removed. Web Applications must be resilient to any individual cache, cache entry or service worker being removed.
https://webkit.org/blog/8090/workers-at-your-service/

I was looking for a workaround using some of the less popular workbox 2.x APIs.

Seeing this in v3 is a relief :)

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

Successfully merging this pull request may close these issues.

3 participants