How can I use a CDN with fall back in Episerver?

  • Page Owner: Not Set
  • Last Reviewed: 2018-11-01

I'd like to use a CDN for common libraries, but I also want to have a fall back. How can I set this up in Episerver?


Answer

This isn't just for episerver but .net in general. Below is the code that will try to use the cdn first, else fall back to your local copy.

bundles.UseCdn = true;
BundleTable.EnableOptimizations = true; //force optimization while debugging

var jquery = new ScriptBundle("~/bundles/jquery", "//ajax.aspnetcdn.com/ajax/jquery/jquery-2.0.0.min.js").Include(
        "~/Scripts/jquery-{version}.js");
jquery.CdnFallbackExpression = "window.jQuery";
bundles.Add(jquery);