Best way to combine and minify JS / CSS on Heroku

2019-01-31 19:05发布

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?

8条回答
Summer. ? 凉城
2楼-- · 2019-01-31 19:07
The star\"
3楼-- · 2019-01-31 19:09

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'
查看更多
▲ chillily
4楼-- · 2019-01-31 19:16

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.

查看更多
淡お忘
5楼-- · 2019-01-31 19:17

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.

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-31 19:22

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.

查看更多
冷血范
7楼-- · 2019-01-31 19:25

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.

查看更多
登录 后发表回答