1

I am trying to make a site available offline and to cache images retrieved from AWS S3. I am using the workbox library:

var CACHE_VERSION = '2019-02-03'

// cf https://developers.google.com/web/tools/workbox/guides/get-started
importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.6.1/workbox-sw.js')

workbox.routing.registerRoute(
  // Cache image files
  /https:\/\/s3\.amazonaws\.com\/myproject\/img\/.*\.(?:png|jpg|jpeg|svg)/,
  // Use the cache if it's available
  workbox.strategies.cacheFirst({
    // Use a custom cache name
    cacheName: 'image-cache-' + CACHE_VERSION,
    plugins: [
      new workbox.expiration.Plugin({
        // Cache for a maximum of a week
        maxAgeSeconds: 7 * 24 * 60 * 60,
      })
    ],
  })
)

This results in the following error message from workbox in the console:

The response for 'https://s3.amazonaws.com/myproject/img/icon.png' is an opaque response. The caching strategy that you're using will not cache opaque responses by default.

I have added the crossorigin='anonymous' attribute for those images, and I have this CORS configuration on AWS S3:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  <CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>DELETE</AllowedMethod>
    <AllowedHeader>*</AllowedHeader>
  </CORSRule>
  <CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>HEAD</AllowedMethod>
    <AllowedHeader>*</AllowedHeader>
  </CORSRule>
</CORSConfiguration>

Am I not using CORS properly? How to make the response a normal one and not an opaque one?

vmarquet
  • 2,384
  • 3
  • 25
  • 39
  • If you are not aware yet, we have an official documentation on how to [Handle Third Party Requests](https://developers.google.com/web/tools/workbox/guides/handle-third-party-requests). At some point in the documentation, you will be redirected where you will be able learn more about [opaque responses](https://stackoverflow.com/questions/39109789/what-limitations-apply-to-opaque-responses) applied to it. – MαπμQμαπkγVπ.0 Feb 05 '19 at 08:03
  • @MαπμQμαπkγVπ.0 I am aware of that documentation, but this is not what I want. I don't want to cache opaque responses, I want the responses to not be opaque. – vmarquet Feb 05 '19 at 12:02

1 Answers1

0

Okay this was a stupid mistake. I was doing the right thing, but the crossorigin="anonymous" attribute was set on some of the images and not on the others. Properly setting it on all the images fixes the problem.

The site I'm working on uses a combination of background images in an unusual way, which made it harder to debug. The image on top properly had the crossorigin attribute, but the other images hidden under it did not have it, and were causing everything to fail, including the display of the image on top, which was properly cached in offline mode. Stupid mistake.

vmarquet
  • 2,384
  • 3
  • 25
  • 39