强制rails创建资源缓存文件

发布于 2024-07-23 23:18:10 字数 220 浏览 5 评论 0原文

我在 s3 上托管我的资产。 在生产中,rails 正在寻找 /javascripts/cache/all.js 和 /stylesheets/cache/all.css。 当我使用 cap 进行部署时,我正在使用一个插件将公共目录转移到 s3。 问题是,在第一次请求这些缓存文件之前,rails 不会创建这些文件,因此当我传输公共目录时,它们在部署期间并不存在。 有没有一种简单的方法可以在部署期间强制创建这些文件?

I am hosting my assets on s3. In production, rails is looking for /javascripts/cache/all.js and /stylesheets/cache/all.css. I'm using a plugin to swoop the public directory over to s3 when I deploy with cap. The problem is that rails doesn't create these cache files until they are requested for the first time, so they aren't around during deployment when I transfer the public dir. Is there an easy way to force the creation of these files during deployment?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鹿港小镇 2024-07-30 23:18:10

我在 rake 任务文件中使用它。 使您无需创建额外的类,并且可以保持干燥,因为您使用的是 stylesheet_link_tag / javascript_include_tag 中的值

desc "Clears javascripts/cache and stylesheets/cache"   task :clear => :environment do
  puts "Clearing javascripts/cache and stylesheets/cache"
  FileUtils.rm(Dir['public/javascripts/cache_[^.]*']) # use :cache => 'cache_all.js' in stylesheet_link_tag
  FileUtils.rm(Dir['public/stylesheets/cache_[^.]*']) # use :cache => 'cache_all.css' in javascript_include_tag
end

desc "Recreate the javascripts/stylesheets cache."
  task :generate => [:environment, :clear] do
  puts "Recreate the javascripts/stylesheets cache"
  ActionController::Base.perform_caching = true
  app = ActionController::Integration::Session.new
  app.get '/'
end

I use this in a rake task file. Saves you from creating an additional class and you keep it DRY because you're using the values from the stylesheet_link_tag / javascript_include_tag

desc "Clears javascripts/cache and stylesheets/cache"   task :clear => :environment do
  puts "Clearing javascripts/cache and stylesheets/cache"
  FileUtils.rm(Dir['public/javascripts/cache_[^.]*']) # use :cache => 'cache_all.js' in stylesheet_link_tag
  FileUtils.rm(Dir['public/stylesheets/cache_[^.]*']) # use :cache => 'cache_all.css' in javascript_include_tag
end

desc "Recreate the javascripts/stylesheets cache."
  task :generate => [:environment, :clear] do
  puts "Recreate the javascripts/stylesheets cache"
  ActionController::Base.perform_caching = true
  app = ActionController::Integration::Session.new
  app.get '/'
end
纸短情长 2024-07-30 23:18:10

如果您使用的是 Engine Yard Cloud,这就是我所做的:

添加文件 /lib/tasks/assetcache.rake

    require 'assetwriter'

    namespace :assetcache do

      desc "Clears javascripts/cache and stylesheets/cache"   
      task :clear => :environment do
        puts "Clearing javascripts/cache and stylesheets/cache"
        FileUtils.rm(Dir['public/javascripts/cache_[^.]*'])
# use :cache => 'cache_all.js' in stylesheet_link_tag
        FileUtils.rm(Dir['public/stylesheets/cache_[^.]*'])
# use :cache => 'cache_all.css' in javascript_include_tag
      end

      desc "Recreate the javascripts/stylesheets cache."
      task :generate => [:environment, :clear] do
        puts "Recreate the javascripts/stylesheets cache"
        AssetCacheWriter.new.write
      end

    end

然后添加 /lib/assetwriter.rb

require 'action_view'
类 AssetCacheWriter

包括 ActionView::Helpers::AssetTagHelper

def write
write_asset_file_contents(File.join(JAVASCRIPTS_DIR, "cache/all.js"),compute_javascript_paths([:all], true))
write_asset_file_contents(File.join(STYLESHEETS_DIR, "cache/all.css"),compute_stylesheet_paths([:all], true))
end

end

