Rails 4.0.13 assets url missing prefix and digest - ruby-on-rails

I've started working on a rails 4.0.13 app and we're trying to move the assets to a CDN
the problem appears on our staging server , the generated url are missing the prefix and digest
Ex:
https://xxyyzz.blob.core.windows.net/stylesheets/application.css
on my local machine using the same environment as on the staging server everything works fine
Ex:https://xxyyzz.blob.core.windows.net/c532aef6b12d99b3ce3f05b4fc17c02d8a682b13/application-dd1ee03f18e6ee4df65b6a21d8cfcdeb.css
config/environments/dev_1.rb :
...
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.compile = false
config.assets.digest = true
config.assets.debug = false
config.assets.version = '1.0'
config.log_level = :info
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_tags = [CustomTagger::USER_TOKEN_LAMBDA, CustomTagger::PROCESS_PID]
config.cache_store = :redis_store, RedisStores.new.url(db: 3), { expires_in: 1.day, driver: :hiredis }
config/application.rb :
...
config.assets.enabled = true
config.assets.precompile = Proc.new do |filename, path|
case
# JS
when /application-.+\.js/ =~ filename then true
when filename == 'vendor.js' then true
# CSS
when filename.end_with?('master-default.css') then true
when filename == 'application.css' then true
# IMG
when /assets\/images\// =~ path then true
# Exclude everything else.
else false
end
end
config.asset_host = 'https://xxyyzz.blob.core.windows.net'
# Each environment must have a different asset path (the SHA1 digest of
# Rails.env + Rails.application.revision).
#
# See http://guides.rubyonrails.org/configuring.html#rails-general-configuration.
# See https://github.com/rails/sprockets-rails/blob/master/lib/sprockets/railtie.rb#L174-L186
config.after_initialize do
ActiveSupport.on_load(:action_view) do
self.assets_prefix = Digest::SHA1.hexdigest(Rails.env + Rails.application.revision)
end unless Rails.env.development?
end
config.autoload_paths += %W(#{config.root}/lib)
config.middleware.insert_before Rack::Sendfile, 'HttpMethodNotAllowed'
Has anyone else encounter this ?

Found out what the problem was https://github.com/rails/rails/issues/15873
It seems rails checks if the file is present in public/assets and if it's not it doesn't set the prefix & digest .
similar issue : https://serverfault.com/questions/638905/does-rails-4-asset-path-helper-uses-asset-prefix
A possible solution would be keeping the manifest.json file in the repo
or adding a deploy task that precompiles only the manifest file .
https://github.com/rails/sprockets-rails/issues/107

Related

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

Rails 3.2.13 - Assets are not displayed in production

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

Rails 4 Bootstrap 3 glyphicon display square under production

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

Rails HAML assets precompile with initialize_on_precompile false

I'm going a little nuts here.
I have 2 servers (heroku, same configuration) in 2 environments (staging, production).
Staging compiles my /assets/templates on demand and caches it without a problem.
Production serves the file uncompiled as html (being haml markup)
The problem is: the configuration of the 2 environments is identical (except mail server)
Gems, environment config and yml are the same.
Any ideas?
Update:
The haml assets where "precompiled" on both servers but raw. Staging didn't get the file missing and re-compiled because assets where compiled for production.
Now I'am stuck with adding haml to the asset compile pipeline with initialize_on_precompile false.
staging.rb / production.rb
config.cache_classes = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = true
config.static_cache_control = "public, max-age=2592000"
config.assets.compress = true
config.assets.compile = true
config.assets.digest = true
config.assets.precompile += %w( jquery.js modernizr+respond.js polyfills.js )
config.assets.precompile += %w( gp.js ad.js richmarker.js infobox.js )
config.assets.css_compressor = :yui
config.assets.js_compressor = :uglifier
config.assets.compress=true
config.threadsafe! unless $rails_rake_task
config.i18n.fallbacks = true
config.logger = Logger.new(STDOUT)
config.logger.level = Logger.const_get(ENV['LOG_LEVEL'] ? ENV['LOG_LEVEL'].upcase : 'INFO')
config.active_support.deprecation = :notify
config.action_dispatch.rack_cache = {
:metastore => Dalli::Client.new,
:entitystore => 'file:tmp/cache/rack/body',
:allow_reload => false
}
Haml::Template.options[:ugly] = true
Solution was to add the haml into the load. Add the following to application.rb
Sprockets.register_engine '.haml', Tilt::HamlTemplate

Rails 3.2 asset_host setting ignored

My production environment -
# Code is not reloaded between requests
config.cache_classes = true
config.assets.enabled = 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 = true
config.static_cache_control = "public, max-age=31536000"
# Compress JavaScripts and CSS
config.assets.compress = true
config.assets.js_compressor = :uglifier
#config.assets.css_compressor = :yui
# 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
# See everything in the log (default is :info)
config.log_level = :warn
config.log_tags = [:remote_ip, lambda { |req| Time.now }]
# Enable serving of images, stylesheets, and JavaScripts from an asset server
ActionController::Base.asset_host = Proc.new { |source|
unless source.starts_with?('/stylesheets' || '/javascripts')
"//dddd.cloudfront.net/"
end
}
However, when I using image_tag it still returns me '/assets..' relative url and not absolute url to the asset host.
irb(main):034:0> helper.image_tag('noimage.gif')
=> "<img alt=\"Noimage\" src=\"/assets/noimage.gif\" />"
irb(main):035:0> helper.image_path('noimage.gif')
I can not seem to figure what might be missing. I even tried doing simple config.asset_host setting, and still it does not recognize the setting.
Have you tried setting the designated config?
config.action_controller.asset_host = 'https://abcd.cloudfront.net'
Not sure it works with protocol relative urls. I simply use https in my apps.
It might also be worth noting that action mailer has a similar setting:
config.action_mailer.asset_host = 'https://abcd.cloudfront.net'
There's something odd in Rails configuration. According to the documentation asset_host is supposed to hold just the host part ('yoursite.com'). I resolved with an helper:
module EmailsHelper
def email_image_tag(src)
protocol = ActionMailer::Base.default_url_options[:protocol] || 'http'
image_tag asset_path(src, protocol: protocol)
end
end
Configure it in your mailer:
class EmailNotificationsMailer < ActionMailer::Base
add_template_helper(EmailsHelper)
...
end
There's no way to force image_tag to use a URL other than to pass a URL to it.

Resources