• 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.


  • A hamster and a snake, best friends?

    This is a cute article I thought I’d pass along. It just goes to show that even the most disagreeable of companions can get along if they really try hard!

    “Gohan and Aochan make strange bedfellows: one’s a 3.5-inch dwarf hamster; the other is a 120 centimeter-long rat snake. Zookeepers at Tokyo’s Mutsugoro Okoku zoo presented the hamster to Aochan as a tasty morsel in October. But instead of indulging, Aochan decided to make friends with the furry rodent, according to keeper Kazuya Yamamoto. The pair have shared a cage since.”

    Read the full Associated Press story

  • Table saw won’t cut off your finger

    Noticed this digg today and immediately thought of a software parallel. The saw constantly monitors its electrical state for changes and shuts down when one is detected. Isn’t this just like Continuous Integration?

  • Wireless broadband hits Greensboro this year

    A friend pointed me to Clearwire a couple of days ago. They currently offer wireless broadband for much of the West Coast and a few other areas of the United States, but what excited me was that they plan to start offering service in Greensboro sometime this year. I wonder when Raleigh might get coverage?

    Clearwire uses cell towers to provide Internet service to devices throughout metropolitan areas. This sounds very similar to what Nextel tried a year or so ago. Does anyone else have news on other wireless service providers who may be setting up camp in North Carolina soon?

  • If you want to cut a bolt, use a bolt cutter

    I saw an EDS ad in Forbes today which had what I thought was an interesting tagline:

    You can talk about it.
    You can think about it.
    You can hope for it.
    But if you actually want to cut a bolt, use a bolt cutter.

    This is so true. Yet as a software developer, this is something I tend to forget. I can blog all day about how wonderful Ruby is, but if I really want to prove its worth I should be willing to do something with it. Likewise, if I haven’t actually created something in C#, I have no basis for commenting on the language, either negatively or positively. Experience breeds credibility and authority.

    Don’t just talk about cutting a bolt. Get out that bolt cutter and use it!