9

If a website load time is a concern, which is generally better to use when referencing a JavaScript or CSS file? Would using a CDN be better or would that just add more HTTP requests?

8

The CDN should be used for all static files (.css/.js/images).

Sometimes however javascript or css files can have dynamic aspect to them such as that it would include a unique user string or something of that sort. In this situation the CDN server would have to contact the origin server on every request which would defeat the purpose.

If your CSS and Javascript is static for all users then using a CDN is the right way to go. This would not cause any extra HTTP requests because it would only load the css and js files from the CDN instead of your own webserver (unless you are using inline code). So instead of a users browser loading these requests from your server they would be loaded via the CDN, there is no additional requests you are just changing where to send these requests (again as long as you are currently not using inline code).

Other benefits from using a CDN would be that the CDN server would most likely be located closer to your end users then your origin which will benefit loading times. The CDN servers are also likely setup to serve static content much faster then your origin server by specifically catering the web server for static content.

  • If the CDN is not nearer and doesn't use edns-client-subnet it's not going to improve performance at all. This is not an "other benefit", being nearer the client, possibly providing some DOS protection/content optimization (though you don't mention these) are the only benefits. – symcbean Apr 6 '16 at 22:39
3

Using a CDN vs traditional web hosting for delivering your static files such as CSS, JS, and images is commonly preferred. This is because once your files are cached on the CDN's edge servers, your site visitors will be delivered static content from the closest point of presence (PoP) instead of the origin server.

In the majority of cases, this shortens the distance between the client and the server and thus helps improve loading times without adding any additional HTTP requests. This also helps in other areas such as increasing redundancy, taking a load off the origin, etc.

2

Use a CDN if you need a CDN. If your user is global and spread over a large area, or you have a lot of such content that you don't want to store on your own server, that is when a CDN is useful. Globally, it can speed up access to your content if the server is closer to the user. If you have many GB or Terabytes of static data and a heavy load for access to that content, a CDN can help with that.

However, small, local sites or lightly loaded sites rarely need such things and a CDN can only add one more complication to your set up, operation and workflow, such as caching problems.

Too often I see people use a CDN cause they read they should be using one and no other reason.

  • Thanks for pointing out it's not always required and indeed adds unnecessary complications. In my case I'm using a laravel based cms with a backend assets combiner and caches minifed files setting up an S3 cdn, gulp and vc on few js files was a waste of time. I'd rather focus on DB caching and proper code for speed – Raja Khoury Nov 26 '17 at 4:14
1

Using a CDN can be both a burden and a benefit to a website, all depending on how it has been implemented.

Positive Points

  1. Static content stored closer to the end user (faster loading times)
  2. Additional sub-domains (cdn1.domain.com, cdn2.domain.com, etc), this assists with the inherit limit in browsers limiting file downloads to two simultaneous files from the same fully qualified domain name at any one time. In other words using this example you would access HTML from www.domain.com and all the while be downloading 2 files from cdn1.domain.com, 2 files from cdn2.domain.com, and 2 files from cdn3.domain.com with all three cdn domains accessing the one cdn service and source.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.