Remote assets in websites
Whenever I build a simple website or web page I like to have all the assets on my own server where they can be easily accessed and downloaded and within my control. Any images, JavaScript files, css etc are all in their respective folders ready to be downloaded when a user visits the web page. For a some time I’ve seen sites that include files on remote servers, a good example of this is jquery and how it can be included via the following URL http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js. More often than not I’d think to myself well that’s stupid why link to another site when you can have it on your own. What would happen if the external site is no longer is available? Alright it is Google but there is the potential to break something here. The last week I’ve been looking into Paul Irish and his html5 boilerplate and picked up quite a few ways to improve my own work. Whilst studying this i came across a great little snippet which caused me to actually use my brain for a moment and think about why i see these external files so often in other peoples code and why i should switch over to it.
<!-- Grab Google CDNs jQuery, with a protocol relative URL; fall back to local if necessary -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"></script>
<script>window.jQuery || document.write("<script src='js/libs/jquery-1.5.1.min.js'>x3C/script>")</script>
This great piece of code allows you to include the jquery script via the Google URL and if for some reason it isn’t accessible it will fallback to a local version. Not only does this remove the one worry i ever had with linking to external files but the comment with this made me face palm in recognition of why people do this in the first place. Those three magic letters CDN. A CDN is a content delivery network and is used to serve up files to users by serving them from locations closest to the user. So not only are we taking a small piece of bandwidth of our shoulders and placing it upon the mighty shoulders of the Google infrastructure we are also increasing the speed that the file is going to be downloaded for users across the world. I know I’ll be using it from now on, will you?
Comments
Comments are currently closed