Rails 4 asset pipline slow - ruby-on-rails

I have read dozens of posts all over the internet about this but mostly for Rails 3.
For some reason, it takes 35 seconds to load a page with a total of 34 assets in development.
I have done the following:
Set config.assets.debug = false in development.rb, but this only cuts it down to 30 seconds,
reloaded the page multiple times, but each load is just as slow,
precompiled the assets manually (although this seems to only applies in production),
looked for something like rails-dev-boost (https://github.com/thedarkone/rails-dev-boost) but cannot find anything for rails 4.
I'm developing on an ubuntu box using vagrant (https://github.com/rails/rails-dev-box). The host machine is the fastest, new MacBook Pro.
I'm almost going to throw out the asset pipeline altogether and compile the assets myself. I cannot wait 35 seconds every time I need to reload the page.
Any help on this is appreciated.
development.rb:
MyProject::Application.configure do
config.cache_classes = false
config.eager_load = false
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.action_mailer.raise_delivery_errors = false
config.active_support.deprecation = :log
config.active_record.migration_error = :page_load
config.assets.debug = false
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
end
Edit
I think the issue is the VM. I installed ruby on my Mac using homebrew and ran the same project with the exact configuration and it loads in under 1 second. I'm not sure what the problem is.

The issue is probably with your shared_folder on Vagrant box, which can be fixed by using rsync:
Install rync on your mac: brew install rsync
In your Vagrant file, add type: "rsync" to your synced_folder: config.vm.synced_folder "[path to host folder]", "[path to guest folder]", type: "rsync"
I was experiencing 20-25 second page load times, and once I added rsync, my pages were loading in under a second. Since the shared_folder by default is only pointing to your code, it essentially has to load those files to the VM each time the page loads.

adding gem 'rack-mini-profiler' or less-easy-to-use gem 'ruby-prof' can help to find what renders slow
i have around fifty heavy js|css files and it renders pretty fast in development environment, so it is very unlikely that problem core in assets pipeline itself

Related

Rails server start up time is very slow with upgrade

I am trying to upgrade rails app from v3 to v4. In v3, the server start time is less than a minute. but in v4, it is taking more than 30 mins. In logs, i can see that for each server start,
1/10 preloading assets...
2/10 preloading assets...
.
.
10/10 preloading aseets...
done
is being logged and this is the part taking up 99% of the time. I believe assets are being compiled every time while loading the classes. could someone please let me know which config is causing this ?
I have tried most of the suggested solutions related to asset config in stackoverflow but doesnt seem to get resolved.
current config:
config.cache_classes = false
config.eager_load = false
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.reload_classes_only_on_change = true
config.assets.digest = false
config.assets.debug = false
config.assets.compress = false
The best way to tackle this is to profile your app startup. There are several ways that you can take, some fairly technical, but not invasive (e.g. using DTrace), others more invasive, but easier to do (e.g. monkeypatch require).
This page contains a couple of relevant sources: https://waynechu.cc/posts/196-profiling-rails-boot-time. I'd be surprised if none of the options proposed there help.
Another option can be found here: https://gist.github.com/robdimarco/e610b2b5c31c68bb13fe
The IMO easiest way to use this is as follows:
add ruby-prof to your Gemfile and run bundle install
boot an irb shell with it: bundle exec irb -rruby-prof
run the code from the snippet in the irb shell
The output there should give you a good indication where to start looking.
Thanks for pitching in folks. Just now, got it resolved.
I am using turbo-sprockets-rails-4 in which preload assets is enabled by default for dev environments.
added below config in config/environments/development.rb
TurboSprockets.configure do |config|
config.preloader.enabled = false
end
now server startup time is as fast as rails3
this is mentioned in their readme itself, but missed it while skimming through

How to show unbundled individual javascript and css of my rails web-application

I want to show unbundled javascript and css of my web-application to a UI developer. I have tried adding require 'sprockets/railties'
config.assets.debug = true
in my production.rb, but it did not work and I can still see bundled, uglified css/js in my browser sources.
I tried running my production in development mode by adding rack_env development in my /etc/nginx/nginx.conf http block, but I get Bad Request due to following error:-
invalid number of arguments in "rack_env" directive in /etc/nginx/nginx.conf:16
Please help
Did you try purging and recompiling your assets by any chance? Depending on your deploy method, production assets may not be recompiled on every deploy / application start.
rake assets:clean (rake assets:clobber) for Rails 4+
rake assets:precompile
By default, config.assets.debug = true is what controls this 'bundling' behavior.
You might also try to comment out
config.assets.js_compressor = ... or
config.assets.css_compressor = ...
if you have any of those in your production.rb.
Another reason may be any sort of external cache, depending on where you host your app: Cloudflare or Heroku Asset Pipeline Cache (those usually cache based on MD5 of your assets).
And the last but not the least is... browser cache, just in case :)

