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


  • Motivation and goals

    Motivation is generally the natural result of what we’re doing. We’re motivated to get a license so we can drive to the store. We’re motivated to learn a new programming language because our current job is boring. We’re motivated to lose weight because we want to look like the people in the movies. But often, motivation isn’t natural or free.

    Yet most things worth doing in life require the proper motivation. If we have goals that don’t have built-in motivation, it’s up to us to provide what’s lacking. For example, I’m not wild about a consulting gig I’m on right now. The work just isn’t that interesting. I motivate myself to work on the project each day by taking hourly breaks and ending the day with a reward… an hour spent reading a book or watching a movie.

    This is what has worked for me in my particular situation. The things that motivate you will probably be different. Being around certain people can be motivating. Seeing clear progress towards a goal can be motivating. Cash can be motivating. Reading certain books can be motivating.

    Speaking of motivation, Jared Richardson and I have been in the planning stages of a new book for the past few months. We launched this blog to motivate us to move more quickly toward our goal of publishing the book before the next ice age hits. Having a blog about the book makes us accountable to the public. If we aren’t making progress, people will complain. That’s highly motivating for us.

    What motivates you? Tell us in the comments.

  • Ruby Hoedown 2008

    Ruby Hoedown 2008The Ruby Hoedown moves to Hunstville, Alabama this year. I’m filling Nathaniel’s place as co-organizer this go round, along with Jeremy McAnally who was the driving force behind the conference last year too. We’re cooking up a good mix of Ruby talks. It’s not too late to get your own proposal in: the deadline is June 2nd. David A. Black and Chris Wanstrath are keynoting so you’re in good company.

    This year’s conference is shaping up to be another rip-roaring success, with several companies already onboard as sponsors and some nice giveaways in the works. I expect to see y’all down south in Alabama come August to celebrate our favorite language: Ruby!

  • test_spec_on_rails goes Git

    The test_spec_on_rails plugin, which provides some nice helpers for testing Rails applications when using test/spec, has moved from Subversion to Git.

    Check out the profile page or clone the repository itself from this path:

    git://github.com/pelargir/test_spec_on_rails.git