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


  • Rails in the Enterprise

    Tim O’Brien recently cataloged the top four myths that often prevent Rails from being used in Enterprise settings. From performance to flexibility, he does a thorough job covering many the objections that I’ve heard raised myself when proposing Rails as a possible solution. It can be frustrating to be in that situation, but Tim provides some “real answers” that you can fire back when challenged. This is a fascinating read, even for someone who isn’t plugged in to a large corporation right now.

  • RubyConf in the southeast

    The Ruby community in the Research Triangle Park area of North Carolina has been growing by leaps and bounds lately, in large part due to the fantastic job Nathaniel Talbott has been doing with the local Ruby Brigade. Another sign of the growing interest in Ruby and Rails in the area is the just-announced Ruby Hoedown, a RubyConf of sorts that’s being hosted by the Brigade in the Raleigh area on August 10th and 11th. Details are sketchy, but you can register on the site to receive more information as it becomes available. And to all you aspiring speakers, now is the time to start thinking about what you’d like to present at the conference!

  • First Agile RTP meeting tonight

    This is just a reminder from your friendly neighborhood agilist that the new Agile RTP user group has its first meeting in Cary tonight. Read more about it on Jared’s blog. This sounds like a great opportunity to meet other agile-minded people in the area and listen to an interesting talk. Oh, and let’s not forget the bragging rights secured by those who participate in the first meeting of a new user group.

  • Highrise is now taking leases

    37 Signals’ latest web application, Highrise, is now open for business. They offer a free account so you can try out the service without laying down any dinero. Looks like a useful tool. It’ll be interesting to see how tightly it integrates with Basecamp.