Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
801 views
in Technique[技术] by (71.8m points)

ruby on rails - missing gzip version of css and js assets

I'm using Rails 4.2 for a quite simple project. When I run rake assets:precompile (for development as well as production environments) I get an application-xyz.js and application-xyz.css file in public/assets. But there will be no gzip versions created, i.e. no application-xyz.js.gz and no application-xyz.css.gz. I'm not aware of any option to disable this feature. Did I miss anything?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Sprockets 3 no longer generates gzipped versions of assets. According to this issue it is largely because they were rarely actually used.

You can bring back this functionality by gzipping assets yourself after precompilation, for example this example capistrano task by Xavier Noria uses find to iterate over all the css and js files in your assets folder and then uses xargs to pass them to gzip:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...