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


  • Default methods fixed in fixture_replacement2

    The problem I recently blogged about has been fixed in fixture_replacement2. Thanks, guys!

  • Layout assertion added to test_spec_on_rails

    After contacting Rick with my patch, he offered to give me commit access to the project. I took him up on the offer, so the layout assertion is now in there.

    test_spec_on_rails is now on Git so unless you’re running Edge Rails you can’t use script/install to grab it. My suggestion is to clone from Git into vendor/plugins and then svn:ignore the .git file:

    git clone git://github.com/pelargir/test_spec_on_rails.git vendor/plugins/test_spec_on_rails
    svn propset svn:ignore .git vendor/plugins/test_spec_on_rails
    
  • Fixed bid vs. time and materials

    “It’s unwise to pay too much… but it’s worse to pay too little. When you pay too much, you lose a little money… that is all. When you pay too little, you sometimes lose everything, because the thing you bought was incapable of doing the thing it was bought to do. The common law of business balance prohibits paying a little and getting a lot…it can’t be done. If you deal with the lowest bidder it is well to add something for the risk you gain. And if you do that, you will have enough to pay for something better.” — John Ruskin 1819-1890

  • Add layout checking to test_spec_on_rails

    test_spec_on_rails is a plugin that adds Rails-specific assertions to test_spec, allowing you to do nifty things like this:

    it "should render with foo template" do
      get :some_action
      template.should.be "foo"
      # equivalent to assert_equal "foo", @response.template
    end
    
    it "should display foo on page" do
      get :some_action
      page.should.select "p", "foo"
      # equivalent to assert_select "p", "foo"  
    end
    

    One thing test_spec_on_rails is missing, though, is the ability to check the layout that a template renders with. For example, it would be nice to do this:

    it "should render with foo layout" do
      get :some_action
      layout.should.be "foo"
      # equivalent to assert_equal "foo", @response.layout
    end
    

    I’ve submitted a patch to Rick Olson that adds this capability. I’m not sure if or when it will get incorporated into the plugin though, so I’m posting the patch here for anyone who might need it.

    To apply, right-click on the link and download the file into your vendor/plugins/test_spec_on_rails folder, then run this command from inside the same folder:

    patch -p0 < add_layout.diff
    

    Assuming your plugin isn't checked out as an external, you'll be able to commit the changes to your own repository. You can then start using the new hotness as described above. Enjoy!

    Update: Rick made me a committer so I was able to add this myself.