Introduction:
As a best practice, websites generally points to CDNs for downloading of Javascript libraries and CSS. CDN hosts are geographically scattered and when a user connects to a website, the libraries are downloaded from nearest CDN which improves overall website performance.
However, we can’t completely rely on CDNs. They may be down during a maintenance downtime. In that case we can’t take our website down and impact our users. Hence we need to implement a fallback mechanism.
FallbackJS:
Fallback JS is open source javascript library using which we can easily implement a fallback mechanism. Here is the brief description on this library from fallback js website.
Fallback JS is a tiny library that allows you to load both JavaScript and CSS libraries after your page has already loaded. Our library also allows you to specify “failovers” or “fallbacks” for each of your libraries, that way in case one of those external libraries your site is using happens to go down, you won’t be leaving your users with a dysfunctional website. Just because someone else’s website breaks, it doesn’t mean that yours should!
Install fallback js:
Using npm we can download fallback js. Here is the command:
Or we could directly download fallback.min.js and include in the project.
Add following line to .html page to include fallback js script.
http://scripts/fallback.min.js
Define library sources:
We need to specify the libraries inside the load method. In the following code snippet, the ‘angular’ and ‘custom’ are the library names. For each library, we can specify multiple source paths. The order of the source matters here. In this example I am loading angular.min.js from opensource.keycdn. The next source is the local library on the website. So, first it tries to load angular from keycdn. It it can’t for some reason, then it goes to the second option which is local source.
The names of the library can be anything except if you want to use the shimming. Shimming is the process of specifying the dependencies/ order of the libraries.
Note: If you want to specify shim, then you should name the libraries according to the library what you are trying to load. For instance, if it is a angularJS then it angular. If it is a kendo library then it should be ‘kendo’. If it is JQuery UI library then JQuery.UI. Otherwise shimming doesn’t work. For CSS libraries, there are not restrictions.
In this example, I have to load angular first before loading my home.js javascript file. Hence the dependency is specified in the shim as ‘custom’: [‘angular’]. Observe that its an array. So you can specify any number of library names as dependencies.
fallback.load({ 'angular': [ 'https://opensource.keycdn.com/angularjs/1.6.2/angular1.min.js', 'scripts/angular.min.js' ], 'custom': ['scripts/home.js'] }, { shim: { 'custom':['angular'] } }); fallback.ready(function() { angular.element(document).ready(function () { angular.bootstrap(document, ['newangularapp1']); }); });AngularJS manual bootstrap:
As we are loading libraries using fallback we can’t use angular’s default behavior. Rather we have to bootstrap angular manually once the libraries are loaded. Fallback provides a method called ready which takes a function definition. We can define anything here which we want to run after the libraries are loaded.
In our code, I have added code to call angular bootstrap inside ready method.
<div class="row"> <div>{{message}}</div> </div>Limitations:
There are limitations with fallback js. It doesn’t seem to be working correctly with IE browser. I didn’t have any issues with Chrome.
Conclusion:
For performance reasons websites point to CDN for any javascript and CSS libraries. However, we need have backup or fallback support if CDN is down. Using Fallback JS we can achieve it. It provides flexibility to define multiple source and define dependencies. As libraries load process can’t be determined, hence we need to manually bootstrap angular.