• 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: Wendy Kay Donnahoo

    “Motherhood will never be an Olympic event: they don’t feature extreme sports.” — Wendy Kay Donnahoo

  • The Torn-Up Credit Card Application

    A visual and textual warning to those of us who toss our junk mail out without properly mangling it.

  • Quote of the Week: Thomas Paine

    “Better fare hard with good men than feast it with bad.” — Thomas Paine

  • Krugle: The Search Engine for Software Developers

    I received my access code for the Krugle beta by e-mail last night. I immediately tried it out, of course. Here are my initial impressions.

    The goal of Krugle is to make it easy for software developers to search for technical articles and code samples. Let’s face it: Google is a fantastic search engine, but it’s sometimes difficult to locate good code samples with it. Krugle changes all of that by allowing visitors to search specifically for source code, not just across web pages, but within project files from sites like Sourceforge as well.

    After executing a code search, two panes open up on your screen. The left pane contains the source code you selected. The right pane shows other source code files in the same project. As you open new files, Krugle places them in tabs across the top of the page, very similar to Firefox. The difference is that Krugle uses Ajax goodness to drive its user-friendly interface. Searching with Krugle is a real treat.

    If you’re interested in trying Krugle for yourself, visit the web site to sign up for the beta. Alternately, you could just wait a few more months for the production version of the engine to be released. If Krugle proves as useful to other developers as it has to me, I would be expecting Google to aquire yet another search engine a few months after Krugle goes live.