Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile assets automatically and serve them with nginx (development)

I'm working on a Rails app with a high number of assets, which sadly cannot be reduced. In production this is not a problem, but in development, ~20 asset requests per visited page cannot be quickly served by an application server (like webrick or Thin).

So I started using nginx in development for serving anything in public/assets. Note that nginx is purely a development facility - we don't intend to use it in production.

For it to work I just had to do two things:

  • Set config.assets.debug to false
  • run rake assets:precompile

Sadly there are two problems (the latter being the most important one) with my setup:

  • Every assets change requires manually running rake assets:precompile again
  • For the app server to pick up the newly-compiled assets, I have to restart it.

What is a correct nginx / Asset Pipeline setup which does not require a Rails server restart after precompilation?

Automatic compilation would also be welcome.

like image 862
deprecated Avatar asked Nov 27 '15 11:11

deprecated


2 Answers

This setup worked for me:

  • Include the nginx port in config.asset_host
  • config.assets.debug = false
  • config.assets.digest = true
  • config.assets.compile = true
  • before starting the Rails server, run rm -rf public/assets; rake tmp:clear tmp:cache:clear assets:clean assets:precompile
  • launch the Rails server
  • On every asset change, run rake assets:precompile again. Guard can take care of that.
like image 135
deprecated Avatar answered Oct 13 '22 00:10

deprecated


This may require a lot of effort but consider switching to gulp or grunt in order to compile assets. Using node js can speed up the process significantly (a lot of articles on it, here is an example one http://blog.carbonfive.com/2014/05/05/roll-your-own-asset-pipeline-with-gulp/). And what is also important assets may be compiled without server restart (process triggered on file change [hooks]). In project I'm involved one of our guys is trying to make this kind of switch, and from what he says I understand it's not a one-day task.

like image 35
djaszczurowski Avatar answered Oct 13 '22 02:10

djaszczurowski