然后我添加到 /deploy/after_restart.rb

run "cd #{release_path} && bundle exec rake assetcache:generate"

If you're using Engine Yard Cloud, this is what I did:

added a file /lib/tasks/assetcache.rake

    require 'assetwriter'

    namespace :assetcache do

      desc "Clears javascripts/cache and stylesheets/cache"   
      task :clear => :environment do
        puts "Clearing javascripts/cache and stylesheets/cache"
        FileUtils.rm(Dir['public/javascripts/cache_[^.]*'])
# use :cache => 'cache_all.js' in stylesheet_link_tag
        FileUtils.rm(Dir['public/stylesheets/cache_[^.]*'])
# use :cache => 'cache_all.css' in javascript_include_tag
      end

      desc "Recreate the javascripts/stylesheets cache."
      task :generate => [:environment, :clear] do
        puts "Recreate the javascripts/stylesheets cache"
        AssetCacheWriter.new.write
      end

    end

then I added /lib/assetwriter.rb

require 'action_view'
class AssetCacheWriter

include ActionView::Helpers::AssetTagHelper

def write
write_asset_file_contents(File.join(JAVASCRIPTS_DIR, "cache/all.js"), compute_javascript_paths([:all], true))
write_asset_file_contents(File.join(STYLESHEETS_DIR, "cache/all.css"), compute_stylesheet_paths([:all], true))
end

end

then I added to /deploy/after_restart.rb

run "cd #{release_path} && bundle exec rake assetcache:generate"

梦幻的心爱 2024-07-30 23:18:10

我发现包含顺序很重要(例如,jQuery 在其他脚本之前包含),因此使用了以下方法。 还允许使用 :defaults。

首先lib/asset_pacakger:

require 'action_view'

class AssetCacheWriter
  include ActionView::Helpers::AssetTagHelper
  include ActionView::Helpers::TagHelper 

  def write
    ActionController::Base.perform_caching = true    
    stylesheet_link_tag *ApplicationController::STYLESHEET_INCLUDES
    javascript_include_tag *ApplicationController::JAVASCRIPT_INCLUDES
  end
end

然后是scripts/package_assets:

#!/usr/bin/env ruby

RAILS_ROOT = File.expand_path(File.dirname(__FILE__) + '/../')
Dir.chdir(RAILS_ROOT)

RAILS_ENV = ENV['RAILS_ENV'] || 'development'

puts "Bundling assets for environment #{RAILS_ENV}"

require File.join('config', 'environment') 

AssetCacheWriter.new.write

然后是cap任务:

