Within my company, there are numerous applications that rely on shared resources stored on AWS S3. Some of these applications utilize the crossorigin="anonymous" HTML element, while others do not. One issue we face is that AWS does not send back CORS response headers such as 'Allow-access-control-origin' when no Origin request header is present. This can result in users encountering browser cache versions of files that lack CORS response headers.
As a consequence, users accessing our team's application may experience failures with the Service Worker due to the non-CORS format of assets stored in the browser disk cache. The error message typically appears as follows:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8001' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Using an opaque response for reliable caching is not a viable solution.
I have explored the option of applying a cache-control request header to bypass the browser cache. However, the Headers object within the Fetch API Request is immutable, preventing me from adding headers to the existing request. Creating a new Request also poses challenges since I cannot receive a CORS response from AWS due to the forbidden nature of setting the Origin header in the Fetch API.
Potential solutions to this problem could involve:
Requiring all teams within the company to use the crossorigin HTML attribute, which unfortunately is not feasible.
Making AWS consistently respond with CORS headers, yet this is beyond my control.
Executing a Fetch Request with an Origin header, which is currently restricted.
Persuading my organization to implement cache-control headers that prevent browser caching of assets, though this might not be ideal.
Are there any steps I can take to address this challenge? At present, my interim solution involves disabling Service Worker caching for these shared assets to mitigate network issues.