ChatGPT解决这个技术问题 Extra ChatGPT

Why should I use Google's CDN for jQuery?

This may be obvious to some, but I've been wondering: why should I depend on Google's server to host jQuery for my site?

Is it only because it loads faster this way?

No one mentions what Google's policy was on hotlinking to their JS files (given hotlinking is usually frowned upon), so heres the URL where Google mention this is OK and more about the libraries they host: code.google.com/apis/ajaxlibs/documentation/index.html
Many users already have downloaded jQuery from Google or Microsoft when visiting another site. As a result, it will be loaded from cache when they visit your site, which leads to faster loading time
You should not use Microsoft or Google. Use MaxCDN. Much more likely to get cache hit, which is the most important thing here blog.jquery.com/2014/01/14/…

J
John Gietzen

This is because:

It increases the parallelism available. (Most browsers will only download 3 or 4 files at a time from any given site.) It increases the chance that there will be a cache-hit. (As more sites follow this practice, more users already have the file ready.) It ensures that the payload will be as small as possible. (Google can pre-compress the file in a wide array of formats (like GZIP or DEFLATE). This makes the time-to-download very small, because it is super compressed and it isn't compressed on the fly.) It reduces the amount of bandwidth used by your server. (Google is basically offering free bandwidth.) It ensures that the user will get a geographically close response. (Google has servers all over the world, further decreasing the latency.) (Optional) They will automatically keep your scripts up to date. (If you like to "fly by the seat of your pants," you can always use the latest version of any script that they offer. These could fix security holes, but generally just break your stuff.)


I love how you keep coming up with more and more reasons. +1 for that alone.
6. Googles servers are probably faster than yours! I'm not sure point 3 is really relevant since anybody can minify just as well as google. +1 for a great answer :)
I feel like #3 probably has more to do with gzip and the like, since jQuery already gives out a minified version.
God everyone was attacking me , i just sent my ideas , and was accepted , LOL
@farshad: Not attacking you, attacking the accepted answer which wasn't as good as the next answer. That's the way community Q&A works. Try formatting your answer a bit more professionally next time, and limiting your criticism of the question to the useful, constructive kind.
Q
Qantas 94 Heavy

There are several scenarios when you might not want to use jQuery from Google's CDN:

When you are building an intranet application where the web server is hosted on the same network as the clients. If you use Google's CDN jQuery, you will be making a call to the internet rather than a webserver on the local network. This increases bandwidth for your organization, and is slower. When you want to run your application offline. (Quite linked to the first issue) If you need to work on a development environment (managed for example with Bower), you might need to be able to make your application work without any internet connection (ie: in a train :) When you need to customize it. For example if you use Grunt to build the library in order to use only certain modules or setting the AMD name When you are serving pages over SSL that require jQuery. You should serve the JavaScript over SSL as well as your page to avoid security problems and warnings.

Also, Microsoft hosts jQuery on their CDN. That is another choice comparable to using Google's hosted jQuery.


Just an FYI that you can use Google's servers to serve up an SSL version of the hosted javascript libraries. ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js works.
Good to know, Aaron. Thanks.
+1 for mentioning Microsoft. Google gets way too much credit imo.
Also FYI: Scheme relative hyperlinks, like src="//ajax.googleapis.com/...", work.
And also consider cndjs
c
cantera

This study by TJ VanToll convinced me that it's better to concatenate jQuery with other scripts rather than load it from a CDN.

The reason is the latency involved in fetching jQuery on mobile devices:

"In 2012 the average RTT time on a mobile network in the United States was 344ms. And that 344ms applies to not only every HTTP request – which the average web page now makes 93 of – but also every DNS lookup and TCP connection... While average RTTs are improving, there are only small additional gains to be had, as current networks are within a small factor of the theoretical limit dictated by physics."

He also cites this post from Steve Souders that shows why you're actually unlikely to get the caching benefit from using a CDN:

"Because of the fragmentation in CDN providers, jQuery versions, and protocol usage (http vs. https), the chances of getting a CDN cache hit are shockingly low – and downloading from an external domain has the potential to perform not one, but three round trips (a DNS lookup, a TCP connection, and an HTTP GET)."


This is not longer true for HTTP2 and SPDY. Today you can host all your external resources in a CDN like Cdnjs.com or something and all files will be download using the same request. Thats basically the same as concatenate.
M
Matt Moor

The greatest benefit is from caching. The theory is that if a visitor visited a site that was loading their JavaScript libraries, say jQuery for example from the Google CDN, then when they visit your website, the library is already in that user’s browser cache and will not have to be downloaded again. This sounds great in theory.

The benefits being shared here and elsewhere are all theoretical. Just came across an in-depth analysis of using a CDN and if it provides the expected performance benefits. http://www.root777.com/appdev/does-using-google-libraries-api-cdn-give-you-performance-benefits


This answer is grossly underrated. It provides the equally valid "cons" to the "pro" accepted answer above.
@user239558 The link wouldn't be dead if the author of the study had actually used a CDN ;-) The error is: [function.require]: failed to open stream: No such file or directory in /homepages/41/d222999437/htdocs/wp-content/themes/prose/functions.php
The article isn't well thought out. It says that the least common jQuery libs are served by the Google CDN on about 0.5% of all pages. During a general browsing session I visit loads of pages, especially those little links to sites like for example the one that hosts it. Or blog sites. I easily visit 30 unique sites a day, after a week the chances of not having it cached are pretty slim. Although it may not be cached for all of your user, people who make extensive use of the internet will nearly always have them cached, because they visit far more than 1 unique domain in total.
J
Joshua Maddox

A major reason NOT to let Google host your jQuery, one that many people don't think about, is that it won't download if you are in China. It's blocked along with many other scripts, fonts etc... hosted by Google CDN's. If you need to reach a Chinese audience you're best to always use a fall back hosted on your own server. Google APIS Blocked in China


I disagree @tmthydvnprt. The question is opinionated, so that's where most of the fault lies to begin with, but this does answer the question.
I came here to same the same thing. If users are in China, most everything from Google is blocked by the great firewall. Though, this is probably a non-issue since lots of people in China use a VPN service, and you probably don't want to target the Chinese market anyway.
E
EdwardM

Some good answers here to "Why you should..." and "Why you should not..."

I simply want to add a list of alternatives to Google if you did want to load jQuery from a CDN.

CDN JS - https://cdnjs.com/libraries/jquery

Microsoft - https://docs.microsoft.com/en-us/aspnet/ajax/cdn/overview#jQuery_Releases_on_the_CDN_0]2

jsDelivr - https://www.jsdelivr.com/package/npm/jquery

code.jquery.com (StackPath) - https://code.jquery.com/

Google - https://developers.google.com/speed/libraries/#jquery

But to sum it up, you are basically improving your overall website/application performance.


O
Oscar Gardiazabal

Usign CDN with a Service Worker, you can download the CDN once in the client lifetime, and not every time you update your code.