• 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: Alexander Pope

    “A little learning is a dangerous thing; drink of it deeply, or taste it not, for shallow thoughts intoxicate the brain, and drinking deeply sobers us again.” — Alexander Pope

  • Announcing Adeptware

    Adeptware :: Software consultancy specializing in Ruby, Java, .NET and PHPI’m pleased to announce that the web site for my new software consulting company, Adeptware, is now live. Half the battle in starting something new is picking a good name for it. The name was the result of a combination of suggestions from my family. I’m quite happy with what we came up with, and even happier that the domain name was available. I plan on expanding the site later on, but for now it’s suitable for what I want to say.

    My goal for the company is to solve clients’ business problems through custom software development in Ruby, Java, .NET and PHP. I’m also interested in helping clients implement agile processes in their own environments, especially automated testing and continuous integration, two things that I’ve had a lot of experience with.

    I plan on leveraging my background in agile methodologies and a variety of programming languages and techniques to create compelling, durable software for clients. Whether you’re a startup, small business, or Fortune 500 company, I’m interested in working with you. If you’d like more information, feel free to contact me via the web site or simply post a comment below.

  • Quote of the Week: George Bernard Shaw

    “Everything happens to everybody sooner or later if there is time enough.” — George Bernard Shaw

  • Waiting rooms are evil

    If we could recapture the time wasted sitting in tiny rooms waiting to be called on to see the doctor, we could solve world hunger and declare global peace in a single day.

    Why yes, I did visit the doc today. How ever did you guess?

    After being told to show up a half-hour early to fill out paperwork that took 5 minutes to complete, then sitting around for the next hour reading ancient copies of Parents magazine, you can imagine the mood I was in when I was finally called. God is obviously trying to teach me patience through these ordeals, it’s just my own thick skull that’s getting in the way.

    God must also be testing my tolerance for pain since this is the third time in the past few months that a doc has found the need to “practice” (yes, it’s a pun) an especially painful procedure on me, albeit for my own (eventual) good. At least the pain brought about some immediate good by making me consider an interesting question: do we place the same amount of faith in God that we do in medical professionals?

    Ahhh, the theology of the doctor’s office!