2

What are the scenarios when you want to use CDN to load a javascript file? And which files you might want to lazy load. Or perhaps which scripts are the best candidate to be combined and minified?

for example:

jquery + jquery plugins - should these be accessed via CDN or better to combine/minify them as one to be accessed by just one request

angularjs modules - is it better to lazy load modules as needed or just combine/minify them all at once upon deployment so all modules are actually available to the application at any time

Let me know your idea on this

2 Answers 2

3

This question is kind of subjective, but here's my opinion.

CDN's are great because:

  1. Provide better performance as the content is cached around the world. This will mean lower latency for sites accessed from a wide range of locations
  2. The scripts will be cached so if i visit one site that uses the latest stable Angular on google cdn and then visit another with the same then only one request is performed. The more sites using cdns the higher likelihood that scripts will be cached so they should be used when possible
  3. Reduce the load on your servers which in turn will reduce the cost. This is especially important for sites that get a lot of traffic and also cloud hosted sites as you pay for what you use. I would say for cloud it's a must to use cdns
  4. As browsers have limits on the number of requests per domain at once, cdns will increase the amount of concurrent requests to content

Issues with CDN's:

  1. For each script a separate request needs to be made (unless cached) so this can increase the total amount of requests for your site
  2. Cdn could go down or suffer performance problems which could cause your site to go down. If using a cdn make sure you have a failback to a local version if the cdn fails e.g. for Angular check window.angular

Combining is great because:

  1. It reduces the amount of requests. This is important on mobile because mobile connections have high latency. It's also important because browsers can only perform a fixed number of requests per domain. This will also reduce the load on your servers as there is less requests to process

In short, use cdns when you can for common scripts such as angular, jquery etc and then use combining and minification for your own custom scripts. You should try to aim to only have one request for your own scripts.

If you have a huge amount of javascript then you should consider lazy loading of your scripts to reduce the initial loading time of you site.

Sign up to request clarification or add additional context in comments.

Comments

3

You will find difference of opinion .. Some people prefer to combine all of the js files in a single file and some load when particular module of javascript is required. I prefer to combine all of them in a signle file.

CDN Scenario is totally different. It is useful for parallel loading. Browser can load few requests parallel from server so it's better if you host your css and js files on CDN so that browser can load them parallel with HTML and other files.

1 Comment

very good answer and also it depends on the application. High traffic sites will be better of loading a single file (cdn or not) rather then lazy loading as the web server will be constantly bombarded with requests.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.