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


  • Embedding Google Charts in FXRuby

    Lyle Johnson just wrote a great tutorial on how to integrate Google Charts into an FXRuby application. This is perfect timing since I’ve been doing some experimenting with FXRuby over the past few days.

  • Exploring Google Charts with the gchartrb gem

    Google quietly released their new chart generation web service late last year. It’s a dead simple way to build beautiful charts using nothing but URLs. The gchartrb gem wraps Google’s API to make things even easier. InfoQ recently published this article that I wrote. It introduces the basics of the API and gives some examples of how to use gchartrb to create beautiful charts.

  • Installing FXRuby on OS X

    Thought I’d do some hacking on FXRuby today. I ran into trouble during the installation so I’m posting the solutions here in case anyone else experiences the same issues.

    I attempted installing the fxruby gem first, but didn’t get very far. I ended up having success with the rb-fxruby package for MacPorts so I highly recommend going that route.

    Make sure you uninstall and clean the gem if it’s already installed:

    gem uninstall fxruby
    gem clean fxruby
    

    Now install the MacPorts package:

    port install rb-fxruby
    

    This might take a while. You may have to run the command twice if you get a failure the first time through. Now copy and paste this script into a .rb file and try running it:

    require 'rubygems'
    require 'fox16'
    
    include Fox
    
    app = FXApp.new
    win = FXMainWindow.new(app, 'Hello World')
    
    app.create
    win.show
    
    app.run
    

    You should see an error message that looks something like this:

    FXRbApp::openDisplay: unable to open display :0.0
    

    It means your X11 terminal isn’t running. You can find it in /Applications/Utilities. Double click to launch. If you roll with Quicksilver, you should be able to type in X11 and launch it that way. Once launched, run the Ruby script again and you should see a tiny window with the title “Hello World.”

  • Brightkite is fun

    I’m really enjoying using Brightkite. It’s a location-based social networking tool that lets you see what other people in your vicinity are doing. A GPS unit isn’t required and it integrates fairly well with Twitter and GMaps. (I’d love to see even tighter Twitter integration in the future.) Drop a comment on this post if you want an invite.