First look at this:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
With this code, you can download the jQuery library directly from the Google Content Delivery Network (CDN).
')
Please note that you can directly specify which version (/1.4.4/) of the library should be downloaded. But this is not all that can be done. By simply changing this part of the link, you can create small amenities:
/1.4.4/ - loads the specified version of the library, which will never change.
/1.4/ - download version 1.4.4 right now, but if version 1.4.5 comes out tomorrow, this link will point to it. If 1.5 then appears, it will indicate the latest release in the 1.4.x branch.
/ 1 / - download version 1.4.4 right now. If tomorrow appears 1.5, it will point to it. After the release of version 2.0, it will point to the latest release in jQuery 1.x.
A small reminder of why this is all being done.Reduce the delay - the file is loaded from the nearest geographically server.
We parallelize the download - browsers limit the number of simultaneous connections to one domain, and so files can be loaded in parallel, speeding up the download.
We improve caching - there is a high probability that in the browser cache of the visitor the file already lies and this is the fastest way to load it.
Save traffic - compressed version 1.4.4 “weighs” 82 kilobytes. If your visitors request a million pages with an empty browser cache, you will save 74 gigabytes of traffic.
CachingThere should be an important point. The type of caching depends on the link you use to load the library. Only a link to the direct version will give the best result.
/1.4.4/ - public, max-age = 31536000
(one year)/1.4/ - public, must-revalidate, proxy-revalidate, max-age = 3600
(one hour with recheck)/ 1 / - public, must-revalidate, proxy-revalidate, max-age = 3600
(one hour with recheck)Obviously, it is completely useless to cache for one hour. On the other hand, if someone released 1.4.4 for a year, the output of version 1.4.5 would get a stale version, which is also not very good.
Consideration of factors of delays, parallel loading and saving traffic is still important, but caching is no less significant. So if caching plays a big role for us, use the option of a link to a strictly specific version.
What to choose/1.4.4/ - never change, never destroy functionality, caches best of all, intuitively understandable
/1.4/ - may lead to a violation of the code when updating, poorly cached.
/ 1 / - it is very likely that it will lead to a malfunction when updating, it is poorly cached.
Thus, for most application scenarios, the version with a strict version specification is best suited. It will also keep your site or application under control.
Do not forget, of course, combine all your scripts. True: one file is always better to upload than several.
Not jQuery oneThe above considerations are valid for all libraries in Google CDN. The author checked them against MooTools and everything works in the same way.
Other CDNsJQuery can be picked up from the Microsoft network or from jquery.com. They do not give much freedom when choosing versions, but, it is worth noting, Microsoft allows you to cache the file for a year:
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.4.min.js"></script>
jQuery.com does not specify how to cache its file:
<script src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
Update 1.Support the domestic manufacturer with an extensive list of libraries:
api.yandex.ru/jslibsUpdate 2.Such a download will allow search engines to use this tool as an additional source of statistics on visits to certain sites, if this is confusing for someone.
Caching even for a year often does not make sense, since The size of the browser cache for many users is small. My default Firefox and Opera were 50 and 40 megabytes respectively. With such sizes and current access speeds, it will be updated very often due to the replacement of old elements with new ones. I increase the size of caches to 500 megabytes, although some authors prefer to think that this greatly reduces the speed of browsers.
Update 3. Transmitted traffic
Experiments with FireBag showed that the minimum traffic is provided by loading jQuery from CDN Yandex.st, and Microsoft still for some reason slips a bun:
Yandex.st | Google cnn | Microsoft CDN |
24605 bytes, gzip | 27100 bytes, gzip | 34187 bytes, gzip + 1090 bytes cookie |
Update 4. Response speed
Measurements were made in Krasnodar (Beeline) manually by Yslow by means of a 10-fold refresh. In other cities, the results may differ dramatically. In brackets the time of the first response with a blank cache.
Yandex.st | Google cnn | Microsoft CDN |
~ 53 ms (305-320 ms) | ~ 110 ms (324-333 ms) | ~ 400 ms (720-990 ms) |