desc "Package assets"
task :package_assets do
  %x[#{Dir.getwd}/script/package_assets] 
  %x[svn commit #{Dir.getwd}/public/javascripts/defaults.js #{Dir.getwd}/public/stylesheets/defaults.css -m "(capistrano) packaging assets to trunk"]
  logger.info "packaged assets to trunk"
end

I found that include order mattered (so, for example, jQuery is included before other scripts) and so used the below approach instead. Also allows for the use of :defaults.

First lib/asset_pacakger:

require 'action_view'

class AssetCacheWriter
  include ActionView::Helpers::AssetTagHelper
  include ActionView::Helpers::TagHelper 

  def write
    ActionController::Base.perform_caching = true    
    stylesheet_link_tag *ApplicationController::STYLESHEET_INCLUDES
    javascript_include_tag *ApplicationController::JAVASCRIPT_INCLUDES
  end
end

Then scripts/package_assets:

#!/usr/bin/env ruby

RAILS_ROOT = File.expand_path(File.dirname(__FILE__) + '/../')
Dir.chdir(RAILS_ROOT)

RAILS_ENV = ENV['RAILS_ENV'] || 'development'

puts "Bundling assets for environment #{RAILS_ENV}"

require File.join('config', 'environment') 

AssetCacheWriter.new.write

Then a cap task:

desc "Package assets"
task :package_assets do
  %x[#{Dir.getwd}/script/package_assets] 
  %x[svn commit #{Dir.getwd}/public/javascripts/defaults.js #{Dir.getwd}/public/stylesheets/defaults.css -m "(capistrano) packaging assets to trunk"]
  logger.info "packaged assets to trunk"
end
酸甜透明夹心 2024-07-30 23:18:10

在 Rails 3.0.7 中,上述方法似乎都不起作用。 我收到很多关于配置未定义的错误。

在亲自查看 Rails 代码后,我想出了这个对我有用的解决方案。

lib/tasks/cache_assets.rake

require 'asset_packager'

desc "cache assets"
task :cache_assets do

  puts "-----> Caching Assets"
  AssetCacheWriter.new.write
  puts "-----> Done!"

end

然后是 lib/asset_packager.rb

require 'action_view'

class AssetCacheWriter
  include ActionView::Helpers::AssetTagHelper
  include ActionView::Helpers::TagHelper 

  def config
    ApplicationController
  end

  def controller    
    ApplicationController
  end


  def write
    ActionController::Base.perform_caching = true    

    # You can replace these with your app's cached assets. They should look the same
    # as in your view templates.
    stylesheet_link_tag :vendor, :defaults, :cache => 'cached/all'
    javascript_include_tag :bd_defaults, :'vendor-head', :cache => 'cached/defaults_vendor'

  end

end

然后要执行它,只需运行 rake cache_assets

In Rails 3.0.7 none of the above approaches seemed to work. I got many errors about config being undefined.

After getting my hands dirty looking over the Rails code, I came up with this solution that is working for me.

lib/tasks/cache_assets.rake

require 'asset_packager'

desc "cache assets"
task :cache_assets do

  puts "-----> Caching Assets"
  AssetCacheWriter.new.write
  puts "-----> Done!"

end

And then lib/asset_packager.rb

require 'action_view'

class AssetCacheWriter
  include ActionView::Helpers::AssetTagHelper
  include ActionView::Helpers::TagHelper 

  def config
    ApplicationController
  end

  def controller    
    ApplicationController
  end


  def write
    ActionController::Base.perform_caching = true    

    # You can replace these with your app's cached assets. They should look the same
    # as in your view templates.
    stylesheet_link_tag :vendor, :defaults, :cache => 'cached/all'
    javascript_include_tag :bd_defaults, :'vendor-head', :cache => 'cached/defaults_vendor'

  end

end

Then to execute it, just run rake cache_assets

流年已逝 2024-07-30 23:18:10

创建缓存文件的实际 Rails 模块是 ActionView::Helpers::AssetTagHelper,您可以在部署期间重用它来创建缓存文件。 以下对我来说效果很好:

require 'action_view'
class AssetCacheWriter

  include ActionView::Helpers::AssetTagHelper

  def write
    write_asset_file_contents(File.join(JAVASCRIPTS_DIR, "all.js"), compute_javascript_paths([:all], true))
    write_asset_file_contents(File.join(STYLESHEETS_DIR, "all.css"), compute_stylesheet_paths([:all], true))
'standard_all')
  end

end

这两个 write_asset_file_contents 调用都是我从 Rails 代码中提取的。 _DIR 常量在包含的模块中定义,compute_xxx_paths 调用中的 true 参数指示应递归包含所有文件。

我创建了一个简单的 cap 任务,只写出文件:

namespace :sample
  task :assets do
    AssetCacheWriter.new.write
  end
end

The actual rails module that creates the cache files is ActionView::Helpers::AssetTagHelper which you may be able to re-use to create the cache files during deployment. The following worked okay for me:

require 'action_view'
class AssetCacheWriter

  include ActionView::Helpers::AssetTagHelper

  def write
    write_asset_file_contents(File.join(JAVASCRIPTS_DIR, "all.js"), compute_javascript_paths([:all], true))
    write_asset_file_contents(File.join(STYLESHEETS_DIR, "all.css"), compute_stylesheet_paths([:all], true))
'standard_all')
  end

end

Both those write_asset_file_contents calls I've lifted from the rails code. The _DIR constants are defined in the included module and the true parameters in the compute_xxx_paths calls indicate that all files should be included recursively.

I created a simple cap task that just wrote the files out:

namespace :sample
  task :assets do
    AssetCacheWriter.new.write
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文