• CORS woes on Heroku

    ,

    After spending the past 4 hours attempting to solve what boiled down to a rather simple problem, I figure I’d better blog about it to save someone else the time and effort.

    If you’ve been leveraging Passenger’s new –nginx-config-template command line option to add CORS headers to static assets served from a Rails app hosted on Heroku, and the CORS headers recently disappeared under mysterious circumstances… read on.

    I’ve been using the method described here to add CORS headers to custom fonts served from a Heroku-hosted Rails app that’s proxied by Nginx which handles serving static files. I recently updated to Rails 4.2.2 and suddenly, my custom fonts (.woff and .woff2 files) no longer had CORS headers on them.

    After the aforementioned hours spent scratching my head, I discovered that the latest version of the sprockets gem is generating asset digests that are 64 chars in length, where previously they had been 32. Nginx’s default regexp for identifying requests for static assets assumes the digest will be 32 chars long, like so:

    # Rails asset pipeline support.
    location ~ "^/assets/.+-[0-9a-f]{32}\..+" {
      error_page 490 = @static_asset;
      error_page 491 = @dynamic_request;
      recursive_error_pages on;</code>
    
      if (-f $request_filename) {
        return 490;
      }
      if (!-f $request_filename) {
        return 491;
      }
    }
    

    Changing the regexp to recognize digests that are 64 chars in length immediately solved the problem:

    location ~ "^/assets/.+-[0-9a-f]{64}\..+" {
       ...
    }
    

    I had to laugh after something so stupid and silly cost me a good chunk of my Saturday to debug. But at least it’s working now. My statically served custom fonts have the correct CORS headers and Chrome and Firefox are happy again.


Need help?

I’m an independent software developer available for consulting, contract work, or training. Contact me if you’re interested.


  • Quote of the Week: Napoleon Hill

    “It takes more than a college degree to make one a person of education. Any person who is educated is one who has learned to get whatever he wants in life without violating the rights of others. Education consists, not so much of knowledge, but of knowledge effectively and persistently applied. Men are paid, not merely for what they know, but more particularly for what they do with that which they know.” — Napoleon Hill

  • Quote of the Week: Psalm 8:1

    “O Lord, our Lord, how majestic is your name in all the earth! You have set your glory above the heavens.” — Psalm 8:1

  • Willy Gates and the e-mail factory

    It’s not my intention to bash Microsoft with this post, but what I just experienced is so ridiculous I have to blog about it.

    I recently downloaded Microsoft’s free version of Visual C# 2005 and a few days later, began being harassed by a tiny balloon which would pop up every half-hour or so reminding me that I needed to register within 30 days or I wouldn’t be able to use the software anymore. Okay, fine, I bit the bullet and decided to register.

    The first page I encountered required me to sign up for Microsoft Passport. But wait, I thought Passport was dead? And why force me to sign up just to register my software?

    Continuing on, I received a confirmation e-mail for my Passport registration. I also received an e-mail requesting that I verify my e-mail address by clicking on a link, which I did.

    In the meantime, I had finally gotten to the Visual C# registration page and had entered in my information. Clicking save resulted in another e-mail in my box, asking me to confirm that my e-mail address is correct. But wait, didn’t I do this already? Oh yeah, that was just for Passport. Don’t bother sharing information between applications, guys. Sheesh.

    After receiving confirmation for my Visual C# registration, I had received a total of four… yes four… e-mails after registering a single product.

    Does it really have to be this way?

  • Quote of the Week: Bruce Barton

    “Nothing splendid has ever been achieved except by those who dared believe that something inside them was superior to circumstance.” — Bruce Barton