Lovelock
Lovelock

Reputation: 8105

CDN hosted javascript libraries vs downloaded and minified

Looking for some information here mainly.

Recently changed my setup to work with Grunt and LiveReload. Its great for creating small minified css files from scss files so thats great.

However, whats best in terms of JS? I currently use CDNs for most things such as jQuery, Isotope, TagIt and so on.

I have a few custom JS files which contain my own code for my site, so its okay for me to concatenate them and minify. I understand that having many requests affects performance, hence minifying and concatenating.

But is it better to download all the libraries and compile them into one JS file to include on my site? Or keep the external ones linked to a CDN?

Upvotes: 4

Views: 899

Answers (1)

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95629

There are really two considerations here:

  • Security
  • Performance

Security

In terms of security, hosting JavaScript yourself puts you in charge, whereas relying on JavaScript from an external domain essentially trusts that domain with the security of your domain (which, depending on your level of trust with that third party, may or may not be acceptable). If you use advanced security settings such as Content-Security-Policy, you may need to do some extra work to allow these externally hosted scripts (e.g. specifically whitelisting the CDN domains in the script-src).

Performance

In terms of performance, using a CDN hosted version -- especially if it is popular -- may give you better caching; however, hosting it yourself and combining it with your other scripts may produce fewer requests. In terms of which is actually faster, you will need to do some of your own measurement (I recommend doing an a/b test with real user traffic, which will give you a better idea of whether real users already have the CDN hosted versions of the resource cached or not).

Upvotes: 4

Related Questions