Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 768
Thomas Preißler Avatar asked Apr 17 '15 13:04

Thomas Preißler


3 Answers

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
like image 141
Frederick Cheung Avatar answered Sep 23 '22 03:09

Frederick Cheung


I prefer

namespace :assets do
  desc "Create .gz versions of assets"
  task :gzip => :environment do
    zip_types = /\.(?:css|html|js|otf|svg|txt|xml)$/

    public_assets = File.join(
      Rails.root,
      "public",
      Rails.application.config.assets.prefix)

    Dir["#{public_assets}/**/*"].each do |f|
      next unless f =~ zip_types

      mtime = File.mtime(f)
      gz_file = "#{f}.gz"
      next if File.exist?(gz_file) && File.mtime(gz_file) >= mtime

      File.open(gz_file, "wb") do |dest|
        gz = Zlib::GzipWriter.new(dest, Zlib::BEST_COMPRESSION)
        gz.mtime = mtime.to_i
        IO.copy_stream(open(f), gz)
        gz.close
      end

      File.utime(mtime, mtime, gz_file)
    end
  end

  # Hook into existing assets:precompile task
  Rake::Task["assets:precompile"].enhance do
    Rake::Task["assets:gzip"].invoke
  end
end

Source

like image 28
Nishant Avatar answered Sep 24 '22 03:09

Nishant


As of Sprockets 3.5.2, gzip compression is enabled again and gz assets are generated. You do have to configure your server to serve them correctly. For Nginx:

location ~ ^/(assets)/ {
  gzip_static on;
}

Then in application.rb:

  config.middleware.insert_before(Rack::Sendfile, Rack::Deflater)

  # Compress JavaScripts and CSS.
  config.assets.compress = true
  config.assets.js_compressor = Uglifier.new(mangle: false)
like image 26
Snowman Avatar answered Sep 27 '22 03:09

Snowman