Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the Rails server to refresh precompiled assets?

I'm testing my Rails 4 app in the production environment on my localhost:3000 using the built in Webrick server. When I run RAILS_ENV=production bundle exec rake assets:precompile the assets are rebuilt in public and the manifest is rebuilt, but the pages are still being served with the previous asset names.

Restarting the rails server makes the new assets appear. Is there a less extreme way to achieve this and how will this behave when I port this to my production server running Phusion Passenger. I really don't want to restart Apache to get my assets in gear.

like image 794
Peter Wooster Avatar asked Apr 20 '14 12:04

Peter Wooster


People also ask

What does rake assets Clean do?

The clean it removes the old versions of the precompiled assets while leaving the new assets in place. Show activity on this post. rake assets:clean removes compiled assets. It is run by cap deploy:assets:clean to remove compiled assets, generally from a remote server.

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 and pre-processors such as CoffeeScript, Sass, and ERB.

How do you Precompile an asset?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.


2 Answers

If you did not change the contents of assets, the precompiled version will be as same as the previous one. If you change it even a bit, the fingerprint will change and app will request for the new one only as you have set config.assets.digest = true.

Anyway another work around would be:

Just run:

rake assets:clean

and then,

rake assets:precompile

This makes everything in the asset pipeline to be rebuilt and serve freshly.

Rails automatically clear the cache for every individual file when its contents are edited.

If any of the above did not work, please try as below:

config.serve_static_assets = true in config/environments/production.rb

config.serve_static_assets configures Rails itself to serve static assets. Defaults to true, but in the production environment is turned off as the server software (e.g. Nginx or Apache) used to run the application should serve static assets instead. Unlike the default setting set this to true when running (absolutely not recommended!) or testing your app in production mode using WEBrick. Otherwise you won't be able use page caching and requests for files that exist regularly under the public directory will anyway hit your Rails app.

Ref: http://guides.rubyonrails.org/configuring.html#rails-general-configuration

Hope it helps :)

like image 107
Rajesh Omanakuttan Avatar answered Sep 18 '22 10:09

Rajesh Omanakuttan


We faced the same problem where the old assets were being served even after trying rake assets:clean or assets:clobber and eventually server reboot would resolve the issue. The culprit in our case was unicorn. While deploying our rails app using mina and mina-unicorn, we ran rake assets:clobber, then compiled assets and then restarted unicorn in the end. By doing this the unicorn master never gets stopped and continues to show old assets. So, we changed our mina deploy script and instead of restarting unicorn, we stopped unicorn and started it back. This resolved the issue. So, the key steps are

  1. Deploy application

  2. run rake assets:clobber

  3. run rake assets:precompile

  4. stop unicorn

  5. start unicorn

This kills the concept of zero downtime but this is a better solution than restarting the server.

I understand that you use passenger but this information can be helpful for others

like image 30
Bhavya Keniya Avatar answered Sep 20 '22 10:09

Bhavya Keniya