In this article, we are going to discuss what is CDN and Advantages and Disadvantages of loading jQuery from CDN. We are also going to discuss What if the jQuery file cannot be downloaded from CDN?
Let’s see the benefits of using CDN: –
There are several benefits of using CDN. Like :
- Distributed CDN servers: The jQuery file can be downloaded from CDN server that is closest to the user. Let’s say we are hosting a web application and we are hosting it on a server which is in Poland and let us assume that we have a client from India. Now this client access a page within your application. All the resources required by that page i.e. the images, CSS files, jQuery files all these files need to be downloaded from the server that is there in Poland.
Now let us assume that our application is loading a jQuery from a CDN server, now What is a CDN? It is a system of distributed servers. So there are loads of CDN servers, across the globe, therefore, there could be a CDN in India as well. So when the same user requests the page from your application since there is a CDN server in India, so now the jQuery file can be downloaded from the CDN server that is in India which is closer to your client and that means your page load time can be improved because it is being downloaded from a server that is closest to the user so that is going to improve the downloading time which is, in turn, improves your load time.
The other benefit is as follows:
- Browser caching: –
jQuery is used on many popular websites. If a user has already visited a web page that uses jQuery from a CDN then if he arrives at your page the jQuery file has already been cached by the browser so there is no need to download it again.
- Parallel Downloads:-
There is a browser limit on how many files can be concurrently downloaded from a given domain. This number varies from browser to browser. For ex:- If the browser allows only 2 concurrent downloads from a given domain, the 3rd download is blocked until one of the previous files has been fully downloaded from a different domain so this means the browser allows another 2 parallel downloads from the CDN server.
- Reduced Server Load:- The HTTP request for jQuery file is handled by the CDN server so the load on your server is reduced. This also means there is a saving on your website bandwidth consumption which in turn will reduce your hosting cost.
There a disadvantage of using CDN: – Your client firewalls may block the CDN. So you may have to request your clients to whitelist the CDN.
This is especially true when you are switching the CDN provider now when you have initially developed your application let’s say using Microsoft CDN and you told your clients about your domain name so the clients have unblocked their firewalls for that domain name. But all of a sudden because of some technical issue you may want to switch to another CDN provider, now when you do that you will have to tell your clients about that new CDN provider so if it is not whitelisted, the firewalls may block that CDN provider, as a result, the jQuery file will not be loaded and your application may not work.
Now, what will happen if the required jQuery file cannot be downloaded from CDN?
Let’s assume that the CDN is down or because of some network issue we are not able to download jQuery from CDN., in this case, we will have to fall back to use jQuery file that we hosted on our own server.
If jQuery is successfully downloaded, the jQuery property is added to the window object if this property is not found then jQuery is not downloaded.
Visit – Hostdens