Ruby on Rails:heroku 运行 rake 资产:预编译 [英] Ruby on Rails: heroku run rake assets:precompile

查看:28
本文介绍了Ruby on Rails:heroku 运行 rake 资产:预编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我理解 heroku run rake assets:precompile 究竟做了什么.自从我开始在 ruby​​ on rails 上工作以来,我总是在推送到 github 和 heroku 之前运行这三个命令:

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

在我推送到 heroku 之后,我会运行:

After I push to heroku, I would run:

heroku run rake assets:precompile

但是,当我在上次推送到 heroku 后尝试运行它时,我在不同的文件上遇到了一堆相同的错误.例如:

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:

警告.保存缓存时遇到错误...无法转储匿名类...

为了看看我能不能解决这个问题,我跑了

To see if I can fix this, I ran

heroku run rake assets:clean 然后 heroku run rake assets:precompile 再次.问题是一切正常,但我只是觉得有所有这些警告/错误.请帮我理解.谢谢!

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!

推荐答案

预编译

为了给您一些更清晰的定义 - Heroku 并不是唯一需要您预编译"资产的系统.资产预编译是大多数 Rails 生产环境的先决条件,因为它允许您提供静态资产(文件) - 非常适合速度 &效率

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

以下是 Rails 文档 对此的说明:

Here's what the Rails documentation says about it:

在生产环境中,Sprockets 使用指纹方案以上概述.默认情况下,Rails 假定资产已经预编译并将由您的网络服务器作为静态资产提供.

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.

在预编译阶段从内容生成MD5编译后的文件,并按原样插入文件名写入光盘.Rails 使用这些指纹名称代替清单名称的助手.

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.

Heroku 之所以要你预编译你的资产,是因为 Heroku 环境是为速度& 而设计的.效率;因此不想消耗 CPU 资源来为您的应用程序的每个请求/实例编译资产

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

这意味着您必须自己预编译资产,或者让 Heroku buildpacks 帮你解决

This means you have to either precompile the assets yourself, or let the Heroku buildpacks sort that out for you

Heroku

正如CWitty 所提到的,您需要确保在本地编译您的资产.虽然我不确定您收到的错误,但我知道一件事:预编译填充 public/assets 文件夹

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

这意味着如果你在本地提交到 Heroku 之前预编译,你将在你的 public/assets 目录中之前拥有所有最新的资产> 您尝试在 Heroku 上运行该应用程序

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

尽管 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

这将使您能够填充 public/assets 文件夹,然后您可以毫无问题地推送到 Heroku

This will give you the ability to populate the public/assets folder, allowing you to then push to Heroku without any issues

这篇关于Ruby on Rails:heroku 运行 rake 资产:预编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