缺少 css 和 js 资产的 gzip 版本
missing gzip version of css and js assets
我正在为一个非常简单的项目使用 Rails 4.2。当我 运行 rake assets:precompile
(用于开发和生产环境)时,我在 public/assets 中得到一个 application-xyz.js
和 application-xyz.css
文件。但是不会创建 gzip 版本,即没有 application-xyz.js.gz
和 application-xyz.css.gz
。我不知道有任何选项可以禁用此功能。我错过了什么吗?
Sprockets 3 不再生成资产的 gzip 版本。根据 this issue 的说法,这主要是因为它们很少实际使用。
您可以通过在预编译后自行 gzip 资产来恢复此功能,例如 Xavier Noria 的 example capistrano task 使用 find
遍历所有 css 和 js 文件assets 文件夹,然后使用 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
我比较喜欢
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
从 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)
我正在为一个非常简单的项目使用 Rails 4.2。当我 运行 rake assets:precompile
(用于开发和生产环境)时,我在 public/assets 中得到一个 application-xyz.js
和 application-xyz.css
文件。但是不会创建 gzip 版本,即没有 application-xyz.js.gz
和 application-xyz.css.gz
。我不知道有任何选项可以禁用此功能。我错过了什么吗?
Sprockets 3 不再生成资产的 gzip 版本。根据 this issue 的说法,这主要是因为它们很少实际使用。
您可以通过在预编译后自行 gzip 资产来恢复此功能,例如 Xavier Noria 的 example capistrano task 使用 find
遍历所有 css 和 js 文件assets 文件夹,然后使用 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
我比较喜欢
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
从 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)