-1

I am using a script bundle in an MVC application to load a script from CDN using the code below:

bundles.Add(new ScriptBundle("~/bundles/jqueryValidate", "//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js").Include("~/Scripts/jquery.validate.min.js"));

If the CDN fails it should load the script from the local repository (from inside the project).

I have added:

bundles.UseCdn = true;

and

BundleTable.EnableOptimizations = true;

to make sure the CDN is loaded first.

When I try from my network (public network) it works just fine: the script loads from the CDN without any issues.

If I try from my client's network (corporate network) the CDN is blocked and the replacement script is not loaded. I don't understand why the failover mechanism that I put in place was not working.

Do you know why that might happen?

Matthew Verstraete
  • 6,335
  • 22
  • 67
  • 123
Andrei Petrut
  • 390
  • 2
  • 15
  • When the pages don't load have you looked at the source code for the path it is trying to load from and seen if you can directly brows that path? – Matthew Verstraete Feb 10 '16 at 16:05
  • Yes, the source was the CDN path. I put that source directly in the browser and the script didn't load. I got an error saying: net:: ERR_CONNECTION_CLOSED. In this case shouldn't the failover mechanism enable itself and load the script from the local project? – Andrei Petrut Feb 10 '16 at 16:10
  • What code are you using for failover checking? Have you read over http://www.asp.net/mvc/overview/performance/bundling-and-minification – Matthew Verstraete Feb 10 '16 at 16:13
  • This code is supposed to do the failover: bundles.Add(new ScriptBundle("~/bundles/jqueryValidate", "//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js").Include("~/Scripts/jquery.validate.min.js")); – Andrei Petrut Feb 10 '16 at 16:17
  • Using `Include` does not know anything about failing over AFAIK - that is just the way to include extra scripts in that bundle. – Jamiec Feb 10 '16 at 16:19
  • Useful: http://www.hanselman.com/blog/CDNsFailButYourScriptsDontHaveToFallbackFromCDNToLocalJQuery.aspx – Jamiec Feb 10 '16 at 16:25

1 Answers1

0

The code your using is not a fail over code. If you look at the docs for this it is explicitly stated that if you just run

bundles.Add(new ScriptBundle("~/bundles/jqueryValidate", "//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js").Include("~/Scripts/jquery.validate.min.js"));

the CDN will be loaded in production and the Include path will be loaded in debug. Then they state

jQuery will be requested from the CDN while in release mode and the debug version of jQuery will be fetched locally in debug mode. When using a CDN, you should have a fallback mechanism in case the CDN request fails. The following markup fragment from the end of the layout file shows script added to request jQuery should the CDN fail.

And they even give you an example code of

    <script type="text/javascript">
        if (typeof jQuery == 'undefined') {
            var e = document.createElement('script');
            e.src = '@Url.Content("~/Scripts/jquery-1.7.1.js")';
            e.type = 'text/javascript';
            document.getElementsByTagName("head")[0].appendChild(e);

        }
    </script> 

as a check to make sure the CND loaded and if not to fail over.

If your using MVC on .Net 4.5 look into the CdnFallbackExpression call to have it autogenerate, check, and fallback for you.

You might also find this blog from Scott Hanselman helpful as well.

Matthew Verstraete
  • 6,335
  • 22
  • 67
  • 123