Ruby on Rails: heroku run rake assets:precompile

后端 未结 4 2105
青春惊慌失措
青春惊慌失措 2020-12-29 10:46

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

4条回答
  •  佛祖请我去吃肉
    2020-12-29 11:12

    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

提交回复
热议问题