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


  • 20.weeks.ago

    Another example why Ruby is generally the best choice when you want to get something up and running fast with a minimum amount of code and a maximum amount of clarity.

    Dion Almaer’s 20.weeks.ago vs. java.util.Calendar

  • Watir: Web Application Testing in Ruby

    WATIR stands for “Web Application Testing in Ruby”. Watir is a free, open-source functional testing tool for automating browser-based tests of web applications. It is pronounced water.

    I began experimenting with Watir over the weekend. I run a database-driven web site for a local non-profit group and have been looking for a good way to functionally test the site with a minimum time investment in a particular framework. I’ve had bad experiences with HtmlUnit and HttpUnit in the past and wanted a way to code the tests in Ruby. I’ve been told that the state of Watir a year ago was really awful, but apparently they’ve made lots of improvements since then because I didn’t have any trouble getting up and running.

    As a test writer, most of your interaction with Watir takes placed through the IE object which lets you open a specific URL and then make assertions about what the page should look like. Watir’s flexibility in looking up data on the page is phenomenal. You can do lookups by name, ID, position, and so on. The pages I’ve tested thus far contain light JavaScript so I’m not sure how well Watir performs on pages with heavy scripting, but for my purposes Watir made it quick and easy to write tests that didn’t contain an enourmous amount of code, yet were powerful enough to thoroughly verify the functionality of my web app.

    Couple Watir with ActiveRecord and you have a powerful way to verify that data on the backend is getting written and updated correctly from your web application.

    I highly recommend you check out Watir for your next project. If you end up using it, feel free to share your thoughts about it by posting a comment here or e-mailing me directly. I’d enjoy hearing from you.

  • An incredibly geeky display of Christmas lighting prowess

    Someone just sent me a link to this video. How amazingly geeky. Yet another case where I’m wondering why people like this have so much extra time on their hands. There has to be an explanation!

  • Sails: Simplifying Java web apps

    Sails: a dynamic, open-source web framework for Java designed in the spirit of Rails.

    The top 5 reasons to use it?

    1. Created using test-driven development (no, seriously, it was!)
    2. Controllers are testable (oh yeah)
    3. Leans heavily on convention over configuration (Rails anyone?)
    4. Binds URLs to Actions on Controllers and renders their templates (sure wish Struts would do this sans XML)
    5. Converts objects into Strings and Strings into objects (sure, Struts does this, but Sails does it better)

    Born out of a software project some of my old co-workers from RoleModel Software are currently working on, this framework appears to be quite slick, but is still in its infancy. My experience with it so far has been limited, but I like what I see. I encourage you to at least poke around the Sails web site and read up on the framework.