1

我和我的团队数周以来一直试图解决这个问题,但无济于事。每次我们在生产服务器上预编译资产时,我们的应用程序都会开始加载所有资产两次(一次作为预编译脚本,然后再次作为每个资产单独加载)。此问题通常发生在预编译后的 2-5 天之间(换句话说,它会在中断前工作几天)。我真的不确定为什么会这样。

我见过很多其他人有同样的问题,但他们的问题总是与开发环境有关。但是,我们的应用程序的环境是在生产环境中。

这是我们的预编译方法:

rake assets:clean
rake assets:precompile

这是我的/config/environment.rb:

# Load the rails application
require File.expand_path('../application', __FILE__)

ENV['RAILS_ENV'] ||= 'production'

# Initialize the rails application
Myapp::Application.initialize!

这是我的 /config/environments/production.rb:

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

  # Code is not reloaded between requests
  config.cache_classes = false

  # Full error reports are disabled and caching is turned on
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

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

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

  # Don't fallback to assets pipeline if a precompiled asset is missed
  config.assets.compile = true

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

  # BEGIN ICONIC PRECOMPILE ISSUE FIX
  # add iconic path to precompile
  config.assets.paths << Rails.root.join('app', 'assets', 'fonts')

  # Precompile additional assets
  config.assets.precompile += %w( .svg .eot .woff .ttf )
  # END ICONIC PRECOMPILE ISSUE FIX

  # Defaults to nil and saved in location specified by config.assets.prefix
  # config.assets.manifest = YOUR_PATH

  # Specifies the header that your server uses for sending files
  # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
  # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

  # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
  # config.force_ssl = true

  # See everything in the log (default is :info)
  config.log_level = :debug

  # Prepend all log lines with the following tags
  # config.log_tags = [ :subdomain, :uuid ]

  # Use a different logger for distributed setups
  # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)

  # Use a different cache store in production
  # config.cache_store = :mem_cache_store

  # Enable serving of images, stylesheets, and JavaScripts from an asset server
  # config.action_controller.asset_host = "http://assets.example.com"

  # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
  # config.assets.precompile += %w( search.js )

  # Disable delivery errors, bad email addresses will be ignored
  # config.action_mailer.raise_delivery_errors = false

  # Enable threaded mode
  # config.threadsafe!

  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation can not be found)
  config.i18n.fallbacks = true

  # Send deprecation notices to registered listeners
  config.active_support.deprecation = :notify

  # 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
end

而且,虽然我不明白你为什么需要它,但这是我的 /config/environments/development.rb:

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

  # 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 = true

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

  # Don't care if the mailer can't send
  config.action_mailer.raise_delivery_errors = false

  # 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

  # Do not compress assets
  config.assets.compress = false

  # Expands the lines which load the assets
  config.assets.debug = true
end

***更新*

根据建议的答案,我修改了我的环境/production.rb。现在看起来像这样:

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

  # Code is not reloaded between requests
  config.cache_classes = false

  # Full error reports are disabled and caching is turned on
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

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

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

  # Don't fallback to assets pipeline if a precompiled asset is missed
  config.assets.compile = true

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

  # BEGIN ICONIC PRECOMPILE ISSUE FIX
  # add iconic path to precompile
  # config.assets.paths << Rails.root.join('app', 'assets', 'fonts')

  # Precompile additional assets
  # config.assets.precompile += %w( .svg .eot .woff .ttf )
  # END ICONIC PRECOMPILE ISSUE FIX

  # Defaults to nil and saved in location specified by config.assets.prefix
  # config.assets.manifest = YOUR_PATH

  # Specifies the header that your server uses for sending files
  # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
  # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

  # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
  # config.force_ssl = true

  # See everything in the log (default is :info)
  config.log_level = :debug

  # Prepend all log lines with the following tags
  # config.log_tags = [ :subdomain, :uuid ]

  # Use a different logger for distributed setups
  # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)

  # Use a different cache store in production
  # config.cache_store = :mem_cache_store

  # Enable serving of images, stylesheets, and JavaScripts from an asset server
  # config.action_controller.asset_host = "http://assets.example.com"

  # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
  # config.assets.precompile += %w( search.js )

  # Disable delivery errors, bad email addresses will be ignored
  # config.action_mailer.raise_delivery_errors = false

  # Enable threaded mode
  # config.threadsafe!

  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation can not be found)
  config.i18n.fallbacks = true

  # Send deprecation notices to registered listeners
  config.active_support.deprecation = :notify

  # 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
end

我的所有资产现在都在预编译后立即加载两次,而不是解决我的问题。我不确定我在这里做错了什么。

这是我正在运行的命令:

rm -rf public/assets
rake assets:clean
rake assets:precompile

任何见解将不胜感激。谢谢!

4

3 回答 3

3

也许在生产环境中禁用资产的实时编译可以解决您的问题。

在您的 config/environments/production.rb 集中:

config.assets.compile = false

快速浏览一下这个问题。它可能会澄清问题的“几天后”部分。

GL & 高频

于 2013-11-22T14:20:50.337 回答
1

您说您的所有资产都从 application.css / application.js 加载一次,然后再次从它们的各个文件加载。根据您所说,听起来您的元素中有一堆<script><style>标签<head>- 是真的吗?这应该只发生在你有config.assets.debug = true. 在生产中,单个文件根本不应该出现在那里。你应该只有 application.css 和 application.js。

这让我认为您的生产配置有问题,或者您的服务器上使用了错误的环境配置。

(如果您在布局中添加了指向各个文件的链接,也可能发生这种情况。您这样做了吗?)

如果您提供<head>文档的元素 + 应用程序布局的 head 部分(或您正在使用的任何布局),则调试会更容易。

一些想法:

  • 你的服务器设置了RAILS_ENV=production吗?

  • 当你在你的生产机器上编译你的资产时,你是否设置RAILS_ENV=production了 rake 任务?

  • 您的生产机器上的时钟是否正确?

  • 您的资产真的被加载了两次,还是您只是看到 JS 回调多次触发?如果您使用的是 Turbolinks,那么一旦您单击任何启用了 turbolinks 的链接(但不是在重新加载后),您的文档正文中附加了 on-document-ready 回调的任何 JS 都将开始多次触发。

  • 您更新的配置中仍然有“config.assets.compile = true”。如果您正在预编译资产,则不需要它,它可以掩盖问题,所以我建议将其改回 false。

于 2013-11-24T18:31:55.833 回答
0

这是我用于生产应用程序的资产配置:

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

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

# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false

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

请尝试使用该配置并让我们保持更新。

于 2013-11-27T16:15:36.760 回答