如果 google.load 失败怎么办?

发布于 2024-08-16 02:58:26 字数 170 浏览 2 评论 0原文

好奇是否有一种简单的方法可以在 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 技术交流群。

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

发布评论

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

评论(1

紫轩蝶泪 2024-08-23 02:58:26

这种情况发生的可能性很小,很可能属于过度思考的范畴。实际上,您自己的服务器比 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 call google.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 the google 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 the google 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 like 0.0.0.0 in your hosts file. I would not be surprised if there are still issues at that point with things like onload events, since the event might have been fired before the event handler even gets registered in the fail-over case.

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