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


  • Spreedly extension for Radiant

    I built the original Spreedly extension for Radiant a couple of years ago. It’s a nice little package that makes it really easy to integrate Spreedly’s subscription payment system with your Radiant site. You can choose which pages to require a subscription to view, manage subscribers from the admin backend, and so on. It’s long overdue, but I finally got around to upgrading the extension to work flawlessly with Radiant 0.9.1. Check it out and have fun.

  • Learn about A/B testing at raleigh.rb

    I’ll be presenting on A/B testing at tonight’s raleigh.rb meetup. As developers, we use tools like RSpec and Cucumber to verify that our application is functional, but how can we verify that the layout of our home page is user-friendly? How can we determine the ideal size for our signup button? How can we maximize throughput to our signup form? A/B testing is an easy and compelling way to increase the effectiveness of our web applications. Join us tonight to learn how to leverage A/B testing in Ruby using several popular tools.

  • I just returned from BizConf (a fantastic experience which I’ll be summarizing in a future post) and I have to report one very sad thing: out of four airports I passed through, RDU is still the only one that doesn’t offer free wi-fi. Nashville, Tampa, and Jacksonville all have it. RDU charges. This just doesn’t seem right, especially given that out of the aforementioned cities, Raleigh is known as a high tech mecca. This is RTP for cryin’ out loud. Please fix?

  • Quote of the Week: H.G. Wells

    “Affliction comes to us, not to make us sad but sober; not to make us sorry but wise.” — H.G. Wells