Public folder size is huge - digested assets - Ruby on Rails - ruby-on-rails

In my RoR app the production.rb has
MyApp::Application.configure do
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.action_dispatch.show_detailed_exceptions = false
# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_assets = true
# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
# Callback 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.3'
config.assets.prefix = "/assets-#{`git rev-parse HEAD`[0..8]}"
config.log_level = :debug
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.action_controller.perform_caching = true
end
The problem is in the server, the public folder size is 15GB. It contains a lot of folders with names like this:
assets-2d947fb8a
assets-64bb1c0ee
assets-6b4777157
assets-33d9c439d
assets-e6f2c6c37
assets-cb3a9bc23
assets-f1e77e02d
Each folder contains digested images, js, css, and more.
So, is there a way to delete/expire these assets automatically?

Although very non-standard, I have to say, I kind of like this approach, seems cleaner to me than one huge assets dir that Rails uses now.
Anyway, if you want to keep say 10 newest assets directories and delete everything else, you can simply create a capistrano task (if you're using capistrano) and call a shell command for the dirs expiration from it, such as this one:
ls -td -- /path/to/rails/public/assets-*/ | # list dirs, order by newest
sed 's/.$//' | # remove the trailing '/'
tail -n +10 | # leave out first 10 directories
xargs rm -ri # delete the dirs!
With the -i option, deletion will be interactive, i.e. you will be asked to confirm every file to be deleted. Change this to -f when you are sure you have the script production-ready and the dirs will be deleted silently. SO answers here helped me construct the ls part.
You can put this small "script" into a capistrano task to be called after assets precompilation during deploy.

Related

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

Font Awesome icons shows up as black squares on Heroku

I'm using the Font-Awesome-Sass gem with my Rails Project.
I followed the gem's instructions, and have included the #import into the application.css.scss. I'm also using the correct Rails syntax in the html to reference the icons. Everything works great locally, but as soon as I push to my staging Heroku environment, the icons just show as black squares.
Here is a snippet of staging.rb (the staging Heroku environment I was talking about)
# Code is not reloaded between requests.
config.cache_classes = true
config.eager_load = true
# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Enable Rack::Cache to put a simple HTTP cache in front of your application
# Add `rack-cache` to your Gemfile before enabling this.
# For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid.
# config.action_dispatch.rack_cache = 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.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'
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# Set to :debug to see everything in the log.
config.log_level = :info
# 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 = "//MYCLOUDFRONTHOSTINGURL-hidden-for-this-question"
# 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
# Disable automatic flushing of the log to improve performance.
# config.autoflush_log = false
# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new
Is there something I'm missing?
Thanks for the help!
This happens when you have another font overriding your icon font. Search your CSS to see if there is another font-family:OtherFont!important;.
First make sure that your Font-awesome CSS is loaded, and font-urls point to the correct fonts.
Then explicitly specify your i.fa tags (which are used by Font-awesome to create the font) to
i.fa {
font-family:FontAwesome!important;
}
Worked great for me.
I had the same problem! Font awesome was working on local however when deployed to Heroku all icons appeared as a small white square. It was previously working without problem.
This worked for me: Add below line to layout file.
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
Works fine now.

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