Wednesday, April 06, 2011

Making Jammit jam with Heroku

So, you want to use Heroku and Jammit together, and you've come here of all places! I know, there's no good solution for this floating around The Interwebs, so I figured one out myself, and now I'm publishing so that you, YOU THE VIEWER, can be both shocked and amazed.

There's a nicely formatted gist right here, or you can read it inline below:


unless Rails.env.development?
require 'fileutils'
output_folder = Rails.root.join("tmp", "jammit", "assets")
FileUtils.mkdir_p(output_folder)
# Heroku specific hack.
Jammit::Packager.class_eval do
def glob_files_with_heroku_hack(glob)
paths = glob_files_without_heroku_hack(glob)
# Heroku has a magical "public/javascripts/all.js" file. It is a Mystery.
paths.reject { |path| path =~ /javascripts\/all.js$/ }
end
alias_method_chain :glob_files, :heroku_hack
end
Jammit.package! :output_folder => output_folder
Rails.configuration.middleware.insert_before('Rack::Sendfile', 'Rack::Static',
:urls => ['/assets'],
:root => "#{Rails.root}/tmp/jammit")
end
view raw jammit.rb hosted with ❤ by GitHub



Yes, that large hack floating around in the middle is specifically for Heroku. If you're on another cloud service that is a little less wizardly, you can drop the class_eval block entirely and you should be good to go.

If you're wondering if the resultant files will be cached by Heroku's Varnish HTTP accelerator: yes. Rack::Static will return the fetched files as Rack::File objects, which Heroku caches.



UPDATE: if it's not obvious, this needs to go into config/initializers -- so it runs once on app start up.

No comments: