Rails compiles assets both with and without md5 hash, why? - ruby-on-rails

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

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

Running assets precompile in Rails 4.2.5 locally creates problems with "production.rb" defaults

I've built an experimental website using Rails ver. 4.2.5.1, on a CentOS 6.6 Linux box. I followed the "Getting Started With Rails" guide, and the Weblog build tutorial,
and then made changes to use SSL, and run in production mode.
To precompile the assets (javascript and images), I ran "rake assets:clobber" to remove previous cached results, and then "rake assets:precompile". This creates the "fingerprinted" filenames (long hex string (MD5 hash?) catenated to original filenames), and drops compiled assets into ../Weblog/public/assets directory.
The "assets:precompile" process is documented in the Rails "Asset Pipeline" documentation, and the Sprockets documentation.
The documentation I followed is at:
http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets
https://github.com/rails/sprockets-rails
The "production.rb" file as generated by default using the methods in the "Getting Started With Rails" guide, does not seem to work. I start the WEBrick rails server using a two line script file, which is:
export SSL=true
rails server -b 0.0.0.0 -p 3000 -e production
I wanted to use the assets:precompile feature, and to make this work, I have made changes to the "config/application.rb" file, as per the sprockets-rails documentation, and included the line:
require 'sprockets/railtie'
I also had to made modifications to file "config/environments/production.rb" file. The default version of the file had:
# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
# config.assets.css_compressor = :sass
Note that the :uglifier line was active, but the :sass line was commented out.
The default production.rb file also had this line:
# Do not fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
What I had to do to make the WEBrick server actually operate with precompile assets, in production mode, from the Linux box, was to invoke the css_compessor, and to comment out the "config.assets.compile = false" line, not toggle it. (If the assets are already locally precompiled, then I don't need or want to compile them live, I am assuming)
The changes to the config/production.rb file to make the production mode webserver operate on the CentOS box, are thus:
# 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
And that seems to do it. Without these changes, images would not render in the browser (Firefox 44 on Windows/ Safari on iOS/ Firefox on Android 5.1.1), and the "Destroy" link (to invoke delete method and delete an article from the database of articles) would not bring up the confirmation dialog box, or do the delete action. With the above changes to production.rb it all works, so when I run the server, the image files display, and the javascript to actually run the "Destroy/delete" process is getting served up to the browser, and the tutorial Weblog website works as expected. But it won't work if "config.assets.js_compressor = :uglifier" is turned on (by removing the # comment), and it won't work if the "config.assets.compile = false" is not commented out.
As near as I can tell, the precompile is working, and the precompiled assets, with the fingerprinted info (the long MD5 hex string) added to the filenames of the image files, are being served correctly, as is the javascript.
I understand most deployments are to Heroku and AWS, so my use of my own hardware for production mode is a bit non-standard for Rails with WEBrick, but I am curious as to why. Am I missing some needed step?
Is there a different (better?) way to do the assets precompile assuming production mode is run on a local Linux box?
And is there any obvious reason why ":uglifier" is not working? Thx.

Asset Pipeline issue in Production: Images not displaying with CDN

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>"

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 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