How to Speed Up Assets Precompilation Time in Ruby on Rails
Every Rails developer knows that loading assets can be really time-consuming. Here are some useful tips on how to speed it up.
- Do not bundle everything into one large file. Use CDN for tools like CKeditor and require it only on those pages where it is needed.
I18n-jskeeps translations in a separate file. Configure only the required languages. O
therwise, thetranslation file may inflate very fast along with every new library. If you configure only the required languages, you'll avoid large compiled files, which means faster compilation and page load.
config.i18n.available_locales = %i(en)
, becausethis gem uses a lot of memory. Make sure that the latest version of Node is installed instead.
- Do not use
inyour SASS/SCSS files. They are very primitive and do not work well with Sass files. Instead, use Sass's native
directive,which sass-rails has customised to integrate with the conventions of your Rails projects.
- In your SASS/SCSS files
directivecarefully. Avoid importing all the package assets as
whenyou can only do
in your JS/COFFEE manifests. Let's imagine a directive scenarioin which your app has an admin panel with separate assets:
- Check logs while compiling your assets. It's easy to override the default logger and see what's going on under the hood by accessing the DEBUG mode.
# /lib/tasks/assets.rake require 'sprockets/rails/task' Sprockets::Rails::Task.new(Rails.application) do |t| t.logger = Logger.new(STDOUT) end
The above tips can help you retrieve your compiled assets faster! As a proof of concept, please take a look at
Rails 5.1 introduced some other ways of managing assets. I highly recommend you