Skip to content

Instantly share code, notes, and snippets.

@fxn
Last active July 2, 2019 18:14
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save fxn/427dca61ec44adf8253b to your computer and use it in GitHub Desktop.
Save fxn/427dca61ec44adf8253b to your computer and use it in GitHub Desktop.
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
@yuri-zubov
Copy link

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

@felixbuenemann
Copy link

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

@Chucheen
Copy link

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