Could somebody explain to me what the command rake assets:clean really does? Unfortunately the Rails Guides dont mention it. There is also the command rake assets:cleanup. Whats the difference?
Furthermore could somebody tell me when do I have to run rake assets:precompile in production. Do I run it on the server console after I deployed all my application files to my production server? Or do I precompile on my local machine and then do a deploy of all files?
Thanks all
Note: This answer is rails 3 specific. For rails 4 and later, look at other answers here.
If you precompile on your local machine, then you can commit these generated assets into the repository and proceed with deployment. No need to compile them on production machine.
But it introduces a problem: now when you change source files (coffescript / scss), the app won't pick up the changes, because it will serve precompiled files instead. rake assets:clean deletes these precompiled files.
In my projects assets are precompiled as a part of deployment. Capistrano makes it very easy.
Also, I never heard of rake assets:cleanup.
Run rake assets:clobber to actually clean the assets.
http://www.dixis.com/?p=735
Sergio's answer was completely correct in Rails 3. rake assets:clean deleted all assets that had been previously precompiled into the public/assets directory.
In Rails 4, you run rake assets:clobber to do the same thing.
If you run rake assets:precompile with the following config (by default turned on in staging and production):
# config/environments/production.rb
config.assets.digest = true
You compiled assets get timestamped. This means you can compile your new assets while leaving the old assets in place. You usually want to do this in production so you website will still access the old files while your running precompile to create your new files (because you've added new css/javascript). You now want to get rid of the old files that are no longer in use. The clean it removes the old versions of the precompiled assets while leaving the new assets in place.
rake assets:clean removes compiled assets. It is run by cap deploy:assets:clean to remove compiled assets, generally from a remote server.
cap deploy:clean removes old releases, generally from a remote server. It is not rake assets:clean
rake != cap
rake assets:clean is now run by cap deploy:cleanup_assets. Add require 'capistrano/rails/assets' to your Capfile and you get this cap-task. My capistrano version is v3.2.1.
clean up those untracked files with git clean -f for files and git clean -f -d for directories
Related
I've deployed a new version of a Rails 5 app on Heroku, running on cedar-14 stack. It didn't precompile while deploying, so I did heroku run rake assets:precompile manually. Still, I can see it includes old assets while requiring css and js files.
My files are in app/assets so it's not possible that directory isn't in assets compile path.
My config on application.rb and production.rb:
config.assets.compile = true
# I checked the environment variable, it responds to 'enabled',
# which would return true for the option.
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
# Which I changed to expire old assets.
config.assets.version='1.1'
I tried these, but they didn't work:
$ heroku restart
$ heroku run rake assets:precompile
$ heroku run rake assets:clobber
The weird thing with these is that they do not affect assets in heroku server, which I checked with $ heroku run ls public/assets. Even after $ rake assets:precompile, even though it says this:
WRITING /app/public/assets/application-{VERY_LONG_HASH}.js
WRITING /app/public/assets/application-{VERY_LONG_HASH}.js.gz
WRITING /app/public/assets/application-{VERY_LONG_HASH}.css
WRITING /app/public/assets/application-{VERY_LONG_HASH}.css.gz
when I peek with the $ heroku run ls public/assets, I still see old assets staying there.
EDIT: I solved it by deleting all the local assets in public/assets, recompiling them with $ rake assets:clean && rake assets:precompile and including these assets in my git repository. Here is one concern:
Shouldn't heroku be responsible of compiling my assets on the fly? I think I shouldn't be compiling my assets everytime I deploy my application. Thanks.
Run on local
RAILS_ENV=production bundle exec rake assets:precompile
Next git add .
Next got commit -m"assets precompile"
Next git push origin yourBranchName
Deploy on heroku and you are done
You need to commit your changes first.
Then execute git push heroku yourbranch:master
I read the docs but can't seem to understand if you have to run rake assets:precompile locally each time you change scss file or any other assets? Isn't there an automatic way to do it? One of the things I have noticed is that I forget to run it sometimes and my heroku changes do not appear. There must be a way to set it up automatically in rails?
If I change
config.assets.compile = false
to true, will do that it? Is a disadvantage of doing that?
You don't have to precompile your assets for Heroku to serve them. Heroku will precompile your assets automatically if you have not already precompiled assets locally. Read this heroku doc regarding the asset pipeline in Rails 3 (even if you are already using Rails 4). Then read this doc regarding the asset pipeline in Rails 4 on heroku.
Pay particular attention to this part:
If a public/assets/manifest.yml is detected in your app, Heroku will
assume you are handling asset compilation yourself and will not
attempt to compile your assets. Rails 4 uses a file called
public/assets/manifest-.json instead. On both versions you
can generate this file by running $ rake assets:precompile locally and
checking the resultant files into Git.
rake assets:precompile must be run for production environment. Do not need to run the command for the development environment. The command is used to collect all files into one and so be lighter to serve in production. Under development the styles they are wanted in the assets folder. After running the command, the styles are placed in the public folder.
If you forget to run rake assets:precompile Heroku should do it automatically. One reason it may not be is if you have checked your public folder into git as then during slug compilation Heroku will assume you precompiled your assets and will not do it for you.
Setting config.assets.compile = true can slow down your application by a lot, which is why it is only used in development.
Right now I have occasional issues with the Rails 4 asset pipeline that requires removing all assets and then recreating the assets.
During deployment, after pulling the latest from git, I refresh the assets by running, rake assets:clobber and then rake assets:precompile on my server. The problem is that during the time the assets are removed, the page gets served, not surprisingly, with no assets.
Is there anyway to force Rails to recompile all assets without having to run assets:clobber?
Here is what I usually do, when precompiling the assets.
// At a terminal, remove all assets from the file system
$ rm -rf public/assets
// Precompile the assets
$ rake assets:precompile
Always worked for me.
First run rake assets:precompile then run rake assets:clean. That will only remove old assets. https://github.com/rails/sprockets-rails/blob/master/README.md#rake-task
I use Capistrano for deploying my Rails 4 app. Precompiling assets on the server is very slow so I manually precompile it in development using RAILS_ENV=production bundle exec rake assets:precompile and add it to my git repo.
The problem I am facing is that the assets:precompile task does not remove the old assets. So, I have multiple copies of application-<manifest code>.css and application-<manifest-code>.js in my public assets.
Capistrano has deploy:assets:clean_expired task for this though I am not sure how can I replicate it in my case. I have seen other approaches which use capistrano task to precompile assets and rsync them up. This link - http://keighl.com/post/fast-rails-assets-precompile-capistrano/ - has an approach for Rails 3 using the turbo-sprockets-rails3 gem.
There are similar questions here - Do I have to run rake assets:clean before rake assets:precompile? and Confusion about rake assets:clean / cleanup on the asset pipeline in rails
How can I adapt it for Rails 4?
This is working well for me now - https://gist.github.com/mishrarohit/7802260 It uses git revision history to check if any changes have been made to assets. It will be better if we can use the manifest for this.
I was compiling my asset pipeline for my production environment and it did for all my environments. How can I uncompile my asset pipeline for my development environment?
I have checked my config/development environment and cannot find a fix.
Thanks in advance for any help...
To remove precompiled assets use:
rake assets:clean
What this basically does is remove the public/assets directory. You may need to include the RAILS_ENV variable if you need to run it for a certain environment.
Try using
rake assets:clobber
worked for me in rails 4
When you run the compile task locally (on your development machine) the assets are compiled in the Rails production environment, but are written to the public folder.
This means that even when you run in development mode it'll use the compiled assets instead of sending requests to the pipeline. This is normal behavor - requests only go to the pipeline if the file does not exists in public/assets.
The compile task should generally only be used when deploying, and on the remote (production) machine.
If you have compiled locally, you can delete all the files in the public/assets folder and development will behave as before. If you checked these files into source control you'll need to remove them.
Once removed things should work fine.
s
One final tip: if this is an upgraded app check your config settings against those in the last section of the Rails asset pipeline guide.
For Rails 5:
$ RAILS_ENV=development bin/rake assets:clobber