Chrome can't load cached responses for statics when missing CORS headers
See original GitHub issueOn chrome, I’ve sometimes seen an issue where the CSS isn’t loaded. Looking at the responses for the CSS there’s no CORS headers in the response:
Age: 583098
Cache-Control: max-age=315360000, immutable
Connection: keep-alive
Date: Thu, 02 Aug 2018 10:35:25 GMT
Expires: Wed, 26 Jul 2028 16:23:50 GMT
Server: AmazonS3
Vary: Accept-Encoding
Via: 1.1 c31d49af55fff364fbd11e21a32f7fcb.cloudfront.net (CloudFront)
X-Amz-Cf-Id: 4XuBVKTsYSjZMOP4N-D6BKMykZiFEAKmN_3qmiqDoE1cmYrzb6EeEA==
x-amz-version-id: Jx_OHlhrHy8mZwyTzxdwMZaEFoaw7Rav
X-Cache: Hit from cloudfront
I also see the following in the console:
Access to CSS stylesheet at 'https://addons-amo.cdn.mozilla.net/amo-1e82033f757cb2138991a4cf400ee756.css' from origin 'https://addons.mozilla.org' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://addons.mozilla.org' is therefore not allowed access.
Digging into this further it appears that if a cached response has no cors headers then it will fail to load the second time if CORS headers are required.
A hard refresh fixes the issue.
I’m not sure how it gets into this state, but a way to reproduce the problem can be carried out as follows.
- Find the URL for the current CSS file loaded on addons.mozilla.org.
- Load that in a new Chrome incognito window to prevent existing caches messing things up (It’s the same origin (the CDN’s) so no CORS headers will be in the response)
- This file will then be cached
- Now request AMO from https://addons.mozilla.org (different origin need CORS to load for SRI)
- Note there’s no CSS and the CSS load was denied.
I think to fix this we’d need to add Vary: Origin
.
See also this bug https://bugs.chromium.org/p/chromium/issues/detail?id=409090
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:18 (11 by maintainers)
Top Results From Across the Web
CORS issue due to aggressive Chrome cache - Keul Blog
It seems that Chrome is reusing the same request from the cache although the request should be a different ones. If we disable...
Read more >CORS request blocked by a previous normal media request ...
I engaged a problem with CORS on chrome recently, ... the cached-response didn't have CORS headers, so I won't download successfully.
Read more >Resolve the "No 'Access-Control-Allow-Origin' header" error ...
If CORS headers are not returned in the response, then the origin server is not correctly setup for ... Open your distribution from...
Read more >Why doesn't adding CORS headers to an OPTIONS route ...
first simply install cors in your project. Take terminal(command prompt) and cd to your project directory and run the below command: npm install...
Read more >CORS errors and how to solve them - Topcoder
Still facing a CORS error? ... Open a network tab in your console. ... In the response header look for the Access-Control-Allow-Origin header....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ve tested this out on -dev and it looks to solve the problem for chrome.
Since statics via the CDN should be returning:
Access-Control-Allow-Origin: *
I think the way the origin is being set on the request is probably OK as long as it’s only applied to static resources from the CDN.
/cc @willdurand @kumar303 @diox in-case you have other thoughts.
FYI: this change triggered a couple minutes of downtime in production for some users (on multiple browsers) while the deployment of https://github.com/mozilla-services/cloudops-deployment/pull/2451 was pushed to the CDN ahead of code deployment.
This was confusing because the site looked broken; it loaded without styles.