代码之家  ›  专栏  ›  技术社区  ›  lesce

Rails 4.0.13资产url缺少前缀和摘要

  •  1
  • lesce  · 技术社区  · 10 年前

    我已经开始开发rails 4.0.13应用程序,我们正在尝试将资产转移到CDN

    问题出现在我们的登台服务器上,生成的url缺少前缀和摘要

    前任 : https://xxyyzz.blob.core.windows.net/stylesheets/application.css

    在我的本地计算机上,使用与登台服务器相同的环境,一切正常

    前任 : https://xxyyzz.blob.core.windows.net/c532aef6b12d99b3ce3f05b4fc17c02d8a682b13/application-dd1ee03f18e6ee4df65b6a21d8cfcdeb.css

    config/environments/dev_1.rb :

    ...
      config.cache_classes = true
      config.eager_load = true
      config.consider_all_requests_local       = false
      config.action_controller.perform_caching = true
      config.serve_static_assets = false
      config.assets.compile = false
      config.assets.digest = true
      config.assets.debug = false
      config.assets.version = '1.0'
      config.log_level = :info
      config.i18n.fallbacks = true
      config.active_support.deprecation = :notify
      config.log_tags = [CustomTagger::USER_TOKEN_LAMBDA, CustomTagger::PROCESS_PID]
      config.cache_store = :redis_store, RedisStores.new.url(db: 3), { expires_in: 1.day, driver: :hiredis }
    

    配置/应用程序.rb :

    ...
     config.assets.enabled = true
    
     config.assets.precompile = Proc.new do |filename, path|
       case
       # JS
       when /application-.+\.js/ =~ filename then true
       when filename == 'vendor.js' then true
       # CSS
       when filename.end_with?('master-default.css') then true
       when filename == 'application.css' then true
       # IMG
       when /assets\/images\// =~ path then true
       # Exclude everything else.
       else false
       end
     end
     config.asset_host = 'https://xxyyzz.blob.core.windows.net'
    
     # Each environment must have a different asset path (the SHA1 digest of
     # Rails.env + Rails.application.revision).
     #
     # See http://guides.rubyonrails.org/configuring.html#rails-general-configuration.
     # See https://github.com/rails/sprockets-rails/blob/master/lib/sprockets/railtie.rb#L174-L186
     config.after_initialize do
       ActiveSupport.on_load(:action_view) do
         self.assets_prefix = Digest::SHA1.hexdigest(Rails.env + Rails.application.revision)
       end unless Rails.env.development?
     end
     config.autoload_paths += %W(#{config.root}/lib)
     config.middleware.insert_before Rack::Sendfile, 'HttpMethodNotAllowed'
    

    还有人遇到过这种情况吗?

    1 回复  |  直到 10 年前
        1
  •  2
  •   Community Mohan Dere    8 年前

    找出问题所在 https://github.com/rails/rails/issues/15873

    似乎rails会检查文件是否存在于公共/资产中,如果不存在,则不会设置前缀&消化

    类似问题: https://serverfault.com/questions/638905/does-rails-4-asset-path-helper-uses-asset-prefix

    一个可能的解决方案是保留清单。repo中的json文件 或添加仅预编译清单文件的部署任务。 https://github.com/rails/sprockets-rails/issues/107

    推荐文章