rake assets:precompile is slooooow. Any way to speed it up? - ruby-on-rails

I have a Rails 3.2 app running on Heroku, and it uses CKEditor. Now, CKEditor is a pretty large collection of files and folders, and is probably the biggest contributor to the time it takes to precompile assets. A regular push to Heroku takes well over a minute on the assets:precompile step.
So I now precompile locally, and only when I've made edits, before I push to Heroku, to shorten deploy times. However, my poor old Windows laptop easily breaks 15 minutes for rake assets:precompile. This makes it a huge pain to make minor edits or additions to js or css files.
I do have config.assets.initialize_on_precompile = false as required by Heroku docs. But I'm pretty sure the real time hog is compression, i.e. Uglifier.
Does anyone have a suggestion to how I can remedy this? Am I simply doing it wrong? Is there a way to only compile changed files? Could/should I move CKEditor directly to the public dir to avoid precompiling?

You can try to load the assets only on the changed files which would speed up compiling process by a huge margin. You can easily do so using turbo-sprockets-gem.
https://github.com/ndbroadbent/turbo-sprockets-rails3
The documentation is pretty straight forward. Hope this helps.

Related

Heroku - assets loading partially

I have just deployed my app to heroku and I found a weird behavior. Every single image of my website was precompiled (as they are all displaying properly), except two of them.
I am using Rails 5.0.0.1 and I did not know about heroku precompilation until now.
What might be the cause of this behavior?
Can I run this compliation directly on heroku or do I have to add some lines of code to make it work?

Image path production reference is not working in Rails 4

Please someone help, spent hours on this and it's driving me mad.
I'm on rails 4, using apache/passenger if that helps. Also I ran: "rake assets:precompile" and restarted apache.
I'm using this in my style sheet:
background-image: url(image-path('pretty-background-image.jpg'));
In production this is outputted in the css file:
background-image: url("/assets/pretty-background-image.jpg");
From this ticket and a lot of messing around, I understand something like this SHOULD be outputted:
background-image: url("/assets/pretty-background-image-8b313354987c309e3cd76eabdb376c1e.jpg");
But it's not. I'm having the same issues with fonts when trying to use:
asset-url('font-name.eot')
All other images within html.erb files are rendering fine (with the extra string of numbers and letters at the end) but nothing inside of my style sheets is working correctly.
rails 4 has incompatible changes when it comes to the asset pipeline.
i think that those changes are good, because they remove a lot of magic, that rails did to keep things backward compatible.
one of those points that were really annoying in rails 3 was the way rake assets:precompile worked.
it ALWAYS tried to run the task with the production environment. and it ran twice to generate assets with fingerprints and without them.
nowadays you have to pass the environment to the rake task like RAILS_ENV=production rake assets:precompile when you want to generate assets in production.
the new task does not generate any assets without fingerprints. if you want to have those, you need to build some custom stuff (symlinking stuff from manifest.yml within your capistrano task etc) for that.

Rails: Clarifying purpose of precompile:assets

I'm trying to understand what exactly precompiling:assets does, because I've realized that for my last project my CSS would never update when I pushed my app to heroku unless I typed bundle exec rake assets:precompile, but this only started happening towards the end, so I believe I probably added something to the config file.
I'm currently trying to understand caching, which made me think about precompile:assets. Is precompile:assets similar to caching by pre-loading the assets to the web server so that those assets aren't loaded directly from the Rails stack? This is for performance purposes right?
Caching is a related, but separate topic.
The purpose of compiling assets includes the combining and minimizing of assets, e.g. javascript that is all on 1 line with 1 letter variables, as opposed to the originals which are used in development mode and let you debug there using the original source code.
You can find everything you need to know in the Asset Pipeline Rails Guide.

Recompiling Sass assets in production

I'm working on a rails app where we want to allow the user to use an admin tool to create new themes. The admin tool is a separate application and communicates with our main application through a database. My problem is that I've written custom Sass extensions to load our data into our style sheets, but once that is done, I am unable to recompile the assets in our production environment.
So far I've seen two possibilities for this:
1.Increment the version of config.assets.version. So I have this code:
MyApp::Application.assets.version =
(MyApp::Application.config.assets.version.to_i + .1)
From what I've read incrementing this should cause the assets to recompile, but it seems to only work when it is incremented by hand and the server is restarted.
2.Create a compiler and tell it to clean up the old assets and recompile them:
compiler = Compass::Compiler.new(
Rails.root.to_s,
Compass.configuration.sass_path,
Compass.configuration.css_path,
{:sass => Compass.sass_engine_options} )
compiler.clean!
compiler.run
With this method, however, I run into the problem that the Sprockets::Index.expire_index! method raises an error when I try to create a new compiler.
Yes, I do understand that I can set the assets to recompile on every request, but the performance hit is not something we want. Also, since this is a theme, the data should not be changing often, so we only need to recompile when the admin chooses to save the new theme.
So, finally, my question is: Are there any other possible methods to do what I want? Or am I going down the right path, and if so, where am I going wrong?
EDIT:
I forgot to mention that since we are using Sass functions to change the values of the stylesheets, even if I do turn on the option to compile in production, it won't work. Since the actual stylesheets will never change.
Rails has a Rake task that does asset compilation for you. You should run it once every time you deploy your application to your production environment.
rake assets:precompile
The compiled assets are output to public/assets. For more details, check out the Rails Asset Pipeline Guide.

Am I doing something wrong with the asset pipeline?

Since "upgrading" to Rails 3.1 my app is really slow in development mode
(> 30 per request)
I have a lot of images and it seems most of this time-delay is the asset pipeline processing each GET request for each image.
Don't have this problem in Staging or Production mode as the assets are cached etc.
Is there something I haven't been told or is this how we're expected to work now?
Requests can be slow if you have gems or portions of your app that load code at the start of each request - or that merely reference portions of your app, causing much of it to be loaded. For most of these, the autoloader is the prime cause of request delay.
The rails auto-reloader deletes any autoloadable classes/modules/etc at the start of each request, and can cause significant delays at the beginning of each request as Rails reloads all the source files it needs.
You might want to try playing with https://github.com/wavii/rails-dev-tweaks, which gives you granular control over which requests cause the auto-reloader to kick in. This really isn't a fix of the root cause (something is doing extra work at the start of every request that it probably doesn't need to be doing) - but it certainly mitigates most such issues.
In the meantime:
cp -R app/assets/images public/assets
really helps
remember to add public/assets/* to .gitignore
If your app is slow it is because of your app or one of the gems that you use. I had similar issue and it looks like Mongoid was the case more you can read here:
http://martinciu.com/2011/06/rails-3-1-and-slow-asset-pipeline.html
You can use a rake task:
rake assets:precompile RAILS_ENV=development RAILS_ASSETS_NONDIGEST=true
And as it was mentioned above, don't forget to include public/assets/* to .gitignore

Resources