How to Speed Up Assets Precompilation Time in Ruby on Rails

Photo of Marcin Brzeziński

Marcin Brzeziński

Updated Sep 28, 2023 • 7 min read
niklas-rhose-14302

Every Rails developer knows that loading assets can be really time-consuming. Here are some useful tips on how to speed it up.

Tips

  • 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-js keeps translations in a separate file. Configure only the required languages. Otherwise, the translation 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)
  • Remove gem 'therubyracer' from Gemfile, because this gem uses a lot of memory. Make sure that the latest version of Node is installed instead.
    https://devcenter.heroku.com/articles/rails-asset-pipeline#therubyracer
  • Do not use require require_tree and require_self in your SASS/SCSS files. They are very primitive and do not work well with Sass files. Instead, use Sass's native @import directive, which sass-rails has customised to integrate with the conventions of your Rails projects.
    https://github.com/rails/sass-rails#important-note
  • In your SASS/SCSS files use @import directive carefully. Avoid importing all the package assets as @import 'compass'; when you can only do @import 'compass/css3/flexbox'.
  • Avoid using the require_tree . directive in your JS/COFFEE manifests. Let's imagine a scenario in which your app has an admin panel with separate assets:
    //** assets/javascripts/admin/admin.js
    
    //= require admin/tab.js
    //= ...
    //** assets/javascripts/application.js
    
    //= require 'something'
    //= require_tree . // BAD! it requires admin assets as well
  • 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

Summary

The above tips can help you retrieve your compiled assets faster! As a proof of concept, please take a look at cintrzyk/sprockets-tips, where you can find a sample Rails app with some more details.

Rails 5.1 introduced some other ways of managing assets. I highly recommend you use Yarn with Webpack, it's worth it, believe me! What you can gain is the management of asset dependencies, more efficient compilation process, hot-reloading of your code without page refresh, ES6 support, PostCSS, and many more!

Photo of Marcin Brzeziński

More posts by this author

Marcin Brzeziński

A perpetually smiling & cheerful guy! For as long as he can remember, computer has been his hobby,...
Lost with AI?  Get the most important news weekly, straight to your inbox, curated by our CEO  Subscribe to AI'm Informed

Read more on our Blog

Check out the knowledge base collected and distilled by experienced professionals.

We're Netguru

At Netguru we specialize in designing, building, shipping and scaling beautiful, usable products with blazing-fast efficiency.

Let's talk business