question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CORS issue, using Rack-cors gem with cloudfront assets

See original GitHub issue

I have a rails 4 app hosted on AWS with assets served on cloudfront. I use the rack-cors gem to add cors headers. My rack-cors settings allows only check for my domain hence cannot provide jsfiddle example.

curl -H "Origin:https://staging.beenlightened.com" -I https://d3czu6zp3f06wr.cloudfront.net/assets/vitality4-064f8c9848768917287e1a7dbcec1749.png

screen shot 2016-10-02 at 4 06 08 pm

Notice CORS headers are added correctly. However html2canvas still complains about cors headers not being present.

screen shot 2016-10-02 at 4 25 06 pm

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
fuggfuggfuggcommented, May 15, 2017

From what I remember: When the page loads the first time, the browser caches the image. Since the domain is the same, the CORS headers are not present. So now when I try to print the image it used the cached image and html2canvas complained about CORS headers not present.

My only solution was to add a timestamp in the ImageContainer method inside the library like so:

if (cors) {
        self.image.crossOrigin = "anonymous";
        // special check for cloudfront images only.
        if (src.toLowerCase().includes("cloudfront")){
          src = src + '?123';
        }
    }

I’ve spent ages on this damn thing. Hope it works for you man.

0reactions
fuggfuggfuggcommented, Jul 30, 2017

Can you make an example on codepen or jsfiddle?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cloudfront CORS issue serving fonts on Rails application
The rack-cors gem implements the CORS protocol in a Rack middleware. In addition to setting Access-Control-Allow-Origin and related headers ...
Read more >
CORS issue with Rails app and AWS Cloudfront(?) - Fly.io
I'm using the rack-cors gem and an initializer that includes all my origins (the Rails app on fly.io as well as the Cloudfront...
Read more >
Various CORS issues related (not only to) Ruby on Rails
I'm writing this article after week full of CORS issues that I've ... Ruby on Rails asset files under CDN pointing to other...
Read more >
Notes on Cloudfront in front of Rails Assets on Heroku, with ...
AWS docs here and here suggest for CORS support you also need to configure the Cloudfront distribution to forward additional headers — Origin, ......
Read more >
Cross-Origin Resource Sharing | reinteractive
Happily, the solution was quite simple. Use the rack-cors gem (source code). Because we aren't using S3 for our assets, we just need...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found