Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
gzip assets with Capistrano
# Compresses all .js and .css files under the assets path.
namespace :deploy do
# It is important that we execute this after :normalize_assets because
# ngx_http_gzip_static_module recommends that compressed and uncompressed
# variants have the same mtime. Note that gzip(1) sets the mtime of the
# compressed file after the original one automatically.
after :normalize_assets, :gzip_assets do
on release_roles(fetch(:assets_roles)) do
assets_path = release_path.join('public', fetch(:assets_prefix))
within assets_path do
execute :find, ". \\( -name '*.js' -o -name '*.css' \\) -exec test ! -e {}.gz \\; -print0 | xargs -r -P8 -0 gzip --keep --best --quiet"
end
end
end
end

for the best compression you must install 7z on your server

task :compress_assets_7z do
  on roles(:app) do
    assets_path = release_path.join('public', fetch(:assets_prefix))
    execute "find -L #{assets_path} \\( -name *.js -o -name *.css -o -name *.ico \\) -exec bash -c '[ ! -f {}.gz ] && 7z a -tgzip -mx=9 {}.gz {}' \\; "
  end
end

after 'deploy:normalize_assets', 'compress_assets_7z'

7z has optimized version of GZIP

The best compression can probably be gained with zopfli, but it is very slow.

Why only .ico ? Can this apply to all kind of images?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment