Best way to combine and minify JS / CSS on Heroku
Asked Answered
L

8

29

First of all, according to this answer, the :cache => true option on stylesheet_link_tag and javascript_include_tag doesn't work on Heroku. Is this true? I've found :cache => true to work occasionally, but not always (weird!)

Also, what's the best solution here? Ideally it would seamlessly combine and minify all CSS / JS. Heroku Asset Packager claims to do this -- are there better options?

Loraleeloralie answered 11/12, 2009 at 18:4 Comment(0)
D
23

I'm using Jammit on Heroku. Works Great. You can locally build your assets and check in to heroku. use

jammit --force

the current version 0.5.1 has issues working on heroku but you can install the fixed version from git://github.com/documentcloud/jammit.git

If you are using Rails 3, specify the below in your bundler Gemfile:

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

For Rails 2.*

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

Good Luck.

Danger answered 11/12, 2009 at 18:4 Comment(0)
S
8

I've found that adding a git pre–commit hook which compiles and packs assets, then adds them to the current commit comes in handy in this case.

Mine using Jammit looks something like this (in .git/hooks/pre-commit):

jammit
rake barista:brew
git add public/assets/*
git add public/javascripts/*

Like this all your assets will be packed for you and you don't have to worry anymore about it.

Stipend answered 11/4, 2011 at 17:30 Comment(0)
R
4

GitHub has a good answer for this, and I'm sure you could modify Heroku's deployment scripts to integrate:

http://github.com/blog/551-optimizing-asset-bundling-and-serving-with-rails

Recidivism answered 6/2, 2010 at 19:49 Comment(1)
The Heroku deployment script is git push heroku. Not much to modify.Countrydance
R
3

I haven't tried it on heroku yet, but Sprockets might be good for that. Also, in the past, I've had more luck with

:cache => 'all.css'
:cache => 'all.js'

instead of 'true'

Ruthanneruthe answered 21/1, 2010 at 3:51 Comment(1)
Make sure you check out github.com/jeffrydegrande/sprockets_on_heroku if you want to use Sprockets. Sprockets is definitely something to watch, as it will be bundled by default in Rails 3.1 (not sure if it's in Rails 3 yet).Shavon
R
1

It's a different way to manage your CSS/Javascript but you may want to check out the Rails plugin shoebox.

Shoebox can do combining, minifying, and caching.

Rubberize answered 21/1, 2010 at 19:0 Comment(0)
T
0

The project name says it all:

http://github.com/amasses/heroku_asset_packager

Truss answered 10/2, 2010 at 1:25 Comment(2)
But this puts packaged assets into /tmp, which, according to docs.heroku.com/constraints, won't necessarily stick around across requestsLoraleeloralie
Have you tried Jammit? documentcloud.github.com/jammit I've read several commits from other public projects that have switched to this though I haven't tried myself. I'll have to test that out myself one day soon... don't opt for pre-compilation, have the gem compile the assets, and then clear out any files from underneath it, and see if it recompiles the assets again. I haven't read Jammit's source to confirm but it's worth a shot unless you've found something else.Truss
G
0

Here are the config options to compress your assets.

http://guides.rubyonrails.org/asset_pipeline.html#customizing-the-pipeline

config.assets.css_compressor = :yui
config.assets.js_compressor = :uglifier
config.assets.compress = true


gem 'uglifier'
gem 'yui-compressor'
Gagman answered 6/3, 2014 at 5:11 Comment(0)
P
0

There are probably various ways to do this, but what works for me is to minify before pushing. Then I use a subtree to keep my build files separate from the "source" files. So, for example, if you build to a folder called "dist", you can push to a subtree called heroku/master like this:

git subtree push --prefix dist heroku master

Just don't forget to ensure that the dist folder is not ignored (it often is, by default) - so edit your .gitignore file accordingly.

The --prefix command ensures that the dist folder effectively becomes the "root" folder from the point of view of that branch.

Polypus answered 5/9, 2015 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.