3

I use MVC.Net bundling and minification at the moment. When I load my page which renders a bundle (For my example I'm using JQuery's one), both the CDN and the fallback are requested by the browser, which makes 2 requests to my web server. The server is expected to receive ~2000 hits per minutes so if I can reduce my scripts requests by half it would be pretty great. So my question is : Why does the fallback is requested by the browser and how can I prevent it?

Here is my BundleConfig

     BundleTable.EnableOptimizations = true;
     bundles.UseCdn = true;
     var bundle = new ScriptBundle("~/bundles/JQueryCore", "//ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.1.min.js")
     {
        CdnFallbackExpression = "window.jquery"
     };

     bundle.Include("~/Scripts/JQuery/jquery-2.1.1.js");
     bundles.Add(bundle);

In my .cshtml I call my script @Scripts.Render("~/bundles/JQueryCore")

The generated html looks like this :

<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.1.min.js"></script>
<script>(window.jquery)||document.write('<script src="/bundles/JQueryCore"><\/script>');</script>
IEatBagels
  • 823
  • 2
  • 8
  • 23

1 Answers1

4

CdnFallbackExpression needs to match the library you're testing for; window.jquery is off my a letter-case. Give this a try:

jqueryBundle.CdnFallbackExpression = "window.jQuery"; // note 'Q'.

This will fix the inline test (and therefore prevent the local script from being called).

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • Darn I'm retarded, I didn't put the right code in there, the fallback expression IS actually "window.jQuery", I will edit my answer.. – IEatBagels Dec 16 '14 at 15:58
  • Are you sure it's not `window.jquery`? If it is, Capitalize the `Q` and all should be corrected. – Brad Christie Dec 16 '14 at 15:59
  • Well, that was it. Thanks a ton! You might want to edit your answer to reflect my edit (the `CdnFallbackExpression` test/source) – IEatBagels Dec 16 '14 at 16:05
  • To be completely clear, `window.jQuery` is a special phrase, it doesn't have to match anything in your code: http://stackoverflow.com/questions/1014203/best-way-to-use-googles-hosted-jquery-but-fall-back-to-my-hosted-library-on-go. Each library has it's own `CdnFallbackExression`. You have to look them up. – nmit026 Nov 14 '16 at 03:00