Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 813
MattDiamant Avatar asked Mar 21 '23 12:03

MattDiamant


2 Answers

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.

like image 165
elclanrs Avatar answered Apr 06 '23 15:04

elclanrs


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)

like image 45
raam86 Avatar answered Apr 06 '23 16:04

raam86