[如果我想通过bundleconfig.vb
文件管理我的Cdn文件并从Cdn加载Kendo-ui,我需要创建2个这样的捆绑包:
Dim kendoWebBundle = New ScriptBundle("~/bundles/kendo", kendoCndWeb).Include("~/Scripts/kendo/kendo.web.min.js")
kendoWebBundle.CdnFallbackExpression = "typeof kendo"
bundles.Add(kendoWebBundle)
Dim kendoAspBundle = New ScriptBundle("~/bundles/kendoMvc", kendoCndAsp).Include("~/Scripts/kendo/kendo.aspnetmvc.min.js")
kendoAspBundle.CdnFallbackExpression = "typeof kendo"
bundles.Add(kendoAspBundle)
但是,我的第二个后备表达式将不起作用,因为将定义kendo
:
<script src="//cdn.kendostatic.com/2014.2.716/js/kendo.web.min.js"></script>
<script>(typeof kendo)||document.write('<script src="/Gabarit.Web/bundles/kendo"><\/script>');</script>
<script src="//cdn.kendostatic.com/2014.2.716/js/kendo.aspnetmvc.min.js"></script>
<script>(typeof kendo)||document.write('<script src="/Gabarit.Web/bundles/kendoMvc"><\/script>');</script>
是否有一些kendo.aspnetmvc
后备表达式,或者我需要一起管理Cdnfallback(bundleconfig中没有fallbackExpression,手动验证kendo类型,如果未定义,则在本地加载所有kendo文件)?
如果我选择第二个选项,如果我的CDN发送kendo.web.min.js,而不是kendo.aspnetmvc.min.js,会发生什么?我觉得将定义剑道,并且不会发生本地回退...
从具有本地脚本后备的CDN中引用Kendo UI
这是您应该执行的Telerik suggests。
仅测试单个脚本,如果失败,则将所有内容本地加载。因此,不要测试kendo.all.min.js,然后再测试kendo.aspnetmvc.min.js,如果CDN出现问题,您可能根本无法解决。
<script src="http://cdn.kendostatic.com/2014.1.318/js/kendo.all.min.js"></script>
<script>
if (typeof kendo == "undefined") {
// checking for loaded CSS files is cumbersome,
// that's why we assume that if the scripts have failed, so have the stylesheets
// fallback to local Kendo UI stylesheets
document.write(decodeURIComponent('%3Clink rel="stylesheet" href="/path/to/local/kendo.common.min.css" %3C/%3E'));
document.write(decodeURIComponent('%3Clink rel="stylesheet" href="/path/to/local/kendo.blueopal.min.css" %3C/%3E'));
// fallback to local Kendo UI scripts
document.write(decodeURIComponent('%3Cscript src="/path/to/local/kendo.all.min.js" %3E%3C/script%3E'));
// also add kendo.aspnetmvc.min.js or kendo.timezones.min.js, if needed
}
</script>
ASP.NET Web优化框架
[就像您现在正在做的,对不起下面的C#。只需将Kendo UI脚本放在单独的包中,然后指定typeof kendo == undefined
后备表达式。
以相同的方式测试CSS,如果CDN不提供脚本,那么CSS也将不存在。
从NuGet获取Microsoft.AspNet.Web.Optimization
程序包。现在,在BundleConfig中,您可以设置捆绑包,使其不仅包含CdnPath,而且还包含CdnFallbackExpression:
public static void RegisterBundles(BundleCollection bundles)
{
bundles.UseCdn = true;
var kendo = new ScriptBundle("~/bundles/scripts/kendo", "http://cdn.kendostatic.com/2015.1.318/js/kendo.all.min.js").Include("~/Scripts/kendo/kendo.all.min.js");
kendo.CdnFallbackExpression = "typeof kendo == undefined";
bundles.Add(kendo);
var kendoMvc = new ScriptBundle("~/bundles/scripts/kendoMvc", "http://cdn.kendostatic.com/2015.1.318/js/kendo.aspnetmvc.min.js").Include("~/Scripts/kendo/kendo.aspnetmvc.min.js");
kendoMvc.CdnFallbackExpression = "typeof kendo == undefined";
bundles.Add(kendoMvc);
BundleTable.EnableOptimizations = true;
}
将后备表达式设置为“ kendo”将导致使用本地资源。
我修改了@Nicholas代码以反映这一点:
public static void RegisterBundles(BundleCollection bundles)
{
bundles.UseCdn = true;
var kendo = new ScriptBundle("~/bundles/scripts/kendo", "http://cdn.kendostatic.com/2015.1.318/js/kendo.all.min.js").Include("~/Scripts/kendo/kendo.all.min.js");
kendo.CdnFallbackExpression = "kendo";
bundles.Add(kendo);
var kendoMvc = new ScriptBundle("~/bundles/scripts/kendoMvc", "http://cdn.kendostatic.com/2015.1.318/js/kendo.aspnetmvc.min.js").Include("~/Scripts/kendo/kendo.aspnetmvc.min.js");
kendoMvc.CdnFallbackExpression = "kendo";
bundles.Add(kendoMvc);
BundleTable.EnableOptimizations = true;
}