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 comma
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 & efficiency
Here's what the Rails documentation says about it:
In the production environment Sprockets uses the fingerprinting scheme outlined above. By default Rails assumes assets have been precompiled and will be served as static assets by your web server.
During the precompilation phase an MD5 is generated from the contents of the compiled files, and inserted into the filenames as they are written to disc. These fingerprinted names are used by the Rails helpers in place of the manifest name.
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 the public/assets
folder
This 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 Heroku
Although Heroku does perform precompilation as part of the build process, you'll be much safer (from an exception perspective) by precompiling locally:
$ rake assets:precompile RAILS_ENV=production
This will give you the ability to populate the public/assets
folder, allowing you to then push to Heroku without any issues
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.
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.
For anyone having trouble figuring out why Heroku won't compile your assets automatically:
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.
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
.