Rails 3.1.1 资产管道 Heroku 缓存问题
简而言之,问题在于,在开发模式下,我们会对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在 github 上检查过这个设计问题吗?具体来说,何塞·瓦利姆说
也许这将允许您在 Heroku 上进行预编译。
Have you checked out this devise issue on github? Specifically Jose Valim says
Maybe this will allow the precompile to happen on Heroku for you.
资产预编译不起作用的原因很可能是 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.
查看 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.