0

I am receiving images URLs from an API response and I want to cache each of those images. I tried just using cache.addAll but it does not work because the images are an opaque response. I am considering fetching each image using fetch and having the routes cache them but I am not sure as to whether this would be the best way. Are there any other better alternatives?

1 Answers1

2

There's some guidance in this Stack Overflow answer that explains how to cache opaque responses; the gist of it is:

const request = new Request('https://third-party-no-cors.com/', {
  mode: 'no-cors',
});

// Assume `cache` is an open instance of the Cache class.
fetch(request).then(response => cache.put(request, response));

The caveat is that your code has no way of knowing whether you're caching a valid response, or a HTTP 404 or some other error.

Jeff Posnick
  • 53,580
  • 14
  • 141
  • 167