我正在按如下方式设置我的 jQuery 包:
var bundle = new ScriptBundle("~/bundles/jquery", "//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js");
// Path to the version of the file on your server (in case the CDN fails)
bundle.Include("~/scripts/jquery-{version}.js");
// JS expression to run, to test if CDN delivered the file or not
bundle.CdnFallbackExpression = "window.$";
bundles.Add(bundle);
为了提高我的 Google PageSpeed Insights 分数,我选择异步加载脚本:
@Scripts.RenderFormat(@"<script src=""{0}"" async></script>", "~/bundles/jquery")
但是现在,它似乎总是失败 CDN 后备表达式并最终加载脚本两次;一次来自我的后备包,一次来自 CDN,当异步调用完成时。我假设是因为当它运行回退测试时,对 CDN 的异步调用尚未完成。
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js" async=""></script>
<script>(window.$)||document.write('<script src="/bundles/jquery"><\/script>');</script>
<script src="/bundles/jquery"></script>
有没有更聪明的方法可以编写我的后备表达式?
完全有更好的解决方案吗?
我会将 jQuery 加载到更靠近底部的位置,但它会破坏任何依赖它的内联调用。