如果 google.load 失败怎么办?
好奇是否有一种简单的方法可以在 google.load() 上实现故障安全
谷歌无法交付文件但我可以的可能性很小,但我认为拥有自己的服务器能够能够会很有趣在请求失败或超时或其他情况下提供故障转移。
有什么想法吗?
我正在使用 MS Visual Studio 2008 / C#
Curious if there was a simple way to have a fail-safe on google.load()
The likely hood that google can't deliver the file but I can is pretty slim but I thought it might be interesting to have my own server be able to provide a failover in case the request bombs or timesout or something.
any thoughts?
I'm using MS visual studio 2008 / C#
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种情况发生的可能性很小,很可能属于过度思考的范畴。实际上,您自己的服务器比 Google 的服务器离线的可能性要大得多。除非您定期在网站上处理价值数百万美元的交易,否则这将是不必要的(而且可能不可靠的)工程。
google.load
有一个回调参数,但是,此参数会在成功而不是失败时调用。我不确定失败时会发生什么,因为我从来无法加载 JS API,但无法加载 Google 托管的其余 JS 文件。这就是一个技巧。如果您首先可以调用google.load
,则可以非常确定 Google 服务器是可访问的。如果由于某种疯狂的原因,Google 服务器关闭了,那么google
对象将永远不会首先被定义。我不知道是否有任何官方记录的方法来检测失败,但我想您可以检测google
对象是否已定义,然后自行加载您自己的托管版本的脚本。如果您确实沿着这条路线走,您可以通过将
google.com
指向主机文件中的诸如0.0.0.0
之类的虚假内容来测试它。如果此时 onload 事件仍然存在问题,我不会感到惊讶,因为该事件可能在事件处理程序在故障转移情况下注册之前就已被触发。The chances of this are small enough that this probably falls into the category of over-thinking things. Realistically speaking, your own server is vastly more likely to go offline than Google's setup. Unless you're dealing with millions of dollars worth of transactions on your site on a regular basis, this is going to be an unnecessary (and probably unreliable) bit of engineering.
There is a callback parameter for
google.load
, however, this gets called on success rather than failure. I'm not sure what happens on failure because I've never been able to load the JS API but not the rest of the Google-hosted JS files. And that's kind of the trick. If you can callgoogle.load
in the first place, you can be pretty sure that the Google servers are reachable. If, for some insane reason, the Google servers are down, then thegoogle
object will never get defined in the first place. I don't know if there's any officially documented way to detect failure, but I suppose you could just detect if thegoogle
object is defined and load your own hosted version of the scripts yourself at that point.If you do go down that route, you can test it by pointing
google.com
to something bogus like0.0.0.0
in your hosts file. I would not be surprised if there are still issues at that point with things likeonload
events, since the event might have been fired before the event handler even gets registered in the fail-over case.