Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 5 - config.assets.compile should be true - why?

I am developing Rails 5 application and use assets pipeline. It work well in development mode, but if I try to run it in production mode, it can't load images & styles correctly. I checked and found that it is because

config.assets.compile = false

in config/environments/production.rb

Unless I set it true, it doesn't work at all. I know live compilation isn't good for production, what is solution?

like image 229
Alex Avatar asked Sep 16 '16 19:09

Alex


People also ask

What does rails assets Precompile do?

The Rails asset pipeline provides an assets:precompile rake task to allow assets to be compiled and cached up front rather than compiled every time the app boots. There are two ways you can use the asset pipeline on Heroku. Compiling assets locally. Compiling assets during slug compilation.

How does rails asset pipeline work?

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages such as CoffeeScript, Sass and ERB. Prior to Rails 3.1 these features were added through third-party Ruby libraries such as Jammit and Sprockets.


1 Answers

There are two options related to serving assets within a Rails server:

Asset compilation

config.assets.compile = true

refers to asset compilation. That is, whether Rails should recompile the assets when it detects that a new version of the source assets is there. In development, you want to have it set to true, so that your styles get compiled when you edit the css files. With the next request, Rails will automatically recompile the assets. On production, you usually want to set it to false and handle asset compilation during deployment. For this, you have to run

RAILS_ENV=production bin/rails assets:precompile

Usually, if you deploy using Capistrano, it takes care of that.

Asset serving

The second option related to assets is

config.public_file_server.enabled

This describes whether it is Rails that should serve the compiled files from the public/assets directory. In development, you want that, so it's true by default. In production, you usually don't want to fire up your web server to serve the logo image or a css file, so you probably compile the assets and then host them separately (for example, on a CDN like cloudfront). If you still want them to be served in production, you can launch Rails with:

RAILS_SERVE_STATIC_FILES=true RAILS_ENV=production bin/rails server
like image 199
Ivan Zarea Avatar answered Oct 05 '22 01:10

Ivan Zarea