I use Vite PWA to configure the service worker for a react site that uses code splitting and lazy loading. The Workbox manages to precache almost all asset files hosted on a Cloudflare subdomain (resources.fakesite.com) mapped to a S3 bucket. But Workbox is unable to cache a very specific css file from Yet Another React Lightbox
due to this CORS error:
Access to fetch at 'https://resource.fakesite.com/0.9/assets/index-b9a60426.css' from origin 'https://www.fakesite.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
My node server already sets these CORS headers :
const corsMiddleWare = cors({
origin: ["fakesite.com", "resources.fakesite.com"],
credentials: true,
optionsSuccessStatus:200,
});
app.use(corsMiddleWare);
I don't understand why other js and css files are able to get cached by Workbox but not this very specific CSS file due to Missing Allow Origin Header
.
My S3 bucket CORS:
[
{
"AllowedHeaders": [
"Authorization",
"Content-*"
],
"AllowedMethods": [
"GET",
"HEAD",
"PUT",
"POST"
],
"AllowedOrigins": [
"https://www.faksesite.com"
],
"ExposeHeaders": [],
"MaxAgeSeconds": 3000
},
]
Updated:
This could be related to this issue: https://stackoverflow.com/a/55265139/2598292.
I used the console tool to send two fetch requests:
1.fetch(https://resource.fakesite.com/0.9/assets/index-b9a60426.css`)
2.fetch(https://resource.fakesite.com/0.9/assets/index-b9a60426.css?x-request=xhr`)
The second one works without the 'Access-Control-Allow-Origin' header is present on the requested resource.
error.