Asset pipeline in production with Rails 7.0.0.alpha - ruby-on-rails

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

Related

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

Rails Assets and CDN Issues (CSS, JS references)

I am dealing with Rails 3.1.2, asset_sync and cludfront. I have installed asset_sync and precompiled all the assets. The problem I am facing is the following:
rake compiles combine javascript and css files into a application.[js|css].
In production mode the application is still referencing the original name but with the new cdn path and I get a 404 error.
this is production env file:
Griov4::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# 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 = true
# Generate digests for assets URLs
config.assets.digest = true
# Defaults to Rails.root.join("public/assets")
# config.assets.manifest = YOUR_PATH
# 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 = true
# See everything in the log (default is :info)
# config.log_level = :debug
# Use a different logger for distributed setups
# config.logger = SyslogLogger.new
# Use a different cache store in production
# config.cache_store = :mem_cache_store
# Enable serving of images, stylesheets, and JavaScripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
# Disable delivery errors, bad email addresses will be ignored
# config.action_mailer.raise_delivery_errors = false
# Enable threaded mode
# config.threadsafe!
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
config.action_controller.asset_host = "#{ENV['CDN']}"
end
This is my .env file
FOG_PROVIDER=AWS
FOG_DIRECTORY=
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
CDN=http://d3tf1w68p27174.cloudfront.net
RACK_ENV=production
my manifest file within js folder:
// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
//= require_tree .
my manifest file within css folder:
/*
* This is a manifest file that'll automatically include all the stylesheets available in this directory
* and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
* the top of the compiled file, but it's generally better to create a new file per style scope.
*= require_self
*= require bootstrap.min
*/
Then I type: $bundle exec rake assets:clean assets:precompile
application.ss and application.js gets created but unfortunately the production application still refers to the original css/js files with the following path:
http://d3tf1w68p27174.cloudfront.net/assets/home-24d72d1643e0016381b14c19d90d9e74.css
http://d3tf1w68p27174.cloudfront.net/assets/home-74ac0007a6e42997f8210f80b99a203b.js
I checked both my local folder and the cdn folder and none of them contains those files.
asset_sync is working correctly because I can see the rest of the assets on my cdn folder.
I know it could be something related to the asset pipeline, but I can't figure out what it is.
Thanks for your help.
To ensure all of your assets reference the new asset host, make sure you’re using the Rails AssetTagHelper methods (like image_tag, stylesheet_link_tag, favicon_link_tag, etc).
If you are using background image references in your CSS, you can leverage the multiple asset-preprocessor support to ensure they also reference the new asset host.
For example, to enable multiple asset-preprocessing on "home.css", you can add the .erb extension to it changing it to "home.css.erb". This file will first be processed by ERB, then CSS, which means you can reference your assets as shown in the example below:
body {
background: url(<%= asset_path 'bg.png' %>);
}
You can take this further by doing something like "home.css.scss.erb".
See this and this for more info.

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