Rails 5.2资产助手,不包括指纹摘要

时间:2018-08-23 23:46:30

标签: ruby-on-rails asset-pipeline ruby-on-rails-5.2

我正在将我们的产品从Rails 4.1升级到5.2。我对似乎与资产管道有关的问题挂了电话。当我在浏览器中加载该应用程序时,我在服务器日志中看到以下错误,并且该应用程序缺少它通常具有的所有样式和javascript代码。

DEPRECATION WARNING: The asset application.js" is not present in the asset pipeline.Falling back to an asset that may be in the public folder.
This behavior is deprecated and will be removed.
To bypass the asset pipeline and preserve this behavior,
use the `skip_pipeline: true` option.

当我获取应用程序的index.html时,我发现所有图像和javascript url都缺少它们应具有的指纹摘要。有趣的是,他们确实拥有我们拥有的S3 asset_host,因此实际上它们正在由助手处理。

为了演示,我有两台服务器,它们的配置相同,但是一台服务器运行4.1,另一台服务器运行5.2。在这两者上,我都使用S3作为asset_host,并且摘要已打开。我已经在他们的控制台中运行了以下命令:

=== Rails 4 ===

Loading qatest environment (Rails 4.1.0)
irb(main):001:0> Rails.application.config.action_controller.asset_host
=> "https://redacted.s3.amazonaws.com"
irb(main):002:0> Rails.application.config.assets.digest
=> true
irb(main):003:0> ActionController::Base.helpers.asset_path('application.js')
=> "https://redacted.s3.amazonaws.com/assets/application-042f2014ca329c79c304ab1332557040d3f7b922247202f40c28acc950f30ef8.js"

=== Rails 5 ===
Loading sean environment (Rails 5.2.1)
irb(main):001:0> Rails.application.config.action_controller.asset_host
=> "https://redacted.s3.amazonaws.com"
irb(main):002:0> Rails.application.config.assets.digest
=> true
irb(main):003:0> ActionController::Base.helpers.asset_path('application.js')
=> "https://redacted.s3.amazonaws.com/application.js

根据要求,production.rb

require "#{File.dirname(__FILE__)}/includes/s3_assets"

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb

  config.eager_load = true

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = false

  config.log_level = ENV['SHOW_SQL'] != 'false' ? :debug : :info

  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_deliveries = true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    user_name: Rubber.config.email_username,
    password:  Rubber.config.email_password,
    address:   Rubber.config.email_server,
    port:      Rubber.config.email_port,
    enable_starttls_auto: true, # detects and uses STARTTLS
    authentication: 'login' # Mandrill supports 'plain' or 'login'
  }

  # Print deprecation notices to the Rails logger
  config.active_support.deprecation = :log

  # Only use best-standards-support built into browsers
  config.action_dispatch.best_standards_support = :builtin

  # Raise exception on mass assignment protection for Active Record models
  # config.active_record.mass_assignment_sanitizer = :strict

  # Log the query plan for queries taking more than this (works
  # with SQLite, MySQL, and PostgreSQL)
  # config.active_record.auto_explain_threshold_in_seconds = 0.5

  # Compress JavaScripts and CSS
  config.assets.compress = true

  # Don't fall back to assets pipelin if a precompiled asset is
  # missed.
  config.assets.compile = false

  # Generate digests for assets URLs
  config.assets.digest = true
  config.assets.enabled = true

  # Nginx will serve as an asset proxy to s3, where assets are
  # stored.
  config.serve_static_assets = false

  # instead of bundling the assets, include multiple css style includes
  config.assets.debug = true
  config.assets.logger = false

    # Enable serving of images, stylesheets, and JavaScripts from an asset server
  # NB: The exclusion of any protocol in the asset host declaration above will allow browsers to choose the transport mechanism on the fly.
  # So if your application is available under both HTTP and HTTPS the assets will be served to match.

  configure_s3_assets config

  config.action_controller.asset_host = "https://#{Rubber.config.s3_assets_bucket}.s3.amazonaws.com"


  config.action_mailer.default_url_options = { :host => Rubber.config.external_host }

  config.sequel.search_path = %w(public audit)
end

config / initializers / assets.rb

# Be sure to restart your server when you modify this file.

# Version of your assets, change this if you want to expire all your assets.
Rails.application.config.assets.version = '1.0'

# Add additional assets to the asset load path
# Rails.application.config.assets.paths << Emoji.images_path

# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
# Rails.application.config.assets.precompile += %w( search.js )
Rails.application.config.assets.precompile += %w( *.js *.png *.eot *.woff *.ttf *.svg *.gif)

config / environments / includes / s3_assets.rb

def configure_s3_assets(config)
  # Do not compress assets
  config.assets.compress = false

  # Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_assets = false

  # Generate digests for assets URLs
  config.assets.digest = true
  config.assets.enabled = true

  # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
  config.assets.precompile += %w(
  )
  # find javascripts/ stylesheets/ -type f | sed 's/\.scss//' | sed 's/\.coffee//' |
  #    sed 's/^.*\/\///'| sort
  # defaults to application.js, application.css
  # application.css has home.css ?

  config.assets.precompile += %w(
    handlebars.js
    jquery-1.7.js
    json2.js
  )
end

1 个答案:

答案 0 :(得分:0)

深入研究链轮代码后,我发现解析器列表为空。我不确定配置中缺少什么,但是我通过将以下内容添加到config / application.rb

来解决了该问题。

config.assets.resolve_with = [:manifest] //正在生产中

config.assets.resolve_with = [:manifest,:environment] //正在开发中