16

我注意到我的资产似乎被编译了两次,这大大减慢了我的部署速度,因为这一步是最耗时的部分:

~/projects/rewportal(mapwidget ✔) rake assets:precompile
/home/ruy/.rvm/rubies/ruby-1.9.3-p194/bin/ruby /home/ruy/.rvm/gems/ruby-1.9.3-p194@rewportal/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
AssetSync: using /home/ruy/projects/rewportal/config/initializers/asset_sync.rb
AssetSync: using /home/ruy/projects/rewportal/config/initializers/asset_sync.rb
AssetSync: Syncing.
Using: Directory Search of /home/ruy/projects/rewportal/public/assets
Uploading: assets/application-5170f52c1dd49cb382d5135bee01d75e.js
[...]
Fetching files to flag for delete
Flagging 8 file(s) for deletion
Deleting: assets/active_admin-4ce46d089d4b0080e87c9abcb6fa6c97.css
[...]
AssetSync: Done.

这正常吗?

当我预编译到其他环境(非生产环境)时,我可以两次看到每个资产的详细编译:

~/projects/rewportal(mapwidget ✔) rake RAILS_ENV=qa assets:precompile --trace
** Invoke assets:precompile (first_time)
** Execute assets:precompile
/home/ruy/.rvm/rubies/ruby-1.9.3-p194/bin/ruby /home/ruy/.rvm/gems/ruby-1.9.3-p194@rewportal/bin/rake assets:precompile:all RAILS_ENV=qa RAILS_GROUPS=assets --trace
** Invoke assets:precompile:all (first_time)
** Execute assets:precompile:all
** Invoke assets:precompile:primary (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
AssetSync: using /home/ruy/projects/rewportal/config/initializers/asset_sync.rb
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:primary
Compiled gmaps4rails/gmaps4rails.base.js  (141ms)  (pid 8480)
Compiled gmaps4rails/gmaps4rails.googlemaps.js  (148ms)  (pid 8480)
[...]
Compiled active_admin.css  (1299ms)  (pid 8480)
Compiled active_admin/print.css  (113ms)  (pid 8480)
** Invoke assets:precompile:nondigest (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
AssetSync: using /home/ruy/projects/rewportal/config/initializers/asset_sync.rb
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:nondigest
Compiled gmaps4rails/gmaps4rails.base.js  (133ms)  (pid 8480)
Compiled gmaps4rails/gmaps4rails.googlemaps.js  (133ms)  (pid 8480)
[...]
Compiled active_admin.css  (1290ms)  (pid 8480)
Compiled active_admin/print.css  (116ms)  (pid 8480)
AssetSync: Syncing.
Using: Directory Search of /home/ruy/projects/rewportal/public/assets
Uploading: assets/active_admin-d05b61ab8366b74eabc9074d3e60fe82.css.gz
[...]
Fetching files to flag for delete
Flagging 6 file(s) for deletion
Deleting: assets/active_admin-ec90e7d9a9f45f14d1387f58fa1452b4.css
[...]
AssetSync: Done.

application.rb的有以下几点:

config.assets.precompile += %w( active_admin/print.css active_admin.css active_admin.js admin.js admin.css html5shiv.js )

想法?

4

4 回答 4

12

Rails 3 默认编译一次生成指纹资产,一次生成非指纹资产(指纹资产在文件名中有 MD5 哈希)。

您可以使用turbo-sprockets-rails3 gem从一个编译中创建两者。

在 Rails 4 中,此功能被提取到sprockets-rails gem中并且行为发生了变化,因此在 Rails 4 中不会发生双重编译。

于 2013-09-08T15:30:01.827 回答
1

你在用 capistrano 吗?如果是这样,您可以尝试在本地编译您的资产,然后上传到服务器,任务如下

namespace :deploy do
  namespace :assets do
    desc "Precompile assets on local machine and upload them to the server."
    task :precompile, roles: :web, except: {no_release: true} do
      run_locally "bundle exec rake assets:precompile"
      find_servers_for_task(current_task).each do |server|
        run_locally "rsync -vr --exclude='.DS_Store' public/assets #{user}@#{server.host}:#{shared_path}/"
      end
    end
  end
end
于 2013-07-30T14:16:17.650 回答
0

我的解决方案(Rails 3)是运行rake assets:precompile:primary而不是rake assets:precompile:all.

于 2014-03-29T11:45:41.140 回答
0

我覆盖了 deploy.rb 中的 deploy_all 任务以在上传之前进行编译:

task :remake_all do
  puts "precompiling assets"
  res = `env rake assets:precompile:all RAILS_ENV=precompile RAILS_GROUPS=assets 2>&1`

  $stderr.puts "assets res is: #{res}"
  if res =~ /aborted|don't|invalid|segmentation|bug/i
    puts "############  Unable to compile assets  #########" 
    exit
  end
end

我遇到了开发环境无法正确编译以及生产环境需要访问防火墙后面的 mysql 服务器的问题,因此创建了一个名为 :precompile 的新环境

于 2013-08-06T20:20:28.247 回答