Ruby on Rails: heroku run rake assets:precompile

房东的猫 提交于 2019-12-30 01:08:08

问题


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!


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/25253864/ruby-on-rails-heroku-run-rake-assetsprecompile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!