rake assets:precompile is slow in production - ruby-on-rails

My ruby on rails app take about half an hour to complete a deployment.
The longest step is
RAILS_ENV=production RAILS_GROUPS=assets bundle exec rake assets:precompile
which takes about 1073155ms
I have to wait for a long time for each deployment.
I use
ckeditor
rails_admin
I guess it is them who slow down my deployment, but I don't evidence and I don't know how to
solve it, either.
My other environments are as follows:
rails 4.0.3
ruby 2.1.1
My production.rb about assets is
config.serve_static_assets = false
# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
# config.assets.css_compressor = :sass
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = true
# Generate digests for assets URLs.
config.assets.digest = true
# Version of your assets, change this if you want to expire all your assets.
config.assets.version = '1.0'

Try to skip compiling ckeditor assets
config/environments/production.rb
require_relative '../../lib/assets/selective_assets_compressor'
config.assets.js_compressor = SelectiveAssetsCompressor.new
lib/assets/selective_assets_compressor.rb
class SelectiveAssetsCompressor < Uglifier
def initialize(options = {})
super(options)
end
def compress(string)
if string =~ /CKSource/
string
else
super(string)
end
end
end

For faster asset precompiles, you can setting config.assets.initialize_on_precompile to false in config/application.rb. Heroku requires this to be false.
config.assets.initialize_on_precompile = false
If you do that, be sure to test rake assets:precompile locally because the complete environment is not loaded, engines (or other gems) will not be loaded, which can cause missing assets.
In the other hand you can execute asset precompile before deploy locally and deploy the files precompiled.

Related

Rails config.assets.compile = false doesnt work on production

