How to avoid using precompiled assets in development mode?

I prefer not to concatenate JavaScript files in design mode, but to serve them as separate files. Therefore, 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 enable JavaScript using <%= javascript_include_tag "reviews" %> in my layout. The generated page correctly references the three scripts separately, and reviews.js is essentially empty. So far so good.

Now that I am precompiling my assets for production with rake assets:precompile , the three JavaScript files are merged into reviews.js . This is normal for production, but now in development mode, the concatenated reviews.js serves additionally for two separate files.

Of course, this leads to all kinds of unpleasant development errors, because now the contents of foo.js and bar.js executed twice, one of them in a potentially earlier version in reviews.js .

How can I make sure that Rails does not use precompiled assets in development mode?

+48
ruby-on-rails asset-pipeline
Nov 04 '11 at 17:33
source share
4 answers

It looks like you are precompiling locally. Since the files exist at the expected location, they are served by your dev server and requests do not go to Sprockets.

The only way to stop this is to delete the compiled files.

Usually you do not need to compile locally. It is expected that in almost all cases, the precompilation task will be performed during application deployment. There is a Capistrano recipe for this on the asset pipeline manual page.

If you need these files to be locally linked to your repo, you can use a branch to avoid the problem. Reserve the main branch for production code and create a second branch for dev. Just compile and transfer the assets to master. When you switch to dev, they will disappear. Combine dev with master if necessary.

Edit: make sure you force the browser to update (control + F5), or you can find the old assets used in the browser cache!

+52
Nov 04 '11 at 18:37
source share

In config/environments/development.rb install:

 config.assets.prefix = "/assets_dev" 

so in development mode, Rails will look there (but it won’t find anything, since you won’t compile the assets in development (this is really what you are trying to do, not compile the assets)).

When precompiling for production, use

 RAILS_ENV=production rake assets:precompile 

therefore, it compiles to the default folder, public/assets .

+79
Jul 20 2018-12-21T00:
source share

in config/environments/development.rb install:

 config.serve_static_assets = false 

and no files from /public will be served

+14
Oct 06
source share

I tried this and it worked. rake assets:precompile RAILS_ENV=production

I noticed that the new version of the asset pipeline does this when you run rake assets:precompile does rake assets:precompile:all

+1
Nov 04 2018-11-11T00:
source share



All Articles