We've just deployed a Rails 4.0.3 app to production and have found that asset paths generated by stylesheet_link_tag and javascript_link_tag are missing their fingerprints. So instead of requesting something like application-c841bd1c82c25bb1de8452d2338479f7.js, the page is just request application.js.
RAILS_ENV=production bundle exec rake assets:precompile is successful and generates fingerprinted files.
The bits from config/environments/production.rb that seem relevant are:
# 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
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
This is running on our own Apache server, not Heroku. I've looked around quite a bit and found similar problems, but none of the troubleshooting steps for those are helping here. Thanks.
More Information
In case it is helpful, here are the full contents (commented lines removed) of our config files:
application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
module Ctrc
class Application < Rails::Application
config.ceal.application_title = "CTRC Budgeting"
config.encoding = "utf-8"
config.filter_parameters += [:password]
config.active_support.escape_html_entities_in_json = true
config.assets.enabled = true
config.assets.version = '1.0'
config.pmacs_redmine.project_identifier = 'itmat-gcrc'
config.app_data_path = '/data/web/apps/itmat/ctrc'
config.paperclip_defaults = {
path: "/data/web/apps/itmat/ctrc/:attachment/:id/:style/:basename.:extension"
}
if ENV['RAILS_RELATIVE_URL_ROOT']
config.assets.prefix = ENV['RAILS_RELATIVE_URL_ROOT'] + '/assets'
end
end
end
production.rb
Ctrc::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.assets.compress = true
config.assets.compile = false
config.assets.digest = true
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.eager_load = true
end
I know that the application is running in production mode because if I set
config.assets.compile = true
in that file, the CSS and JavaScript are compiled and requested correctly.
I'm including those assets in the page like so:
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>
but it is still generating links to those assets like this:
<link href="/apps/itmat/ctrc/stylesheets/application.css" media="all" rel="stylesheet">
<script src="/apps/itmat/ctrc/javascripts/application.js"></script>
instead of the fingerprinted links I would expect to see.
I had a similar issue and it turned out to be a problem with the gem AssetSync. Either setting config.assets.compile = true or removing the gem resolved the issue. Compiling assets on the fly and having your Rails serve your assets might be acceptable if you're using a CDN, but generally, taking another approach is usually recommended.
For rails 4.x be sure to set the following:
config/application.rb
config.assets.enabled = true
config.assets.version = '1.0'
config/environments/production.rb
config.assets.compile = false
Related
I am in the process of upgrading our product from rails 4.1 to 5.2. I'm hung up on what appears to be an asset pipeline related issue. When I load the app in our browser, I see the following error in the server log and the app is missing all styling and javascript code that it normally has.
DEPRECATION WARNING: The asset application.js" is not present in the asset pipeline.Falling back to an asset that may be in the public folder.
This behavior is deprecated and will be removed.
To bypass the asset pipeline and preserve this behavior,
use the `skip_pipeline: true` option.
When I wget the index.html of the application, I see that all of the images and javascript urls are lacking the fingerprint digest they should have. Interestingly enough, they do have the S3 asset_host we have, so they are in fact being processed by the helpers.
To demonstrate, I have two servers which have identical configurations, but one is running 4.1 and the other 5.2. On both, I am using S3 as an asset_host and digest is turned on. I've run the following commands in their consoles:
=== Rails 4 ===
Loading qatest environment (Rails 4.1.0)
irb(main):001:0> Rails.application.config.action_controller.asset_host
=> "https://redacted.s3.amazonaws.com"
irb(main):002:0> Rails.application.config.assets.digest
=> true
irb(main):003:0> ActionController::Base.helpers.asset_path('application.js')
=> "https://redacted.s3.amazonaws.com/assets/application-042f2014ca329c79c304ab1332557040d3f7b922247202f40c28acc950f30ef8.js"
=== Rails 5 ===
Loading sean environment (Rails 5.2.1)
irb(main):001:0> Rails.application.config.action_controller.asset_host
=> "https://redacted.s3.amazonaws.com"
irb(main):002:0> Rails.application.config.assets.digest
=> true
irb(main):003:0> ActionController::Base.helpers.asset_path('application.js')
=> "https://redacted.s3.amazonaws.com/application.js
As requested, production.rb
require "#{File.dirname(__FILE__)}/includes/s3_assets"
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb
config.eager_load = true
# 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 = false
config.log_level = ENV['SHOW_SQL'] != 'false' ? :debug : :info
# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
user_name: Rubber.config.email_username,
password: Rubber.config.email_password,
address: Rubber.config.email_server,
port: Rubber.config.email_port,
enable_starttls_auto: true, # detects and uses STARTTLS
authentication: 'login' # Mandrill supports 'plain' or 'login'
}
# 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
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fall back to assets pipelin if a precompiled asset is
# missed.
config.assets.compile = false
# Generate digests for assets URLs
config.assets.digest = true
config.assets.enabled = true
# Nginx will serve as an asset proxy to s3, where assets are
# stored.
config.serve_static_assets = false
# instead of bundling the assets, include multiple css style includes
config.assets.debug = true
config.assets.logger = false
# Enable serving of images, stylesheets, and JavaScripts from an asset server
# NB: The exclusion of any protocol in the asset host declaration above will allow browsers to choose the transport mechanism on the fly.
# So if your application is available under both HTTP and HTTPS the assets will be served to match.
configure_s3_assets config
config.action_controller.asset_host = "https://#{Rubber.config.s3_assets_bucket}.s3.amazonaws.com"
config.action_mailer.default_url_options = { :host => Rubber.config.external_host }
config.sequel.search_path = %w(public audit)
end
config/initializers/assets.rb
# Be sure to restart your server when you modify this file.
# Version of your assets, change this if you want to expire all your assets.
Rails.application.config.assets.version = '1.0'
# Add additional assets to the asset load path
# Rails.application.config.assets.paths << Emoji.images_path
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
# Rails.application.config.assets.precompile += %w( search.js )
Rails.application.config.assets.precompile += %w( *.js *.png *.eot *.woff *.ttf *.svg *.gif)
config/environments/includes/s3_assets.rb
def configure_s3_assets(config)
# Do not compress assets
config.assets.compress = false
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Generate digests for assets URLs
config.assets.digest = true
config.assets.enabled = true
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
config.assets.precompile += %w(
)
# find javascripts/ stylesheets/ -type f | sed 's/\.scss//' | sed 's/\.coffee//' |
# sed 's/^.*\/\///'| sort
# defaults to application.js, application.css
# application.css has home.css ?
config.assets.precompile += %w(
handlebars.js
jquery-1.7.js
json2.js
)
end
After digging around in the sprockets code, I discovered that my list of resolvers was empty. I'm not sure what was missing from my configuration, but I fixed the problem by adding the following to config/application.rb
config.assets.resolve_with = [:manifest] // in production
config.assets.resolve_with = [:manifest, :environment] // in development
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
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
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