Home > front-end development > Caching and Google's Ajax Libraries API — Cache your jQuery

Caching and Google's Ajax Libraries API — Cache your jQuery

August 14th, 2009

Leveraging Google's free hosting of common javascript libraries seems to be getting more popular. While you should be serving all javascript concatenated and minified together, the free hosting is nice for quick jobs or more minimal projects.

The versioning system they devised is a very clever way to always serve you the most up-to-date script. For example, you request version 2 of SWFObject and it'll currently deliver 2.2, but in the future you'll always get the most recent 2.x release.

I did a bit of research on how they handle these cases, and since these facts aren't elsewhere, they needed a home. :)

The current caching rules are as follows:

Request Response cached for
/1.3.2/jquery.min.js one year
/1.3/jquery.min.js no caching
/1/jquery.min.js one hour

* jQuery URLs used only for illustration purposes. It's the same case with all scripts' minor/major versions
* Minified and unminified files are treated the same

Previous to now, using the google.load('jquery','1.2.6') technique was the only way to ensure the script stayed considerably cached. Direct path-based access scripts were only cached for one day. But no worries, because all is better now.

2010.08.25 – Updated caching times. How odd..

front-end development

  1. August 15th, 2010 at 06:11 #1

    Would be cool if shorter URLs for older versions got better caching too. For example, there won’t ever be a jQuery 1.2.7, so there’s no need for short-term caching on http://ajax.googleapis.com/ajax/libs/jquery/1.2/jquery.min.js.

  2. November 26th, 2010 at 09:48 #2

    1.4.4 = one year = http://cl.ly/3P2l
    1.4 = one hour, but "must-revalidate"? = http://cl.ly/3Ouw
    1 = one hour, same as 1.4 = http://cl.ly/3Ohp

  3. Rohan
    June 8th, 2011 at 13:20 #3

    Hi Paul,

    Is there a way we can cache the the ajax library as local javascript file part of individual code base?

    I ask since we have used the JSAPI library to render google visualizations and there was a issue when google changed code in the jsapi library causing the entire app to go down.

    I did rather do

    "<script type="text/javascript" src="/jsapi.js">"

    than

    ""

    Thanks,
    Rohan

  4. June 8th, 2011 at 13:35 #4

    With the google ajax library you can always specify the exact version of that library you want to use. Therefore it wont change on you.

    That's what you want to do here.

    http://code.google.com/apis/libraries/devguide.html#versioning

  5. Rohan
    June 8th, 2011 at 15:00 #5

    Paul,

    Thank you for your response.

    I did check out the link you provided however there is no way to get a version of jsapi. I see jquery,jQueryUI, Yahoo…. but no no jsapi which is needed for working with visualizations.

    am I missing something here?

    Thanks,
    Rohan

  1. August 27th, 2010 at 10:08 | #1
  2. August 28th, 2010 at 13:53 | #2
  3. August 29th, 2010 at 01:48 | #3
  4. September 15th, 2010 at 06:22 | #4
  5. September 21st, 2010 at 13:59 | #5
  6. November 26th, 2010 at 16:02 | #6

For code blocks, use <pre lang="javascript">. css and html4strict are also accepted.