Shawn Deprey
Shawn Deprey

Reputation: 525

How can I enable gzip file creation in Rails 4.2?

As of April 14, 2015 it looks like .gz file compilation was removed from sprockets in the latest version of Rails.

https://github.com/rails/sprockets/issues/26

I used these files on an S3 server to speed up page loads, but since the compilation of gzip files have been removed per the above thread there is a big question looming in my mind. If you are using an asset host, what is the new solution for having compiled .gz files? If I were serving the files from my server this would be relatively easy, but the static assets are hosted elsewhere and need precompilation.

Anyway, hopefully somebody has figured this one out. My temp solution if I can't get the asset pipeline to generate and upload .gz files like it once did is to manually create them using grunt-contrib-compress(https://github.com/gruntjs/grunt-contrib-compress). But as we all know manual solutions don't scale well and I would rather have the asset pipeline take care of this one at deploy time.

Thanks very much for the help. :)

Upvotes: 3

Views: 1189

Answers (1)

pierrebeitz
pierrebeitz

Reputation: 221

As Rake does not override but chains tasks, you could declare another assets:precompile. The following solution should work without changing any deployment-scripts:

#/lib/tasks/assets.rake
namespace :assets do
  # We make use of rake's behaviour and chain this after rails' assets:precompile.
  desc 'Gzip assets after rails has finished precompilation'
  task :precompile do
    Dir['public/assets/**/*.{js,css}'].each do |path|
      gz_path = "#{path}.gz"
      next if File.exist?(gz_path)

      Zlib::GzipWriter.open(gz_path) do |gz|
        gz.mtime = File.mtime(path)
        gz.orig_name = path
        gz.write(IO.binread(path))
      end
    end
  end
end

Upvotes: 1

Related Questions