简体   繁体   English

依赖脚本的CDN后备

[英]CDN Fallback for Dependant Scripts

If I am using the following scripts: 如果我使用以下脚本:

  • jQuery jQuery的
  • jQuery Validation jQuery验证
  • jQuery Validation Unobtrusive jQuery验证不引人注目
  • Bootstrap 引导

Bootstrap and jQuery Validation require jQuery and jQuery Validation Unobtrusive requires jQuery Validation so we have interdependent scripts. Bootstrap和jQuery验证需要jQuery和jQuery验证非干扰性需要jQuery验证,因此我们有相互依赖的脚本。 The fallback scripts I have seen all look something like this: 我看到的所有后备脚本看起来都是这样的:

(window.jQuery || document.write('<script src="/bundles/jquery"><\/script>'));
($.validator || document.write('<script src="/bundles/jqueryval"><\/script>'));
($.validator.unobtrusive || document.write('<script src="/bundles/jqueryvalunobtrusive"><\/script>'));
($.fn.modal || document.write('<script src="/bundles/bootstrap"><\/script>'));

The problem is that if I am using the ajax.aspnetcdn.com CDN and it fails, then the first line will cause jQuery to be loaded locally but the next three lines of code will execute before that and error because $ is undefined. 问题是,如果我使用的是ajax.aspnetcdn.com CDN并失败,则第一行将导致jQuery在本地加载,但接下来的三行代码将在此之前执行,并且错误,因为$未定义。

Is there a standard way to handle these types of interdependent fall-back scripts? 是否存在处理这些类型的相互依赖的后备脚本的标准方法? I have looked around and can't find a resource for handling this type of scenario. 我环顾四周,找不到用于处理这种情况的资源。

UPDATE UPDATE

I answered the question as best I can but I'm still looking for an answer on how people handle this. 我已尽我所能回答了这个问题,但我仍在寻找人们如何处理此问题的答案。

It seems overkill to rely on yet another third party script but I have discovered Fallback.js and YepNope.js which may be able to handle this scenario. 依赖于另一个第三方脚本似乎有些矫kill过正 ,但是我发现Fallback.jsYepNope.js可能能够处理这种情况。 I am not sure about the performance implications of the everyday scenario where the CDN's just work. 对于CDN正常​​工作的日常情况,我不确定性能的影响。 More info here . 更多信息在这里

I also had a go at writing my own implementations: 我还可以编写自己的实现:

JavaScript Fallbacks JavaScript后备

(function (document, fallbacks) {

    var check = function (fallbacks, i) {
        if (i < fallbacks.length) {
            var fallback = fallbacks[i];
            if (fallback.test()) {
                check(fallbacks, i + 1);
            }
            else {
                var script = document.createElement("script");
                script.onload = function () {
                    check(fallbacks, i + 1);
                };
                script.src = fallback.src;
                document.getElementsByTagName("body")[0].appendChild(script);
            }
        }
    }
    check(fallbacks, 0);

})(document, [
    { test: function () { return window.Modernizr; }, src: "/js/modernizr.js" },
    { test: function () { return window.jQuery; }, src: "/js/jquery.js" },
    { test: function () { return $.validator; }, src: "/js/jquery-validate.js" },
    { test: function () { return $.validator.unobtrusive; }, src: "/js/jquery-validate-unobtrusive.js" },
    { test: function () { return $.fn.modal; }, src: "/js/bootstrap.js" },
    { test: function () { return window.Hammer && window.Hammer.VERSION; }, src: "/js/hammer.js" },
    { test: function () { return window.Zepto; }, src: "/js/bootstrap-touch-carousel.js" }
]);

CSS Fallbacks CSS后备

The idea for using a meta tag was 'borrowed' from the new ASP.NET 5 MVC 6 framework. 使用元标记的想法是从新的ASP.NET 5 MVC 6框架“借用”的。

(function (document, fallbacks) {

    var metas = document.getElementsByTagName("meta");

    for (var i = 0; i < fallbacks.length; ++i) {
        var fallback = fallbacks[i];

        for (j = 0; j < metas.length; ++j) {
            var meta = metas[j];
            if (meta.getAttribute("name") == fallback.metaName) {
                if (!fallback.test(meta)) {
                    var link = document.createElement("link");
                    link.href = fallback.href;
                    link.rel = "stylesheet";
                    document.getElementsByTagName("head")[0].appendChild(link);
                }
                break;
            }
        }

    }

})(document, [
    {
        // metaName - The name of the meta tag that the test is performed on. The meta tag must have a class from the
        //            relevant stylesheet on it so it is styled and a test can be performed against it. E.g. for 
        //            font awesome the <meta name="x-font-awesome-stylesheet-fallback-test" class="fa"> meta tag is
        //            added. The 'fa' class causes the font awesome style to be applied to it.
        metaName: "x-font-awesome-stylesheet-fallback-test",
        // test - The test to perform against the meta tag. Checks to see if the Font awesome styles loaded 
        //        successfully by checking that the font-family of the meta tag is 'FontAwesome'.
        test: function (meta) { return meta.style.fontFamily === "FontAwesome"; },
        // href - The URL to the fallback stylesheet.
        href: "/css/font-awesome.css"
    }
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM