Monday, 24 December 2018

What are the benefits of CDN.

CDN is a network of computers that exist all over the world. For a web application, there are 2 ways we can make the jQuery library available.

Keep a copy of the jQuery library on your web server and reference it

<script src="~/Scripts/jquery-1.7.1.min.js" type="text/javascript">
</script>

Reference the jQuery library from a CDN. (Microsoft, Google, or jQuery)
<script src="http://code.jquery.com/jquery-1.7.1.min.js" type="text/javascript">
</script>

Advantages of using a CDN:

Caching benefits - If other websites use jquery and if the user has already visited those websites first, jquery file is cached. If the user then visits your website, the cached jquery file is used without having the need to download it again.

Speed benefits - The jquery file from the nearest geographical server will be downloaded.

Reduces network traffic - As the jQuery file is loaded from a CDN, this reduces the network traffic to your web server.

It is always recommended to refer CDN as well as keep a local copy in your production server. When for any reason CDN servers are down at that moment your application will stop working. To avoid this you can design your application in such a way that when CDN servers are down will refer local copy.

The following code checks if jQuery is loaded and if it is not loaded from CDN, the jquery script will be loaded from the web server.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" 
        type="text/javascript">
</script>

<script type="text/javascript">
    window.jQuery || document.write('<script src="/MVCDemo/Scripts/jquery-1.7.1.min.js">\x3C/script>')
</script>

No comments:

Post a Comment