Asset Pipeline issue in Production: Images not displaying with CDN - ruby-on-rails

I’m in the process of upgrading Rails in a project from 3.0 to 3.2, and I’ve run into an issue where the images are not rendered by the asset pipeline in Rails 3.2 (with Apache 2.0.65). Assets are deployed using a CDN.
Only images are affected. Other assets (JS, CSS) render fine.
I’d like to know what I’m doing wrong here. (more details below)
Any pointers would be of great help :)
The images return a 304 Not Modified in production (both the fingerprinted version and the regular one).
production.rb
# Disable Rails's static asset server
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
# I’d set this to true temporarily, but it doesn’t fix things
# Generate digests for assets URLs
config.assets.digest = true
# Use cdn asset host
config.action_controller.asset_host = “<CDN-hostname>"

Related

Asset pipeline in production with Rails 7.0.0.alpha

Hey everyone this one has left me somewhat stumped and I feel like I have been going around in circles. I am trying to get my assets to be served from a CDN (in this case AWS/Cloudfront)
The assets are precompiled and deployed to the S3 bucket via CI server and that is working fine.
I have read the guides and believe I have set the config/environments/production.rb correcting as follows:
# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
# config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
# Compress CSS using a preprocessor.
# config.assets.css_compressor = :sass
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
config.asset_host = ENV['RAILS_ASSET_HOST']
# 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
I have debugged that the environment variable RAILS_ASSET_HOST is set correctly to https://asset.mycdn.com but no matter what I do I continue to get the following error:
ActionView::Template::Error (The asset "application.css" is not present in the asset pipeline.
Any help appreciated. I am sure it is something stupid, just been staring too long!
From recollection, you need to have the generated manifest file on the app server, otherwise rails has no way of translating application.css into the corresponding application-SOME_SHA.css

rails 4 not using digests in asset filenames, but only in production

I have a Rails 4 app that was recently heavily upgraded (in terms of gem versions and some other things). Deploys have been working fine, but once we cleared out our tmp directories we noticed that assets stopped working in production. What's happening is for some reason in production mode, the helpers aren't using digests in any of the asset filenames (e.g. /javascripts/application.js instead of /javascripts/application-some-digest.js). This causes those assets to 404, since they do exist with their proper digest names in the public directory and Google App Engine is set up to independently serve static files for the public directory (which has always worked fine). What is really strange, though, is that in staging mode, the app is doing everything properly, so there is something about our production environment that is making the helpers not use digests.
Even weirder, though, is if I go RAILS_ENV=production rails console and do helper.asset_path 'application.js' I get the proper filename with the digest. What on earth could be going on?
And yes, we are doing RAILS_ENV=production rake:assets:precompile before deploying.
Here are the relevant parts from config/environments/production.rb:
config.eager_load = true
config.assets.cache_store = :dalli_store
# Don't force SSL because we need non-ssl cookies.
config.force_ssl = false
config.stripe_livemode = true
config.assets.digest = true
config.assets.compile = false
Here are the relevant parts from config/environments/shared.rb:
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_files = 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
# Precompile additional assets (application.js, application.css, and all
# non-JS/CSS are already added)
config.assets.precompile += ['zxcvbn.js', 'hammer.min.js', 'jquery.ba-throttle-debounce.min.js', 'mediaCheck.js', 'application-no-mq.css', 'lte-ie7.js', 'mailcheck.js', 'browserconfig.xml', 'main.css', 'oamm.js', 'oamm.min.js']
Here are the relevant parts from config/application.rb:
config.assets.precompile += %w( *.js ^[^_]*.css *.css.erb )
# Enable the asset pipeline
config.assets.enabled = true
config.log_level = :info
config.assets.precompile += ['rails_admin/rails_admin.css', 'rails_admin/rails_admin.js']
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
And finally, here are the relevant parts from config/environments/staging.rb which is somehow working fine:
# Speed up asset compilation on Heroku.
config.assets.cache_store = :dalli_store
config.eager_load = true
config.stripe_livemode = false

How to make the asset pipeline process images in subdirectories

I have a Rails app that began as a 3.0.x app and recently was upgraded to 3.2.2. The images are in app/assets/images/ecommerce/new and when I run rake assets:precompile locally they aren't copied to public/assets/. However, when I copied all image files from app/assets/images/ecommerce/new to the root images asset path (that is, app/assets/imagens) and ran the rake task again, the images were all sent to public/assets.
When I run the server locally in production mode it doesn't find the images but when I deploy to Engine Yard, it does. That's very weird, do you know what's happening?
Can't the asset pipeline process images that are inside subdirectories of app/assets/images? Am I missing anything?
Here follows the source of config/environments/production.rb, on what concerns the asset pipeline:
MyApp::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
config.cache_classes = 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
config.assets.precompile += %w[*.png *.jpg *.jpeg *.gif]
# Generate digests for assets URLs
config.assets.digest = true
config.assets.compile = true is the problem. This is not doing what you think it should. This actually tells the server to compile on demand. That might explain why you see the files sometimes, and other times you don't.
If you want to add directories, add them to config.assets.paths.

Rails compiles assets both with and without md5 hash, why?

I'm relatively new to RoR and I'm curious about why Rails compiles assets both with and without md5 hash for production?
I run bundle exec rake assets:clean then bundle exec rake assets:precompile
My production.rb file:
MyApp::Application.configure do
# Code is not reloaded between requests
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 = false
# Generate digests for assets URLs
config.assets.digest = true
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
config.assets.precompile += %w(tos.js, tos.css)
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
end
My application works with files with hashes in their names and it's the way it should be in my case :)
So I have two questions here:
1) Why is it happening when compiled?
Rails compiles assets both with and without md5 hash for production
2) What are these files (without hashes) for?
Maybe I don't get something, so please could someone explain.
The reason it does it is so that you can access the files without knowing the MD5 fingerprint (for example in a non-rails application, or a file within the rails app which isn't compiled or run by the rails stack (e.g. a 500/502 status error page). In this case you would have to compile the assets then change the css/js links in the static HTML files each time you updated the code (thus causing a change in the MD5 hash).
So instead rails produces 2 copies of each asset file, one with the fingerprint in the filename, the other without (e.g. application-731bc240b0e8dbe7f2e6783811d2151a.css, and application.css). The fingerprinted version is obviously preferred (see 'what is fingerprinting and why should I care' in the rails asset pipeline guide). But the non-digested version is there as a fallback.
As a final thought on the matter I'd take a read of the following pull request to the rails git repo: https://github.com/rails/rails/pull/5379 where they are discussing the pros and cons of the non-digested filenames, and the possibility of being able to turn off compilation of them.
HTH

Rails 3.1 Production - Javascript is missing .js endings on deployment

I've just made my Rails app and deployed it to Heroku. A very weird thing happened in the process though. Half of my javascript-files are missing their endings (.js). I have absolutely no idea why this is.I've searched far and wide, but I don't seem to find an answer.
My production-config looks like this:
# Code is not reloaded between requests
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 = true
# 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
config.assets.precompile << '*.js'
Any suggestions?
Posted this other similar question 1 hour ago (http://stackoverflow.com/questions/9049023/rails-3-1-production-javascript-loads-but-doesnt-execute). Sorry for the spamming.
Does this help? You didn't show where your js files are included. If they have dots in the name, apparently you need to tell Rails.
asset:precompile for .js files? rails 3.1

Resources