Why put JavaScript, CSS and images on another server?


On most sites I noticed that they use another server to load scripts, CSS and images. Why do they use it?


It's a technique called CDN . This technique consists of requesting the file from a very used server. You can for example import a jQuery script from Google server.

But why?

Well, if the user has already entered a site that requested jQuery from Google, and enter your site that is also using the same URL to request it, it will already have cached and therefore it increases performance considerably since it doesn't will need to use it again. Just as if your site is the first, the file you requested will be stored in the browser's cache so that it doesn't need to be requested again on other sites.

The disadvantage is that to work perfectly, the requested URLs need to be the same (So that the browser understands that the file is already in the cache). Also, if your site is the first to request a certain URL, there will be no cache, and loading will be slower at least on this first visit as the request will be to an external server.

Another disadvantage is that if the server hosting the file is offline or inaccessible at the time of the request, the file is not requested, returning a 404 error. However, you can handle this with a check that loads a local version in this case.

See an example:

<script src="//code.jquery.com/jquery-1.2.1.min.js"></script>

    window.jQuery || document.write('<script src="jquery-1.2.1.min.js">\<script>');
Scroll to Top