0

用例:将我的 Rails 4.rc1 应用程序推送到 Heroku,进行资产预编译,然后使用 assets_sync gem 将它们放在 S3 上。我在配置中为 S3 设置了asset_host。

在此之后但在 slug 编译期间,我想将指纹资产 URL 转储到 Redis,以便其他 Heroku 应用程序可以看到它(它们共享相同的 Redis 数据库)并使用相同的资产文件。本质上:

//s3.amazonaws.com/my_bucket/assets/desktop-(fingerprint).css

显然,部署和其他应用程序之间的指纹变化需要更新的 URL。

这是我的第一次尝试:

module AssetShare
  class << self
    def capture_urls
      action_controller = ActionController::Base.new
      REDIS.set('desktop_css_url',action_controller.view_context.stylesheet_url('desktop'))
      REDIS.set('mobile_css_url',action_controller.view_context.stylesheet_url('mobile'))
    end
  end
end

desc 'Capture asset pipeline stylesheet and javascript URLS to Redis'
task 'assets:capture_urls' => :environment do
  AssetShare.capture_urls
end

# stolen from asset_sync
if Rake::Task.task_defined?("assets:precompile:nondigest")
  Rake::Task["assets:precompile:nondigest"].enhance do
    AssetShare.capture_urls
  end
else
  Rake::Task["assets:precompile"].enhance do
    AssetShare.capture_urls
  end
end

这倾倒了:

//s3.amazonaws.com/my_bucket/stylesheets/desktop.css

然后我找到了这个资源,但是 Rails 4 中的 Sprockets 发生了变化:http: //blog.noizeramp.com/2011/10/14/asset-urls-and-paths-in-rake-tasks/

这是我的第二次尝试:

desc 'Capture asset pipeline stylesheet and javascript URLS to Redis'
task 'assets:capture_urls' => :environment do
  MyApp::Application.configure do
    config.assets.debug = false
    config.assets.digest = true
  end

  include ActionView::Helpers::AssetTagHelper

  desktop_url = stylesheet_url('desktop', only_path: false)
  REDIS.set('desktop_css_url', desktop_url)
  puts "Saved desktop url to Redis for Store Rails app: #{desktop_url}"

  mobile_url = stylesheet_url('mobile', only_path: false)
  REDIS.set('mobile_css_url', mobile_url)
  puts "Saved mobile url to Redis for Store Rails app: #{mobile_url}"
end

# stolen from asset_sync
if Rake::Task.task_defined?("assets:precompile:nondigest")
  Rake::Task["assets:precompile:nondigest"].enhance do
    Rake::Task["assets:capture_urls"].invoke
  end
else
  Rake::Task["assets:precompile"].enhance do
    Rake::Task["assets:capture_urls"].invoke
  end
end

这只是倾倒了:

/stylesheets/desktop.css

显然第一次尝试它更接近。我只是无法让它转储指纹网址。如果我通过 heroku 工具包运行 rake 任务,则会打印出正确的指纹 URL。想法?

4

1 回答 1

0

最终,这是我必须采用的解决方案:

    desc 'Capture asset pipeline stylesheet and javascript URLS to Redis'
    task 'assets:urls:capture' => :environment do

      # unfortunately, this way doesn't work when deploying
      #controller = MyApp::Application::ApplicationController.new
      #desktop_url = controller.view_context.stylesheet_url('desktop')
      #mobile_url = controller.view_context.stylesheet_url('mobile')

      desktop_url = MyApp::Application.config.action_controller.asset_host
      desktop_url += "/" + AssetSync.storage.get_remote_files.select{|d| d.include?('desktop') && d.include?('.css') }.first
      REDIS.set('desktop_css_url', desktop_url)
      puts "Saved desktop url to Redis for Store Rails app: #{desktop_url}"

      mobile_url = MyApp::Application.config.action_controller.asset_host
      mobile_url += "/" + AssetSync.storage.get_remote_files.select{|d| d.include?('mobile') && d.include?('.css') }.first
      REDIS.set('mobile_css_url', mobile_url)
      puts "Saved mobile url to Redis for Store Rails app: #{mobile_url}"
    end

    desc 'Clear asset pipeline stylesheet and javascript URLS from Redis'
    task 'assets:urls:clear' => :environment do
      REDIS.del('desktop_css_url')
      puts "Cleared desktop url from Redis"
      REDIS.del('mobile_css_url')
      puts "Cleared mobile url from Redis"
    end

    # stolen from asset_sync
    if Rake::Task.task_defined?("assets:precompile:nondigest")
      Rake::Task["assets:precompile:nondigest"].enhance do
        Rake::Task["assets:urls:capture"].invoke
        #Rake::Task["assets:urls:clear"].invoke
      end
    else
      Rake::Task["assets:precompile"].enhance do
        Rake::Task["assets:urls:capture"].invoke
        #Rake::Task["assets:urls:clear"].invoke
      end
    end

在部署到 Heroku 期间,我永远无法获得正确的指纹 URL。不过,它说它作为生产环境运行。这给了我一个 Redis 中的 URL,例如:

    //s3.amazonaws.com/my_bucket/assets/desktop-6acd36e3d6c8d2b6e0fe58b83690687f.css
于 2013-06-05T22:36:33.500 回答