Please help me understand what heroku run rake assets:precompile
exactly does. Ever since I began working on ruby on rails, I would always run these three commands before I push to github and heroku:
bundle exec rake assets:precompile
RAILS_ENV=production bundle exec rake assets:precompile
After I push to heroku, I would run:
heroku run rake assets:precompile
However, when I tried to run it after my last push to heroku, I got a bunch of the same errors on different files. For example:
Warning. Error encountered while saving cache ... can't dump anonymous class ...
To see if I can fix this, I ran
heroku run rake assets:clean
and then heroku run rake assets:precompile
again. The thing is that everything is working fine, but I just feel iffy having all these warnings/errors. Please help me understand. Thank you!
If you have any .jpeg make sure to change them to .jpg before compiling. The compile step will do it for you but your image_tags will be off if you're specifying your files with extensions.
For anyone having trouble figuring out why Heroku won't compile your assets automatically:
I found a
sprockets-manifest-*.json
and Heroku started compiling my assets automatically after I deleted this file.In my case, this file was generated by the script
rails_composer
.You should be running this command before you push to Heroku as it **pre**compiles your assets. Heroku will automatically run this command if you are missing a manifast.yml file. After running
rake assets:precompile
locally you can commit all of the changes and then push to Heroku.Precompile
To give you some clearer definitions -
Heroku
isn't the only system which requires you to "precompile" your assets. Asset precompilation is a pre-requisite of most Rails production environments, as it allows you to serve static assets (files) - perfect for speed & efficiencyHere's what the Rails documentation says about it:
The reason why Heroku wants you to precompile your assets is because the Heroku environment is designed for speed & efficiency; and hence does not want to expend CPU power on compiling the assests for each request / instanace of your app
This means you have to either precompile the assets yourself, or let the Heroku buildpacks sort that out for you
Heroku
As mentioned by
CWitty
, you'll want to make sure you compile your assets locally. And whilst I'm not sure about the errors you've received, I do know one thing: precompilation populates thepublic/assets
folderThis means if you precompile locally before submitting to Heroku, you'll have all your latest assets present in your
public/assets
directory before you try and run the application on HerokuAlthough Heroku does perform precompilation as part of the build process, you'll be much safer (from an exception perspective) by precompiling locally:
This will give you the ability to populate the
public/assets
folder, allowing you to then push to Heroku without any issues