Heroku can't find some of my rails assets

I'm new to web development, and I've been working on a simple app for learning purposes. I've been able to deploy to Heroku from my local machine, but for some reason, the custom classes that I use for some vector images aren't rendering on Heroku.
This is my page, and as you can see, there are just squares where there should be different images. Looking in the inspector, you see the following errors:
Failed to load resource: the server responded with a status of 404 (Not Found) - https://enigmatic-hollows-2702.herokuapp.com/assets/flaticon.woff
Failed to load resource: the server responded with a status of 404 (Not Found) https://enigmatic-hollows-2702.herokuapp.com/assets/flaticon.ttf
I have tried recompiling assets, recommitting, and pushing to Heroku, but this error persists. What's weird is that if I use Heroku Bash and view the Heroku file directory, it looks like the files are there:
But they are still throwing up that error in the inspector. I've also got the following set in my production.rb file:
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
config.assets.compile = true
config.assets.js_compressor = :uglifier
Any idea what could be causing this asset issue? I'm using Rails 4.2.0.
I was able to answer my own question after reading through this link. Essentially, I moved the font files out of my CSS directory and into their own fonts directory under the app/assets directory, and then in production.rb added the following line to the Application class:
config.assets.paths << Rails.root.join("app", "assets", "fonts")
and presto! It worked. I didn't mess around with #font-face in CSS, since the assets I'm using from flat-icons are built through custom classes used in span or div blocks.

Rails does not reload controllers, helpers on each request in FreeBSD 9.1

I've detected weird behavior of rails. Please give me some advice!
For example I have a code like this:
def new
raise
end
I start rails server in development mode.
Hit refresh in browser and see
RuntimeError in AuthenticationController#new
Okay. I comment out line with "raise" like this:
def
# raise
end
Hit refresh in browser but again I see that error as shown above. Even though in browser I see code with commented out "raise".
My guess is that controllers and helpers etc. are getting reloaded but rails returns cached results.
config/environments/development.rb:
Rails.application.configure do
# BetterErrors::Middleware.allow_ip! '192.168.78.0/16'
# 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
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true
# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = false
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end
How I start server:
=> Booting Puma
=> Rails 4.2.1.rc3 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Puma 2.11.1 starting...
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://0.0.0.0:3000
Any suggestions please.
UPDATE 1.
This problem does not exists in Ubuntu 14.04 but exists in FreeBSD 9.1.
I've created simple app and tested it out in FreeBSD first (same problem), in Ubuntu then (no problem).
Can you help me with advice how to deal with this problem on FreeBSD 9.1?
Had the same issue with Rails 5 + Vagrant + Ubuntu 16. None of the other solutions worked (my guest and host times are synced).
The only thing that worked for me was to comment out the following line from config/environments/development.rb:
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
Thought I would post this in case someone else gets to this page for a similar issue, as I did.
I have finally figured this out!
Here's an answer on rails tracker: https://github.com/rails/rails/issues/16678
If you use VirtualBox + NFS you must synchronize time between host and client due some changes in Rails 4.
Please check if you are really running the application in development mode, rather than production.
Also check you /config/environments/development.rb to see if cache classes is off:
config.cache_classes = false
This other post might help you.
Rails.application.reloader.reload!
found with method(:reload!).source in rails console.
(rails 6)

Altering the files of a rails application without restarting the server

I have a rails application running on production environment on a remote server. Though the application is ready for use it is far from finished so it is going to be subject to changes in views, controllers and css files.
Is there a way to update one of those files and make the server aware of the change without having to stop it? I tried cleaning the caches on both server and browser side in order to force the application to regenerate them with no success.
In the production.rb file I have these options
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 = true
config.assets.digest = true
config.log_level = :fatal
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
Thank you.
Have you considered versioning with Git? In a typical rails deployment, you would use something like git to version your software and then deploy with something like Capistrano so that you can push small changes up to a place like Github and then pull those changes in to your app via capistrano. There's a really great tutorial on how to do this on railscasts by Ryan Bates. Here's a link to Railscast #335.

Resources