Rails 3.1.1 资产管道 Heroku 缓存问题

发布于 2024-12-12 03:29:56 字数 817 浏览 0 评论 0原文

简而言之,问题在于,在开发模式下,我们会对 CSS 或 JS 文件进行更改,但总是会获得这些文件的缓存/旧版本。我所做的一切都没有任何效果。我检查了配置数十次,并尝试了配置值的每种组合,但总是得到相同的结果:陈旧/缓存的文件。我必须实际在生产模式下运行,并在每次更改后重新启动服务器进行测试。

我花了几天时间为这个问题绞尽脑汁,查看了资产管道上的数十个 stackoverflow 问题,但从未找到解决该问题的方法,因此我想将其发布在这里以供后代使用。

我们使用 Heroku 并预编译我们的资产,因为 Heroku 无法为我们预编译(我们还使用了 devise,这显然是 Heroku 预编译失败的原因)。因此,为了将我们的预编译资产推送到 Heroku,我们必须将它们签入 git。

问题来了

当我们升级到 Rails 3.1.1 时,资产预编译生成的文件名称中包含或不包含 MD5 哈希值。我没有想太多,然后继续检查所有这些文件,以便我可以推送到 heroku。一段时间后,我注意到开发模式下缓存结果的问题。没有 MD5 哈希值的预编译和签入资产是从 /public/assets 作为静态文件提供的,这使我们无法看到在 /app/assets 中所做的任何更改。

最终意识到这一点后,我运行了 git rm /public/assets ,一切又恢复正常了。因此,要点是:小心将资产检查到 git 中!

将其转化为一个问题:其他人是如何做到这一点的?我错过了一些明显的东西吗?我真正想要的是 Heroku 为我预编译我的资产,但它因我收集的数据库连接错误而失败,这是因为设计。我曾希望 Rails 3.1.1 能够解决这个问题,但事实并非如此。

The problem in a nutshell is that in development mode we'd make changes to CSS or JS files but would always get cached/old versions of these files. Nothing I did had any effect. I checked configuration dozens of times and tried every combination of config values but always kept getting the same results: stale/cached files. I had to actually run in production mode and restart the server after every change to test.

I spent days tearing my hair out over this issue, looked at dozens of stackoverflow questions on the asset pipeline but never found one that addressed it so I thought I'd post it here for posterity.

We use Heroku and precompile our assets because Heroku fails to precompile for us (we also use devise which apparently is the cause of the heroku precompilation failure). So in order to push our precompiled assets up to Heroku we have to check them in to git.

Here's the problem.

When we upgraded to Rails 3.1.1 asset precompilation produced files both with and without the MD5 hash in the name. I didn't think much of this and went ahead and checked all these files in so I could push to heroku. Sometime later I noticed the problem with cached results in development mode. The precompiled and checked in assets without the MD5 hashes were being served from /public/assets as static files which prevented us from seeing any changes we were making in /app/assets.

After finally realizing this I ran git rm /public/assets and everything works again. So the takeaway is: Be careful checking assets into git!

To turn this into a question: how do others do this? Am I missing something obvious? What I'd really like is for Heroku to precompile my assets for me but it is failing with a db connection error that I gather is because of devise. I had hoped Rails 3.1.1 fixed this but it didn't.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月下凄凉 2024-12-19 03:29:57

您在 github 上检查过这个设计问题吗?具体来说,何塞·瓦利姆说

Rails 3.1.1 Final 有一个名为
config.assets.initialize_on_precompile。如果将其设置为 false,则
应该很好,但它不允许您访问模型信息
您的资产(您可能无论如何都不应该这样做)。

也许这将允许您在 Heroku 上进行预编译。

Have you checked out this devise issue on github? Specifically Jose Valim says

Rails 3.1.1 final has a method called
config.assets.initialize_on_precompile. If you set it to false, you
should be good but it won't allow you to access model information on
your assets (which you probably shouldn't anyway).

Maybe this will allow the precompile to happen on Heroku for you.

沒落の蓅哖 2024-12-19 03:29:57

资产预编译不起作用的原因很可能是 Heroku ENV 变量不存在于 slug 编译(部署)中,如下所述:

http://devcenter.heroku.com/articles/rails31_heroku_cedar

有一种(实验性)方法可以启用正是出于这个原因,部署期间的 ENV 变量可在此处查找信息:

http://devcenter。 heroku.com/articles/labs-user-env-compile

希望这会有所帮助。

The reason the asset precompilation does not work could well be, that the Heroku ENV vars are not present on slug compilation (deploy) as stated here:

http://devcenter.heroku.com/articles/rails31_heroku_cedar

There is an (experimental) way to enable the ENV vars during deploy for exactly this reason, find information here:

http://devcenter.heroku.com/articles/labs-user-env-compile

Hope this helps.

红颜悴 2024-12-19 03:29:57

查看 Heroku 的本指南。它概述了部署 Rails 3.1 应用程序的 3 种方法。其中两个不需要本地预编译。

Check this guide from Heroku. It outlines the 3 ways to deploy Rails 3.1 apps. Two of these do not required local precompilation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文