10
votes

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.

2
Have you tried the other Rails servers like Unicorn and Puma? You could alway use Guard to monitor for file changes and run rake assets:precompile and restart your server.max
+1 for guard. Unicorn/Puma are not specialized HTTP servers (particularly Unicorn which assumes fast clients by design) so I'd prefer to use nginx for assets if possible.deprecated
This sound kind of like you have outgrown the assets pipeline - Rails is not really built to serve static assets in development - it's supposed to be done at deploytime. So restarting the server is going to be pretty clunky. So you could look at using for example grunt or broccolli to boil down your assets instead.max
@vemv, it is listed as being for production, but maybe you can hack the config by combining guides.rubyonrails.org/asset_pipeline.html#live-compilation and stackoverflow.com/questions/14559615/…?benjamin
@vemv, + probably clearing the cache folder of nginx in addition.benjamin

2 Answers

3
votes

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.
1
votes

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.