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


  • Time zone warp

    One of my Rails projects makes heavy use of time zones. I’ve run into some issues writing good tests for this type of thing. In particular, I’ve needed my tests to run within a time zone outside my own. But I don’t want to permanently change the time zone within the scope of the entire test run. I ended up coding this handler:

    module ZoneWarp
      def pretend_zone_is(zone)
        original_zone = Time.zone
        begin
          Time.zone = zone
          yield
        ensure
          Time.zone = original_zone
        end
      end
    end
    
    Test::Unit::TestCase.send(:include, ZoneWarp)
    

    Simply stick this code in a file inside your config/initializers directory (or include it from test_helper.rb or spec_helper.rb if you insist on doing it the right way) and you’re all set to write tests like this:

    test "code works in other time zones" do
      pretend_zone_is "Mountain Time (US & Canada)" do
        # assertions go here
      end
    end
    
  • Secrets of Effective Nomading

    “Secrets of Effective Nomading” was a lightning talk I was planning on giving at this year’s Ruby Hoedown. Due to my flight arrangements, however, I was unable to give the talk in person. I made a video recording instead. It’s about six minutes long.

    If you’re not familiar with the concepts behind nomadic programming, you might want to read my introduction before watching the video.

    By the way, the Hoedown itself was fantastic. This is the third year Jeremy has run the conference and the presentations were top-notch. By far the best yet. The venue (the Opryland in Nashville) was also quite pleasant, if a little on the expensive side. The expense was worth it, though, if only to hear Jim Weirich present on a source control system that sounded a lot like Git, but wasn’t!

    And of course, the nearby mall complete with movie theater and Bass Pro Shop was just the icing on the cake. (C’mon Raleigh, when are we gonna get a Bass Pro Shop? Charlotte has one.)

  • Learn about Prawn at raleigh.rb on August 18th

    I’ll be giving a presentation about Prawn at this month’s raleigh.rb meetup. Prawn is a Ruby gem that enables fast PDF generation. It is a dramatic improvement over previous libraries like PDF::Writer. It can be used standalone or inside your Rails applications. The markup is powerful and relatively painless to use. I hope you can join us for the fun on August 18th at Red Hat HQ.

  • BarCampRDU was a rousing success

    BarCampRDU 2009 took place last weekend, and was a rousing success (as usual). I heard several attendees comment that the presentations outdid last year’s camp.

    One of the highlights for me was attending Nathaniel’s talk about startups. It was a longer version of the talk he gave at Ignite which was perfect for me since I was unable to attend Ignite. I was also intrigued by a round-table discussion about developing applications for the Palm Pre. The primary reason I haven’t switched to the iPhone yet is because I adore Sprint’s phenomenal coverage area. I’m not willing to “downgrade” to AT&T at this point. The Pre looks like it could be a nice alternative. The jury is still out though, because Curtis mentioned in a hallway conversation that Sprint is planning on introducing a phone that runs Android later this year. Hmm, decisions, decisions.

    I also really enjoyed catching up with some folks I hadn’t seen in a while. Who says geeks aren’t social? By the end of the day I was pretty worn out from all the “socializing” being done. I made sure to get my own presentation in early though, which was a change from last year. I pitched a talk about nomadic programming and jumped on the first time slot in room E, a favorite. The turnout was much higher than expected. I enjoyed sharing some of my own experiences with nomading and hearing what other people had been up to.

    If you’re not familiar with nomading, my previous post on nomadic programming is a great introduction to the concept. Essentially, it’s the idea that getting out of the house or the office and to a coffee shop or co-working location has numerous benefits, including enhancing your productivity and providing fresh networking opportunities. I shared a few of my favorite nomading locations, including The Wake Zone. I also ran through the inventory of my “go-bag:”

    • Power strip/block w/surge protection
    • Wireless mouse (Bluetooth is ideal)
    • Headset w/boom mic (for Skype and music)
    • EVDO connectivity (Millenicom is my recommendation)
    • Laptop (preferably a Mac!)
    • Cables for connecting to an external monitor
    • Water bottle (keepin’ it cool)

    I do plan on writing a follow-up to my original post about nomadic programming. Expect to see something later in September. Something I think we all should do going forward is communicate our nomading plans via Twitter. Make sure to use the #twitter hashtag when you do. Twitter has been the best way for me to coordinate co-working trips with fellow geeks. Chris has said that there really should be an app that makes this easier. Hmm, interesting idea! Anyone want to run with it?

    By the way, make sure you’re following me on Twitter. C’mon, you know you want to.

    In conclusion, I continue to find BarCamp to be, far from a waste of time, a great way to reconnect with a group of geeks I don’t see very often, and also an invaluable educational resource. I always pick up something new when I attend. And who can resist free T-shirts and food? If you’ve never been to BarCamp before, you should definitely consider subscribing to the blog and attending in 2010. You won’t regret it.