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


  • Where does TextMate store its bundles anyway?

    After spending nearly an hour tracking down where a particular TextMate bundle was coming from, I have surmised that there are no less than four places on my Mac’s hard drive where bundles may be lurking. In no particular order, they are:

    ~/Library/Application Support/TextMate/Pristine Copy/Bundles
    ~/Library/Application Support/TextMate/Bundles
    /Applications/TextMate.app/Contents/SharedSupport/Bundles
    /Library/Application Support/TextMate/Bundles
    
  • Sending mail from Rails through Google SMTP

    I just ran into a problem configuring a Rails app to deliver email through Google’s SMTP servers. ActionMailer doesn’t support TLS/SSL which is required by Google. Fortunately, the action_mailer_optional_tls plugin provides this functionality.

    I wanted to host my SMTP settings in an external YAML file so I wouldn’t end up checking my username and password into the repository. (The YAML file is placed on the production server and a softlink is created during each deploy.) For some reason, I kept getting “connection refused” messages whenever I tried to send email with this configuration:

    # smtp.yml
    production:
      address: smtp.gmail.com
      port: 587
      tls: true
      domain: foo.com
      authentication: :plain
      user_name: someone@foo.com
      password: secret
    
    # production.rb
    smtp = YAML::load(File.open("#{RAILS_ROOT}/config/smtp.yml"))
    ActionMailer::Base.smtp_settings = smtp[Rails.env]
    

    The problem was the hash returned by YAML. The keys were strings, whereas ActionMailer was expecting the keys to be symbols. The fix was to make the hash use indifferent access:

    ...
    ActionMailer::Base.smtp_settings = smtp[Rails.env].with_indifferent_access
    

    That cleared up the “connection refused” problem. Now my app is sending email like a champ.

  • Tweaked railspdf to support 2.1.0

    railspdf is a plugin that adds templating support for the pdf-writer gem to Rails. The project was last updated in January of 2007, but worked fine up until the 2.1.0 release of Rails which broke several things.

    I’ve forked the project on GitHub and tweaked it to run under 2.1.0. I also added support for custom helpers. Your PDF templates can now call methods in helpers with the same name as your controller (which is the expected behavior anyway). I’ve also started maintaining a CHANGELOG.

  • When hot corners stops working…

    OS X seems to get confused when I disconnect my MacBook from an external monitor. This confusion sometimes leads to the hot corners feature not working. This is definitely a bug in Leopard that needs fixing. Until that happens, restarting the Dock is a quick solution to the problem. This can be done through the Activity Monitor, but I think it’s easier to just run this script which I stole and modified:

    #!/bin/sh
    DOCKPID=`ps aucx| grep Dock | awk '{ print $2; }'`
    kill $DOCKPID