WP script versioning breaks cross-site caching?

I am loading JQuery from the google CDN using the following code:

wp_deregister_script('jquery'); 
 wp_register_script(
    'jquery', // handle - WP uses this name to refer to script
    'http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js',
     array(), // Array of handles representing scripts that this one depends on.
     false, // Version number - defaults to false.
     false // false = put script in header, true = put in footer
 );
wp_enqueue_script('jquery');

In firebug I see that wordpress appends ‘?ver=3.0.4’ to the URL to control caching. In fact there doesn’t seem to be any way to stop WP from appending something to the URL – I can provide my own string in the call to wp_register_script() but WP will use the default ‘ver=3.0.4’ if i leave it blank (or “false”)

I believe the appended version string stop the user’s browser from re-using a cached copy of the file that it might have downloaded from a different website. E.g.

  1. User visits www.example.com which loads ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js so it is now in browser cache.
  2. User then visits my site which loads ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js?ver=3.0.4
  3. The user’s browser not use it’s cached copy because the URLs are different.

Since cross-site caching is one of the main reasons I want to use Google’s CDN, are there any solutions to this other than loading the script manually (not ideal) or hacking WP core?

TIA

/Eoin/

6 Answers
6

Use null as $ver param:

wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js', false, null);
wp_enqueue_script('jquery');

Output:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

Leave a Comment