0

In MVC we can specify a cdn fallback expression using Bundle.CdnFallbackExpression so that our site will use local versions of js or css in case the cdn is down. The expression for jquery, for example, is "window.jQuery". What I'd like to know is how do I go about writing these expressions for any given javascript library? I found a list of expressions for popular js libraries but I don't fully understand how these expressions were created.

I'm dealing with a project that has a lot of js libraries that are not on this list and I'm not sure how to go about writing expressions for all of them.

Am I looking for objects that the library creates? Can we use function names to see if the library loaded correctly?

Community
  • 1
  • 1
Bruno
  • 533
  • 1
  • 6
  • 27
  • It's impossible to write one for "everything". What you need to test for is dependant on the library you want to add the fallback for. You need one for each library – Liam Aug 22 '16 at 13:08
  • I didn't mean I wanted a generic expression for all libraries, I more meant what sort of things am I testing for in general given a js library. Even if it's a library I write myself, am I just looking for an object that is declared inside the library? – Bruno Aug 22 '16 at 13:10
  • Again, it depends. Basically something that tells you the library is loaded (I'm being purposly vague here as that can mean different things to different libraries). Typically (though this is highly variable) it's a global variable of a particular name, i.e. jQuery being the global closure of jQuery. – Liam Aug 22 '16 at 13:13

1 Answers1

1

The fallback expression is simply a test. In the case of jQuery, a jQuery member is added to the window object. Therefore, you can easily tell if jQuery has been added or not by the presence of that member. Tests for different libraries will obviously vary, but the core concept is just finding something that is unique to that library (an object, namespace, some method etc.) and seeing if it exists. For example, if it was a jQuery plugin, you would just look for the extension it adds to the jQuery object.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444