1

my team and i have been trying to figure this one out for weeks to no avail. Every time we precompile our assets on our production server, our application will begin to load all of our assets twice (once as a precompiled script, and then again as each asset individually). This problem usually occurs between 2-5 days after the precompile (in other words, it works for a few days before breaking). I'm really not certain why this is happening.

I have seen a lot of other people with the same issue, but their issues are always pertaining to the development environment. However, our application's environment is in production.

here is our method for precompiling:

rake assets:clean
rake assets:precompile

here is my /config/environment.rb:

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

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

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

here is my /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

and, although i don't see why you would need it, here is my /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

***UPDATE*

as per the suggested answer, I have modified my environments/production.rb. it now looks like this:

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

instead of fixing my problem, all of my assets are now loading twice immediately after precompiling. I'm not sure what I'm doing wrong here.

here are the commands i'm running:

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

any insight would be greatly appreciated. thanks!

Fred Garbutt
  • 187
  • 2
  • 15

3 Answers3

3

Maybe disabling the live compilation of assets on production could fix your issue.

In your config/environments/production.rb set:

config.assets.compile = false

Take a quick look at this question. It may clarify the "after a few days" part of the issue.

GL & HF

Community
  • 1
  • 1
rlecaro2
  • 755
  • 7
  • 14
  • I tried this (i even tried it in the past as well). It seems to do something when i run the folliwing commands sequentially: rm -rf public/assets, then rake assets:clean, then rake assets:precompile. but now i am simply expediting the problem i was already having. instead of it taking a few days to load the assets twice, I am loading all of the assets twice immediately. I will update the question with my new environments/production.rb – Fred Garbutt Nov 22 '13 at 21:14
1

You say that all of your assets are being loaded once from application.css / application.js and then again from their individual files. From what you said, it sounds like you have a bunch of <script> and <style> tags in your <head> element - is that true? That should only happen when you have config.assets.debug = true. In production, the individual files should not appear there at all. You should only have application.css and application.js.

This makes me think that something about your production configuration is wrong or that the wrong environment configuration is being used on your server.

(It could also happen if you added links to the individual files in your layout. Are you doing that?)

It would be easier to debug if you provided the <head> element of your document + the head section of your application layout (or whatever layout you're using).

A few thoughts:

  • Is your server setting RAILS_ENV=production?

  • When you compile your assets on your production machine, are you setting RAILS_ENV=production for the rake task?

  • Is your clock correct on your production machine?

  • Are your assets really being loaded twice, or are you just seeing JS callbacks fire multiple times? If you're using Turbolinks, any JS in the body of your document that attaches on-document-ready callbacks will begin firing multiple times once you have clicked on any turbolinks-enabled link (but not after a fresh load).

  • You still have "config.assets.compile = true" in your updated config. You shouldn't need that if you're precompiling assets, and it can cover up problems, so I would suggest changing it back to false.

Nick Urban
  • 3,568
  • 2
  • 22
  • 36
  • Hi, thanks for the thorough response! I will attempt to answer all of your questions. I have not added links to individual files in my layout. a typical example of the way i would load my javascripts is something like this: <%= javascript_include_tag "application", 'admin/load' %> My rails server is definitely running in production I don't know how to check the accuracy of my clock on my production server, but I think so. We are not using turbolinks. Our assets are being loaded once inside application.js, and then again individually. – Fred Garbutt Nov 25 '13 at 23:18
  • You're saying that in the HTML that Rails is generating, you have both a – Nick Urban Nov 26 '13 at 00:38
  • Could you provide a link to the site or a copy of the generated code? It would be much easier to understand the specific situation if I could see 1) the generated HTML and 2) the application layout. – Nick Urban Nov 26 '13 at 00:41
  • I would gladly post the link, except that the problem is not currently happening. When it IS happening, though, I see in my resources that application.js is fully compiled with all of the minified javascript code inside of it. I also see all of my javascript files being loaded individually. – Fred Garbutt Nov 26 '13 at 02:34
  • Where and how do you see it? In the network tab of your browser debugger? When you "view source" on the page in question? In the server logs? Where is the symptom appearing? – Nick Urban Nov 26 '13 at 13:50
  • I see it in chrome by inspecting element and navigating to Resources->Frames->"Some folder"->Scripts – Fred Garbutt Nov 27 '13 at 19:12
0

Here is the assets config I'm using for production apps:

# 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

Please try with that config and keep us updated.

glorieux
  • 119
  • 3