DEV Community

loading...

Should we use CDN or locally download libraies?

pvcodes profile image Pranjal Verma ・1 min read

Hello guys,
As you all know I'm making my very first portfolio website. So I'm using jQuery for some stuff.
So should I use CDN of jQuery of download it locally and then link.

What will be more effective?
Share your thoughts and experience.

Discussion (4)

pic
Editor guide
Collapse
michelc profile image
Michel

You can do both. Use a CDN:

<script src="https://code.jquery.com/jquery-3.5.1.min.js"
        integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
        crossorigin="anonymous"></script>

Then use your local copy as a fallback:

<script>
window.jQuery || document.write('<script src="/js/jquery-3.5.1.min.js"><\/script>')
</script>

From: timjames.me/jquery-and-jquery-ui-f...

Collapse
marcusatlocalhost profile image
Marcus

If you know what you want to use, use a local copy of jQuery, so you are not dependent on a third party.
If you try out things with several libraries, use the CDN, because it's quicker and once you're done, use local copies for the reason mention before.
The performance argument, that someone could have a cached copy of that same version of jQuery, from the same CDN in their cache and therefore the browser doesn't have to load it is negligible.
Also, linking to a third party exposes the visitor IP to them. Depending on privacy laws, you might have to disclose that and it saves you the hassle to do that if you use local copies.

Collapse
ashishkumarsinghh profile image
Ashish Kumar Singh

CDN

  • If your website grows then, CDN will help in reducing load at your server. The size of this lib is small, but maybe you will use many others and when number of users grow, this small difference is multiplied.
  • Would be faster, depends on the location of CDN, location of client, but if your users are spread all over the world, then using a CDN will definitely reduce the average time to serve this lib.
Collapse
tirthaguha profile image
Tirtha Guha

Depends on your preference actually. If you want to serve static sites only and prefer a runtime inclusion, then CDN suits you. Be careful to take care of security though. It's untrusted third party, and you have no control over the changes in the source.

However, if you're more comfortable with a compile time engagement than runtime engagement, then use package manager. A little safer, since you know what version of library you're including.