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


  • Bill Gates wants to annoy the whole world with Windows

    Yes, it’s true. Bill Gates wants to annoy the whole world with Windows.

  • “Imagine for a moment that someone has established for you a very unusual bank account. Each morning he has agreed to deposit $86,400. At then at the end of each day whatever you have not spent will be forfeited. Would you not be diligent to see to it that every penny of that account was withdrawn and properly spent? Would you allow any of it to expire unredeemed? You have such an account. For God has given you 86,400 precious and irreplaceable seconds in this day. Whatever is not used will expire and can never be reclaimed. Seize each moment therefore and invest it wisely.” — Rick Grubbs

  • Fix for Streamlined menu rendering in Rails 2

    I noticed an interesting side effect of the default behavior of Rails today. A fresh Rails 2.0.2 app includes a line in app/controllers/application.rb that looks like this:

    helper :all # include all helpers, all the time
    

    It looks innocuous enough, but this line can actually cause some unexpected behavior when overriding Streamlined’s streamlined_top_menu and streamlined_side_menu helper methods on a controller-by-controller basis.

    I had two Streamlined controllers setup, one for users and one for roles. I defined the users side menu and the roles side menu to each have certain links, like so:

    # users_helper.rb
    def streamlined_side_menu
       # stuff here
    end
    
    # roles_helper.rb
    def streamlined_side_menu
      # different stuff here
    end
    

    When I visited the users controller, I saw the menu contents for the users controller. When I visited the roles controller, however, I saw the menu contents for… the users controller. What was happening was that ALL the helpers were being loaded for each controller, which meant that the streamlined_side_menu method defined in the roles helper got overridden by the users helper since the users helper was loaded after the roles helper.

    Removing helper: all fixed the problem for me.

  • And you thought homeschooling was tough?

    These people need to seriously rethink their approach to preschool. Why not just teach the kid at home for a year instead of freezing your tail off in line?