检测并记录外部 JavaScript 或 CSS 资源加载失败的情况

发布于 2024-11-01 13:18:43 字数 545 浏览 1 评论 0原文

我有多个对外部 js 和 css 资源的 引用。大多数情况下,这些资源用于第三方分析等。有时(有趣的是)这些资源无法加载,通常会导致浏览器超时。当外部JavaScript或CSS资源加载失败时,是否可以检测并登录服务器?

我正在考虑某种类型的延迟加载机制,当失败时,将调用一个特殊的 URL 来记录此失败。有什么建议吗?

我认为会发生什么:

  1. 用户点击我们的页面,服务器端成功处理并提供页面

  2. 在客户端, HTML 标头尝试连接到我们的第 3 方集成合作伙伴,通常通过以“http://www.someothercompany.com...”开头的 JavaScript 包含。

  3. 另一家公司无法处理我们的负载或正常运行时间很差,因此连接失败。

  4. 用户看到一个通用的 IE 页面未找到,而不是来自我们的服务器的页面。

因此,即使我的网站已启动并且其他一切都运行良好,只是因为对第三方服务器的调用失败(一个在 HTML 页面标头中),我们就无法启动。

I have multiple <head> references to external js and css resources. Mostly, these are for things like third party analytics, etc. From time to time (anecdotally), these resources fail to load, often resulting in browser timeouts. Is it possible to detect and log on the server when external JavaScript or CSS resources fail to load?

I was considering some type of lazy loading mechanism that when, upon failure, a special URL would be called to log this failure. Any suggestions out there?

What I think happens:

  1. The user hits our page and the server side processes successfully and serves the page

  2. On the client side, the HTML header tries to connect to our 3rd party integration partners, usually by a javascript include that starts with "http://www.someothercompany.com...".

  3. The other company cannot handle our load or has shitty up-time, and so the connection fails.

  4. The user sees a generic IE Page Not Found, not one from our server.

So even though my site was up and everything else is running fine, just because this one call out to the third party servers failed, one in the HTML page header, we get a whole failure to launch.

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

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

发布评论

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

评论(4

筱武穆 2024-11-08 13:18:43

如果你的应用程序/页面依赖于JS,你可以用JS加载内容,我知道这很混乱。当使用 JS 加载这些内容时,您可以使用回调,让您只拥有已加载内容的功能,而不必担心未加载的内容。

var script = document.createElement("script");
script.type = "text/javascript";
script.src = 'http://domain.com/somefile.js';
script.onload = CallBackForAfterFileLoaded;
document.body.appendChild(script);
function CallBackForAfterFileLoaded (e) {
//Do your magic here...
}

我通常通过使用相互依赖的 JS 数组和文件来让这变得更复杂,如果它们不加载,那么我就会出现错误状态。

我忘了提及,显然我只是展示如何创建 JS 标签,您必须为要加载的其他类型的文件创建自己的方法。

希望也许有帮助,干杯

If your app/page is dependent on JS, you can load the content with JS, I know it's confusing. When loading these with JS, you can have callbacks that allow you to only have the functionality of the loaded content and not have to worry about what you didn't load.

var script = document.createElement("script");
script.type = "text/javascript";
script.src = 'http://domain.com/somefile.js';
script.onload = CallBackForAfterFileLoaded;
document.body.appendChild(script);
function CallBackForAfterFileLoaded (e) {
//Do your magic here...
}

I usually have this be a bit more complex by having arrays of JS and files that are dependent on each other, and if they don't load then I have an error state.

I forgot to mention, obviously I am just showing how to create a JS tag, you would have to create your own method for the other types of files you want to load.

Hope maybe that helps, cheers

橙味迷妹 2024-11-08 13:18:43

您可以在 JavaScript 中查找是否存在对象,例如查看 jQuery 是否已加载...

if (typeof jQuery !== 'function') {
   // Was not loaded.
}

jsFiddle< /a>.

您还可以检查是否缺少 CSS 样式,例如,如果您知道某个 CSS 文件将背景颜色设置为 #000

if ($('body').css('backgroundColor') !== 'rgb(0, 0, 0)') {
   // Was not loaded.
}

jsFiddle

当这些失败时,您可以向服务器创建 XHR 来记录这些失败。

You can look for the presence of an object in JavaScript, e.g. to see if jQuery is loaded or not...

if (typeof jQuery !== 'function') {
   // Was not loaded.
}

jsFiddle.

You could also check for CSS styles missing, for example, if you know a certain CSS file sets the background colour to #000.

if ($('body').css('backgroundColor') !== 'rgb(0, 0, 0)') {
   // Was not loaded.
}

jsFiddle.

When these fail, you can make an XHR to the server to log these failings.

逐鹿 2024-11-08 13:18:43

那么 ServiceWorker 呢?我们可以用它来拦截所有http请求并获取响应代码来记录外部资源是否加载失败。

What about ServiceWorker? We can use it to intercept all http requests and get response code to log whether the external resource fails to load.

2024-11-08 13:18:43

对 js 名称和会话 cookie 进行哈希处理,并以普通格式发送 js 名称和哈希值。服务器端,进行相同的哈希,如果两者是相同的日志,如果不是,则假设它是滥用。

Make a hash of the js name and session cookie and send both js name in plain and the hash. Server side, make the same hash, if both are same log, if not, assume it's abuse.

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