Why should I use a CDN (content delivery network) to include jQuery over downloading the jQuery library to my project?

Question

Why should I use a CDN (content delivery network) to include jQuery over downloading the jQuery library to my project?

Answer

When we use a CDN our content (in this case the jQuery library) may potentially load faster than if we hosted jQuery on our own server (by downloading it to our project) as the content will be coming from the nearest possible data server in relation to our user. With a CDN there is also the potential that the same version of jQuery will be cached (saved locally) to our user’s web browser and available for our site to use without having to make another request to a server for the same file.

8 Likes

Would it not be better to even go a step further and use module bundlers such as webpack to bundle and compress your application and only send that to the client? Would love an advanced tutorial on how to effectively use webpack (and other bundlers) to compress your code down to the absolute smallest size.

Based on the answer that you have given I have a question

Can we set a condition in our web server that if the CDN by any chance is taking too long to load then load the files from the hosted server instead just in case. Although I don’t doubt JQuery servers are going to crash anytime its just a thought.

Their can be any file that we have to load from a 3rd party vendor not necessarily JQuery but anything else

Please let me know if that would be the right choice and if that’s possible how?

Downloading jQuery from their CDN seems like a no-brainer, and it does not interfere with the site load since it happens asynchronously from another server. That’s the number one reason for a CDN, another server.

What if our website is already being served over a CDN by our hosting provider? Does saving it locally still have the same affect on load times?