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


  • I attended the 2007 National Conference on Christian Apologetics in Charlotte, North Carolina this past weekend. One thing (among many) that I realized while participating was that I needed a copy of the Bible on my MacBook. I brought my MB for note taking since my handwriting is woefully slow, but it would have been great if I could have looked up scripture references without leaving the keyboard.

    I don’t just want a PDF. I want a nice OS X-style application with a built-in concordance and whatnot. Googling around this evening has revealed two possibilities: MacSword (free) and Logos (commercial). Logos isn’t due to be released for the Mac for some time yet, so I’ll be trying out MacSword for now. Anyone have suggestions on other apps that I might have missed? Or maybe there is a generic eBook reader for the Mac out there that I haven’t stumbled across yet?

  • Introducing the Lindo (response_visualizer) plugin for Rails

    Being at RubyConf this weekend has provided the necessary motivation for me to finally release a new Ruby gem I’ve been building. It’s an extraction from a project I’ve been working on for Relevance.

    I’m a regular user of assert_select in my functional tests. I find myself frequently doing something like this when the assertion is failing and I can’t figure out why:

    def test_something
      post :something
      raise @response.body.inspect
      assert_select "div[id=header]"
    end
    

    Inspecting the response body usually leads me to a solution, but it’s tedious parsing through the huge amount of HTML that gets returned, often in a semi-unreadable format. Enter the response_visualizer plugin (which has since been renamed Lindo):

    script/plugin install http://github.com/adeptware/lindo/
    

    Or clone the project directly:

    git clone http://github.com/adeptware/lindo/
    

    The plugin provides a vr method to your functional tests. When this method is called, the response body is automatically opened in the default browser allowing for easy visual inspection of the page’s content:

    post :new
    vr
    ...
    

    If you’d prefer to jump straight to the source code, passing the :html symbol will open the formatted HTML in the default text editor:

    post :new
    vr(:html)
    ...
    

    This has saved me a lot of time in figuring out why a specific assertion is failing. Instead of parsing through the HTML, I can view the entire page and immediately tell if something is missing or out of place. I find myself calling vr even before I write my assertions now.

    After installing, check out the README file for additional documentation. There is also a GitHub project if you’d like to contribute a patch or fork the code. Enjoy!

  • I haven’t gotten up at 5 AM in a long time, but today I did and, after a 2.5 hour drive from Raleigh to Charlotte, am now at RubyConf. Here are some pics from the opening session:

    071102_091619.jpg 071102_091636.jpg