缺少 css 和 js 资产的 gzip 版本 [英] missing gzip version of css and js assets

查看:31
本文介绍了缺少 css 和 js 资产的 gzip 版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

Sprockets 3 不再生成压缩版本的资产.根据这个问题,这主要是因为它们很少被实际使用.

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

您可以通过在预编译后自己压缩资源来恢复此功能,例如 Xavier Noria 使用的这个 示例 capistrano 任务find 遍历 assets 文件夹中的所有 css 和 js 文件,然后使用 xargs 将它们传递给 gzip:

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

这篇关于缺少 css 和 js 资产的 gzip 版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