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

Fallback to default image #143

Closed
chenxsan opened this issue Jul 9, 2016 · 2 comments
Closed

Fallback to default image #143

chenxsan opened this issue Jul 9, 2016 · 2 comments

Comments

@chenxsan
Copy link

chenxsan commented Jul 9, 2016

I'm not sure how to define fallback image for some url patterns as https://jakearchibald.com/2014/offline-cookbook/#generic-fallback describe in sw-precache.

I had default_profile_image.png cached in staticFileGlobs. When offline, if profile images are not in cache, then fallback to default_profile_image.png.

It seems runtimeCaching is the right place to configure my fallback image then I have to write my own request handler, it would be really complex.

Wondering if there's any better way to do it in sw-precache.

@chenxsan
Copy link
Author

chenxsan commented Jul 9, 2016

I think it can be done with sw-toolbox directly as https://developers.google.com/web/showcase/2015/service-workers-iowa#speaker_profile_images showed.

@chenxsan
Copy link
Author

chenxsan commented Jul 9, 2016

Just pass the function to handler like:

    runtimeCaching: [
      {
        urlPattern: /profile_images.*normal\./,
        handler: function (request, values, options) {
          return toolbox.networkOnly(request).catch(function () {
            return toolbox.cacheOnly(new Request('/img/default_profile_2_normal.png'))
          })
        }
      },

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

No branches or pull requests

1 participant