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 ?
Related
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
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
I've spent the whole day trying to figure out this issue, but without any positive results.
When I load my website, there are no CSS, images or working javascript.
The app is running on Rails 3.2.13 and Capistrano 2.
Here's my setup:
config/environments/production.rb
Appname::Application.configure do
config.cache_classes = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
#config.serve_static_assets = false
config.serve_static_assets = true
config.assets.compress = true
config.assets.compile = true
config.assets.precompile = ['*.js', 'application.css', 'styles.css.scss', '*.css.erb']
config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif *.svg *.woff *.ttf *.ico)
config.assets.digest = true
config.cache_store = :memory_store
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
end
application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
Bundler.require(*Rails.groups(:assets => %w(development test)))
end
module Apname
class Application < Rails::Application
config.encoding = "utf-8"
config.filter_parameters += [:password]
config.active_support.escape_html_entities_in_json = true
config.active_record.whitelist_attributes = true
config.assets.enabled = true
config.assets.paths << "#{Rails.root}/app/assets/fonts"
config.assets.precompile << /\.(?:svg|eot|woff|ttf)\z/
config.assets.version = '1.0'
config.assets.initialize_on_precompile = false
end
end
I ran locally bundle exec rake assets:precompile -> the files were generated to the public/assets directory.
But still -- there are no assets on production.
I have already no idea what to change in the config files, still the same result on the production server.
Every help will be warmly appreciated!
EDIT:
I just noticed in the console that images are loaded correctly (I can load http://website.com/assets/social_icons/facebook_ico.png), but not CSS+JS (http://IP/assets/application-3b6ba7703e465259e4e7ebee17c5ea1e.js 404 NOT FOUND - the same for .css)
Did you try to restart the webserver after compiling the assets?
Are the rights set correctly (chmod 755 . -R)?
Also, check that your webserver configuration points to app_root/public and not just app_root
EDIT1:
Check if you can load the url directly. In addition verify that the rendered page display correctly-cached urls.
If one of the two fails try forcing compilation for production environment
rake assets:precompile RAILS_ENV=production
EDIT2:
If nothing works try rolling back to default assets config for production:
# Disable Rails's static asset server (Apache or nginx will already do this).
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'
instead of:
#config.serve_static_assets = false
config.serve_static_assets = true
config.assets.compress = true
config.assets.compile = true
config.assets.precompile = ['*.js', 'application.css', 'styles.css.scss', '*.css.erb']
config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif *.svg *.woff *.ttf *.ico)
config.assets.digest = true
EDIT 3:
EDIT:
I just noticed in the console that images are loaded correctly (I can
load http://website.com/assets/social_icons/facebook_ico.png), but not
CSS+JS
(http://****/assets/application-3b6ba7703e465259e4e7ebee17c5ea1e.js 404
NOT FOUND - the same for .css)
That also means you aren't actually compiling assets. If you were, you'd need to load facebook_ico.png with the hash attached to the name
I use bootstrap-sass to display glyphicons, it works well at localhost under development, but after i deploy it to a server use production, it display as a little square.
I have already search for answers, they just tell me to precompile something. But they didn't work.
This is my production.rb
OperationBackend::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 = false
config.assets.js_compressor = :uglifier
config.assets.compile = false
config.assets.digest = true
config.log_level = :info
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.assets.precompile += ["*.woff", "*.eov", "*.svg", "*.ttf"]
config.log_formatter = ::Logger::Formatter.new
config.active_record.dump_schema_after_migration = false
Paperclip.options[:command_path] = "/usr/local/bin"
end
I also try make config.assets.complie = true
and do RAILS_ENV=production rake asset:precompile but they all didn't work
This problem torture me hours! Wish to got help from you guys!
config.assets.compile = true
should help
This did it for us. The glyphicons are in the vendors folder, so this is what we had to do to get them to work.
For some reason this worked for our production environment, but not our staging. Our staging.rb file is exactly the same as the production.rb file so this is quite the mystery.
# Disable Rails's static asset server (Apache or nginx will already do this).
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 = false
# Generate digests for assets URLs.
config.assets.digest = true
config.assets.precompile << lambda do |filename, path|
path =~ /vendor\/assets/ && !%w(.js .css).include?(File.extname(filename))
end
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.