My rails app (v 5.2.4.1) works perfectly in local with config.assets.compile = true.
All the assets (images, js etc) are correctly loaded.
It works too in production with the same config (but it's very slow...)
So i read here config.assets.compile=true in Rails production, why not?
that it's a better way so set config.assets.compile = false in production
config.assets.compile = false
config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
config.assets.css_compressor = :sass
config.assets.debug = false
config.assets.digest = true
config.public_file_server.enabled = true
After set to false I made this command
RAILS_ENV=production rake assets:precompile (all the assets are generated in public/assets on Heroku)
i restart the server but all images are lost and .js are not loaded ?
Where is my mistake ?

Force stylesheet_link_tag to locate hashed asset in Rails 4

We're coming off of using the asset_sync gem for managing our assets. I'm trying to setup the Asset Pipeline using these settings.
development.rb
config.assets.debug = true
config.assets.compile = false
config.assets.digest = true
config.cache_classes = true
config.eager_load = false
application.rb
config.assets.enabled = true
config.assets.css_compressor = :sass
config.assets.js_compressor = :uglifier
config.assets.version = '1.1'
I'm precompiling in Development and the assets are going into the public directory (hashed asset for each file). When using the stylesheet_link_tag, it works perfectly if I refer to the hashed asset directly:
stylesheet_link_tag("/assets/branding/visit/skin-295511b052d49d541763f276ddcf4efc.css")
But I need to be able to just call skin.css and be served the correct hashed skin.css file. I currently get a 404 with this:
stylesheet_link_tag("/assets/branding/visit/skin.css")
Is there a setting I'm missing to point to the hashed asset? Thanks for any advice.
Tech Specs
Ruby: 2.3.1
Rails: 4.2.7.1

Rails app not loading assets on elastic beanstalk aws

For context, I essentially followed this guide to get my rails app set up: https://medium.com/#jatescher/how-to-set-up-a-rails-4-2-app-on-aws-with-elastic-beanstalk-and-postgresql-3f9f29c046e2
My app is now running on AWS:
http://ems-heroes-dev.elasticbeanstalk.com/
64bit Amazon Linux 2015.03 v2.0.1 running Ruby 2.2 (Puma)
However, I can't get any of my assets to load.
Here is my config/environments/production.rb:
EmsHeroes::Application.configure do
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = true
# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
config.assets.compress = true
config.assets.compile = true
# Bower asset paths
root.join('vendor', 'assets', 'components').to_s.tap do |bower_path|
config.sass.load_paths << bower_path
config.assets.paths << bower_path
end
# Precompile Bootstrap fonts
config.assets.precompile << %r(bootstrap-sass/assets/fonts/bootstrap/[\w-]+\.(?:eot|svg|ttf|woff2?)$)
# Minimum Sass number precision required by bootstrap-sass
::Sass::Script::Value::Number.precision = [8, ::Sass::Script::Value::Number.precision].max
config.less.paths << "#{Rails.root}/lib/less/protractor/stylesheets"
config.less.compress = true
# Generate digests for assets URLs.
config.assets.digest = true
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets.
config.assets.version = '1.0'
# 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 = false
config.log_level = :info
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new
end
My assets are loading fine via heroku though. (However, I dont plan on using heroku any more)
EDIT
It looks like its an issue with rake assets:precompile providing the wrong file names. Ex: My compiled css file is: "public/assets/application-79dc234c01a4f604b52fc53ff49ac89d.css" but Im getting a 404 looking for "application.css". Renaming the file to "public/assets/application.css" will load it properly.
Any idea why this would be the case and how I can get it to precompile properly?
This string after file name is called "digest", it's used to notify browser when file content changes. You need to use view helpers (asset_path, etc) to get name with digest, or you can turn off this feature with config.assets.digest = false

New image assets not displayed on Heroku staging site

UPDATE: I have just discovered that only when I push to production do my assets appear on staging.
Whenever I add new image assets, precompile them, and deploy to staging they do not appear. However, everything works locally and on Production. Here is my code and my process whenever adding new image assets. Any help would be appreciated. I am very new to this and have been searching everywhere for an answer.
staging.rb
Rails.application.configure do
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.action_mailer.default_url_options = { :host => 'http://site_name.herokuapp.com' }
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.assets.js_compressor = :uglifier
config.assets.compile = false
config.assets.digest = true
config.log_level = :debug
config.action_controller.asset_host = Proc.new { |source|
if source =~ /\b(.png|.jpg|.gif|.svg)\b/i
"http://key.cloudfront.net"
end
}
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new
config.active_record.dump_schema_after_migration = false
end
How I precompile my assets for staging
RAILS_ENV=staging bundle exec rake assets:precompile
How I precompile my assets for production
RAILS_ENV=staging bundle exec rake assets:precompile
application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module InfoSite
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
config.serve_static_files = true
config.assets.compile = true
config.assets.configure do |env|
if Rails.env.development? || Rails.env.test?
env.cache = ActiveSupport::Cache.lookup_store(:memory_store)
end
end
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*. {rb,yml}').to_s]
# config.i18n.default_locale = :de
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
end
end
My Process for adding an image asset:
Place image in app/assets/images
Use it in a layout (using asset pipline: = image_tag 'image.png')
Check locally (everything works)
Use RAILS_ENV=staging bundle exec rake assets:precompile
Use RAILS_ENV=production bundle exec rake assets:precompile
Check github, all assets are updated and I can see that they have precompiled
git push staging master
Now if I view staging, the assets return me an empty image box displaying the image's name.
Is there something I am missing in my process? Should I be taking a different approach to this?
Go to config/application.rb.
Find string config.assets.enabled and set value to 'true'.
Or config.assets.initialize_on_precompile and set to true.

turbo-sprockets-rails3 not supporting deploy rollbacks properly

Turbo sprockets seems to fail for the following case:
Make some changes in existing css file in a branch.
Run assets:precompile rake task.
The css file will be compiled by this gem.
Now checkout/rollback to old commit after which css file was modified.
Running precompile rake task will not compile the css file and it still contains the changes done to the css file in the later commit.
Expected Result: The css file should be compiled again as it has been restored to previous state.
EDIT:
Relevant code from application.rb
#for PDF
config.middleware.use PDFKit::Middleware, :print_media_type => true
# Enable the asset pipeline
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
config.middleware.use I18n::JS::Middleware
config.sass.preferred_syntax = :sass
config.generators.stylesheet_engine = :sass
config.generators.template_engine = :slim
config.cache_store = :file_store, "tmp/cache" # specifying the cache store to file_store with the default tmp/cache dir
## Append path to the end of routes file to catch routing errors specifically.
config.after_initialize do |app|
app.routes.append{ match '*a', :to => 'application#error_404' } unless config.consider_all_requests_local
end
from environments/production.rb :
config.cache_classes = true
# 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
Change the following files:
environments/production.rb:
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
application.rb
# Enable the asset pipeline
config.assets.enabled = false

Resources