简体   繁体   中英

Why does HTML5Boilerplate and others use a CDN for jQuery?

HTML5Boilerplate, and others [citation needed] , load jQuery this way, as we all know:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.10.2.min.js"><\/script>')</script>

Is this better for users? It's good practice to keep DNS lookups low, and unless we're also grabbing jQuery-UI or other frameworks from Google, then this is the only resource we get from their CDN. Would serving it from our own servers be faster?

Is this better for the server? Are we really saving that much by using Google's CDN for just this one relatively small file, rather than serving it ourselves?

Why just jQuery? Why just Google? HTML5Boilerplate includes normalize.css, and modernizr.js, both of which are popular files (arguably more popular and more of a staple than jQuery [disputed] ) and are available at http://cdnjs.com/ and at a number of other CDNs. If we're loading jQuery, why not those 2? Is jQuery loaded from a CDN because it's deferred to load at the bottom of the page, and therefore it's OK to wait an extra .1s to get it from a CDN? I know Google CDN is a giant, but it's not unimaginable that other CDNs could handle a good amount of traffic.

Edit: Looking at Stack Overflow's code for this very page, they use their own CDN for 10+ resources, and then use Google for jQuery. There has to be a good reason for this, right?

Is this better for users?

Yes, especially popular libraries, because they might already be cached on the user's browser. Google servers are faster, and more reliable that your server; and so are most CDN's.

Is this better for the server?

Well, you serve less data.

Why just jQuery? Why just Google?

Not just jQuery, not just Google. The point is to use CDN when you can (and fallback to your server version) to benefit from speed and caching. Normalize is very small, but I think you can still benefit from using CDN. As for Modernizr, you want to be using a custom version, built for your needs, that's the recommended way to use the library.

Some of the said point of using a cdn is that users may have it in their cache and they will not have to download the file specifically for you website.

As for using Google CDN, They are huge and have amazing infrastructure; so why not?

Also I believe google is affiliated with html5boiler plate (At least one of the maintainers works for google )

当你从谷歌中提取jQuery时,它很可能已经从以前的网站缓存,阻止了额外的下载。

We've discussed this question on the repo several times. This was the most recent . Lots of opinions, some testing, some super smart, super experienced people offering their 2 cents there. A fun, instructive discussion.

In general, anything you're wondering about with h5bp has been discussed in public somewhere.

To boil it down, the CDN is used because it's the best default configuration. If people download HTML5 Boilerplate and do nothing else to the code, having it linked to the Google CDN is the best default. It offers geographical optimization, fast servers, a cookieless domain and a chance of hitting the cache lottery.

It's also, by far, the most popular CDN, so if you're going to use a 3rd party service that's the one to use if you want a decent chance of getting a cached copy.

Again, there's a ton of detail in the issue if you have any other questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM