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


  • Slides from my RailsConf talk

    For those who may have missed my talk at RailsConf, you can download the slides to get a better idea of what the talk was all about.

    I had a blast, by the way! The audience seemed to enjoy it, and I was pleasantly surprised at the number of questions people had about Teascript and the process of building it. About a dozen people came up to me afterwards saying that they had an idea they were mulling over, and my talk had inspired them to get started on it.

    I’ll probably be writing a couple of follow up articles that go into more detail on how homesteading worked for me. I didn’t have enough time to cover everything I wanted to at the presentation due to the enthusiastic response from the audience. To those who attended, thanks for being there and making the talk a success!

  • RailsConf ’07 wrap up

    Portland is rapidly fading into the distance as I fly back towards Raleigh. I’ve enjoyed the past six days immensely. The Ruby/Rails community continues to surprise me with its passion, dedication, and downright uber-geekiness. The highlight of the conference for me was the time spent between talks and during meals, sharing ideas and experiences with other developers. I’m totally worn out now, but I’m already looking forward to next year’s Conf.

    Not everything was peachy keen, though. There are several things that bothered me about the Conf this year…

    (more…)

  • Let the Conf begin

    I just arrived in Portland a few hours ago for RailsConf ’07. I’m looking forward to attending the tutorial sessions tomorrow, not to mention watching presentations by fellow Rails geeks on Friday and Saturday. My own talk on homesteading and Teascript will be given on Saturday at 10:45 AM for those who are interested. Hope to see you there!

  • SVN diffs from TextMate

    I picked up a neat TextMate trick while pair programming with Justin Gehtland on Monday. Check out a directory from a Subversion repository and open it in TextMate. Click on the root directory of the project and hold down Ctrl-Shift-A. A menu will appear allowing you to select from a number of SVN options, including adding and deleting from the repo, diffing files, and so on. Very nice, especially if you’d prefer not to jump to the command line for such actions.