Heroku is serving old assets for Rails 5 application - ruby-on-rails

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

Related

Removing Precompiled Assets in Rails before Push to Heroku

I have a Heroku app on Rails 4.0.8.
At some point early on, I realized that my CSS and JS changes wouldn't push to Heroku unless I ran rake assets:precompile and committed the new asset files before pushing (git push heroku master).
I've been doing those asset precompiles for about 14 months now, and I've amassed over 48mb in what appear to be old precompiled assets. However, when I remove them from public/assets manually, my site loses all of its CSS and JS.
How do I remove the old precompiled assets and get Heroku to start compiling on push?
Follow these steps:
rake assets:clean
git add .
git commit -m commit_name
git push heroku branch_name
This will clean your assets locally + pushing on heroku would precompile the assets.
Try the following code.
rake assets:clean

How to reset Heroku Rails4 asset pipeline cache

According to Heroku documentation:
Heroku now caches 50mb worth of tmp/cache/assets which is a cache
directory for the asset pipeline to store intermediate files. This
means that future asset compilations will be faster due to not having
to recalculate these files.
My question is how do I manually reset or delete this cache so that all of my assets have to be precompiled again? I tried heroku run console and Rails.cache.clear but it did not work. The reason I want to reset the cache is I have changed the config.action_controller.asset_host in my production.rb file but Heroku is not picking up on the change because of the cache.
To purge Heroku's asset cache, you need the Heroku Repo plugin to the Heroku toolbelt. Install that, then use the command
heroku repo:purge_cache
Deploy after purging the cache.
Hopefully this helps. To get changes to Heroku in development I run rake assets:clean and then rake assets:precompile RAILS_ENV=production --trace before committing and pushing to Heroku
For cleaning assets, run:
rake assets:clobber
I had this problem, then i realized I forgot to configure to serve static assets on the production env, inside config/enviroments/production.rb
config. serve_static_files = true
As this Stack Overflow answer explains, config.assets.version exists for this purpose.
Most likely, you'll find it in config/application.rb or config/initializers/assets.rb. Change it to any value you want to break the cache next time you deploy to any environment.

Safely recompiling all assets on Rails 4 deployment?

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

Getting Rail4 asset-pipeline to heroku

My css & images are not making it to heroku. But I did what their tutorial said to do.
RAILS_ENV=production bundle exec rake assets:precompile
git add public/assets
git commit -m "assets added"
Then I heroku push and get
-----> Preparing app for Rails asset pipeline
Detected manifest file, assuming assets were compiled locally
so my assets pushed but there is still no css images or anything on my heroku app. Any ideas?
Try don't send your assets to heroku.
Remove public/assets from git:
git rm -r public/assets
git commit -m "Remove assets"
If still not working, on config/environments/production.rb set:
config.assets.compile = true
You also can try, on config/environments/production.rb set:
config.serve_static_assets = true
Heroku needs to have static assets, so each time you push to Heroku & change your assets, you should use this command:
rake assets:precompile

Confusion about rake assets:clean / cleanup on the asset pipeline in rails

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

Resources