How to avoid precompiled assets being served in development mode?
I prefer not to concatenate JavaScript files in development mode, but serve them as individual files. So I configured:
development.rb:
config.assets.compress = false
config.assets.debug = true
config.assets.compile = true
In my /app/assets/javascript directory I have:
- reviews.js
- reviews/
- foo.js
- bar.js
reviews.js:
//= require jquery
//= require jquery_ujs
//= require_tree ./reviews
I include the JavaScript using <%= javascript_include_tag "reviews" %>
in my layout. The generated page correctly references the three scripts individually and reviews.js
is essentially empty. So far so good.
Now when I precompile my assets for production using rake assets:precompile
the three JavaScript files are concatenated into reviews.js
. This is all fine for production but now, in development mode, the concatenated reviews.js
is served in addition to the two individual files.
Of course, this leads to all kinds of nasty bugs when developing because now, the content of foo.js
and bar.js
is served twice, one of them in a potentially older version in reviews.js
.
How can I make sure Rails doesn't use the precompiled assets in development mode?
In config/environments/development.rb
set:
config.assets.prefix = "/assets_dev"
so that in development mode Rails will look there (but it will not find anything, as you will not compile assets in development (this is indeed what you are trying to do -- not compile assets)).
When precompiling for production, use
RAILS_ENV=production rake assets:precompile
so it compiles into the default assets folder, public/assets
.
It sounds like you are precompiling locally. Because the files exist in the expected location they are being served by your dev server, and the requests are not going to Sprockets.
The only way to stop this is delete the compiled files.
Normally you do not need to compile locally. It is expected that in almost all cases the precompile task will be run during deployment of the app. There is a Capistrano recipe for this on the asset pipeline guide page.
If you do need to have those files locally committed to your repo you could use a branch to avoid the problem. Reserve your master branch for production code, and make a second branch for dev. Only compile and commit assets on master. When you switch to dev, they will be gone. Merge dev into master as required.
Edit: Make sure you force your browser to update (control + F5) or you may find the old assets used from the browser cache!
in config/environments/development.rb
set:
config.serve_static_assets = false
and no files from /public
will be served
I tried this and it worked. rake assets:precompile RAILS_ENV=production
I observed that the new version of assets pipeline does this when you run rake assets:precompile
does rake assets:precompile:all