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


  • Quote of the Week: G.K. Chesterton

    “The Bible tells us to love our neighbors, and also to love our enemies; probably because they are generally the same people.” — G.K. Chesterton

  • RailsConf draws to a close

    It’s mid-afternoon here in Chicago and RailsConf has just officially ended. There is a huge line of people out front of the hotel waiting to catch the next shuttle to O’Hare. I’ll need a few more days to properly digest the conference and write a more detailed overview of what happened, but overall it was the most positive experience I’ve had at a conference outside of NFJS. Great speakers, late nights of hacking with friends, and geeky conversation. Well worth the time, money, and trouble to come.

  • RailsConf ho!

    Tomorrow I fly to Chicago for the biggest Ruby bash so far this year: RailsConf 2006! Aside from getting to hear some wonderful keynote speakers and hang out with a herd of fellow Rubyists, many of my buddies from the Raleigh area (and elsewhere) will also be in attendance. It’ll be great seeing familiar faces again, and meeting the owners of some new ones. If you’re planning on being at RailsConf too, consider posting a comment here so I’ll know to look for you.

    Chicagoans, brace yourselves. The geeks cometh.

  • NFJS 2006 wrap-up

    It was with mixed sadness and relief that NFJS 2006 came to a close last Sunday evening. Two and a half solid days of listening to some of the best technical speakers in the country can be draining, but the knowledge and enthusiasm one picks up is priceless.

    The highlights from day three were Andy Hunt’s “Refactoring Your Wetware” and “Pragmatic Learning” talks, along with Jared Richardson’s “Software Tools.” I had heard Andy’s wetware talk at the local .NET Users Group last year, but he has since revised and lengthened it, splitting it into two different NFJS talks.

    The first talk covered how the brain works (it’s a dual CPU, shared pipe architecture) and how developers can make incredible productivity gains by using more of their right brain instead of their left. In the second talk, Andy gave tips on how to learn more effectively.

    One point that struck home for me was the need to write all your ideas down the moment you get them. If you’re not writing your ideas down, you’ll start forgetting you have them. I’ve been using a plain old ballpoint pen (not all of us have enough cash kicking around to buy a Fisher Space Pen) and some index cards. They work quite well, and I get the added benefit of more right-brain activity. I wouldn’t otherwise have that if I were typing.

    Jared’s talk centered around CruiseControl, an open-source continuous integration tool. In a matter of minutes, he installed the tool on his laptop and configured it to build and test a couple of Java classes he had written. I was familiar with CruiseControl before attending his presentation, but the demo was still impressive. I’m sure if I were a newby attending his talk I would be eager to set up my own CI server the very next day.

    Jason posted a comment to my NFJS day 2 post requesting more information on Ramnivas’ demo of Selenium. Selenium is a JavaScript-based tool that can be used to generate and run cross-browser functional tests for web applications. Tests can be defined as HTML tables, Ruby scripts, and so on. Element locators are quite flexible, allowing widgets on the page to be looked up by ID, name, XPath, or directly through the DOM. Selenium also has a Firefox plugin that can be used to “record” interaction with a web app. This is a fast way to generate functional tests for an existing application.

    One tip Raminvas gave was to instruct testers or other people submitting bugs to include a Selenium recording with their report. That way, you as a developer can play back the exact steps they made to discover the bug. Handling of browser-specific code doesn’t seem to be a problem with Selenium. It’s certainly the most promising open source web application testing framework to be produced in a long time. Remember that it’s only at version 0.7 so it will continue evolving and (hopefully) improving over time.

    Next stop this month: RailsConf 2006! See you there.