预编译我的资产是在生产模式下加载我的所有资产两次

时间:2013-11-06 18:59:41

标签: ruby-on-rails rake asset-pipeline precompile

我的团队和我一直试图将这个问题弄清楚几周无济于事。每次我们在生产服务器上预编译资产时,我们的应用程序将开始加载我们的所有资产两次(一次作为预编译脚本,然后再作为每个资产单独加载)。此问题通常发生在预编译后的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

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

3 个答案:

答案 0 :(得分:3)

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

在config / environments / production.rb中设置:

config.assets.compile = false

快速浏览this question。它可能会澄清“几天后”部分问题。

GL&amp; HF

答案 1 :(得分:1)

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

这让我觉得您的生产配置有些不对,或者您的服务器上使用了错误的环境配置。

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

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

一些想法:

  • 您的服务器设置为RAILS_ENV=production

  • 在生产计算机上编译资产时,是否为rake任务设置了RAILS_ENV=production

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

  • 您的资产是否真的被加载了两次,或者您只是看到JS回调多次触发?如果您正在使用Turbolinks,那么一旦您点击任何支持turbolinks的链接(但不是在新负载之后),文档正文中附加文档就绪回调的任何JS都将开始多次触发。

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

答案 2 :(得分: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

请尝试使用该配置并随时更新。