【问题标题】:missing gzip version of css and js assets缺少 gzip 版本的 css 和 js 资产
【发布时间】:2015-04-17 13:19:06
【问题描述】:

我将 Rails 4.2 用于一个非常简单的项目。当我运行 rake assets:precompile(用于开发和生产环境)时,我在 public/assets 中得到一个 application-xyz.jsapplication-xyz.css 文件。但是不会创建 gzip 版本,即没有 application-xyz.js.gzapplication-xyz.css.gz。我不知道禁用此功能的任何选项。我错过了什么吗?

【问题讨论】:

  • 您使用的是什么版本的链轮(检查 Gemfile.lock)?
  • 只是rails 4.2.1的依赖:sprockets 3.0.1, sprockets-rails 2.2.4

标签: ruby-on-rails asset-pipeline


【解决方案1】:

Sprockets 3 不再生成压缩版本的资产。根据this issue 的说法,很大程度上是因为它们很少被实际使用。

您可以通过在预编译后自己 gzip 压缩资产来恢复此功能,例如 Xavier Noria 的 example capistrano task 使用 find 迭代资产文件夹中的所有 css 和 js 文件,然后使用 xargs 传递他们给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

【讨论】:

    【解决方案2】:

    我更喜欢

    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

    【讨论】:

      【解决方案3】:

      从 Sprockets 3.5.2 开始,再次启用 gzip 压缩并生成 gz 资产。您必须配置您的服务器才能正确地为它们提供服务。对于 Nginx:

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

      然后在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)
      

      【讨论】:

      • 这对我有用。值得注意的是,请确保在重新加载之前至少在 Chrome 开发工具中“禁用缓存”一次以使更改生效并看到 gzip 显示在 Content-Encoding 列中。
      猜你喜欢
      • 2013-07-24
      • 2023-03-05
      • 2019-01-12
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-25
      相关资源
      最近更新 更多