Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails + Heroku + Jammit

I'm working to install Jammit on my Rails 3 app and then to deploy to Heroku.

I installed the Jammit Gem, and configured assets.yml just fine, it works on dev. But when I pushed to heroku, the files were 404'ing.

Jammit's Usage instructions say: "You can easily use Jammit within your Rakefile, and other scripts:

require 'jammit'
Jammit.package!

I'm not following where/how that works. Running Jammit in my sites command like on the Mac yields a command not found.

Any Jammit users able to help me understand how to move to production with Jammit?

Thanks

like image 259
AnApprentice Avatar asked Feb 04 '23 00:02

AnApprentice


2 Answers

I'm using jammit on a Rails 3.0.7 app, on Heroku

gem "jammit", :git => "git://github.com/documentcloud/jammit.git"

I have this in a rake file, to package up the assets before I commit/deploy

desc 'jammit'
  task :jam  => :environment do
  require 'jammit'
  Jammit.package!
end

And this in .git/hooks/pre-commit so it is done automatically

echo "jamming it"
rake jam
git add public/assets/*
git add public/javascripts/*

By default, the expires time on Heroku was only 12hrs, to increase it (because I have a cache-busting scheme that I am confident in) I put this in config/initializers/heroku.rb

module Heroku
  class StaticAssetsMiddleware
    def cache_static_asset(reply)
      return reply unless can_cache?(reply)
      status, headers, response = reply        
      headers["Expires"] = CGI.rfc1123_date(11.months.from_now)
      build_new_reply(status, headers, response)
    end
  end
end

To decrease the load on my Heroku Rails server, I am also using a free account at CloudFlare which provides a lightweight, reverse-proxy/cdn, with some decent analytics and security features.

When I get around to caching common content, this thing is really gonna scream!

like image 191
J_McCaffrey Avatar answered Feb 05 '23 12:02

J_McCaffrey


You could, as I do, use jammit force to pack your assets, upload everything to s3 and define an asset host(s) in rails. This has the added advantage of keeping your slug smaller and more responsive as you can add your public directory to .slugignore .

Alternatively you'll need to work out how to make the heroku version work due to the read only file system.

like image 21
mark Avatar answered Feb 05 '23 14:02

mark