When I was working on recent posts, I was surprised to notice that jQuery script was loaded twice.
So I investigated it by looking at my current theme, as well as WordPress code. I found out that both WordPress and my current theme is loading jQuery.
My current theme is Pyrmont V2. I look at the header.php, I found out this:
So this is the reason why I found two jQuery source code being loaded (shown below).
But what about WordPress? In which file does the jQuery listed to be automatically loaded? After using Windows File-Content Search to scrutinize WordPress’wp-includes folder, I found the answer:
//... $scripts->add( 'cropper', '/wp-includes/js/crop/cropper.js', array('scriptaculous-dragdrop'), '20070118'); $scripts->add( 'jquery', '/wp-includes/js/jquery/jquery.js', false, '1.3.2'); $scripts->add( 'jquery-ui-core', '/wp-includes/js/jquery/ui.core.js', array('jquery'), '1.7.1' ); //...
I found it interesting that WordPress is loading jQuery from localhost (sodeve.net), but Pyrmont is loading it from Google. There must be a reason for this. Dave Ward at Encosia teaches us that the reasons are:
- Decreased Latency. Because Google is using Content Delivery Network (CDN). Therefore, it is much faster than your server
- Increased Parallelism. Because it reduces the number of connections made to a single server. Browser usually has limit on the number of connection to a single server
- Better Caching. Since the script is obtained from Google, it is very likely the script is already cached when visited any Google’s website that uses jQuery
Anyway, I’m sold to Dave’s idea. I replaced the line in script-loader.php, using http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js instead of /wp-includes/js/jquery/jquery.js. Then I observed the performance using Firebug.
Hmm.. WordPress added the “ver” parameter in the script request. This invalidated Dave’s point no. 3. What can we do to make WordPress not to add “ver” parameter in the script request?
How to Make WordPress Load Script without Ver Parameter
Open class.wp-script.php inside folder wp-includes of WordPress
//..... $src = add_query_arg('ver', $ver, $src); //.....
Comment the line shown above (line 117).
I believe things load faster now 😀 (At least for Google, sodeve.net server is a shared hosting, so its performance is difficult to expect :P)
I hope it helps 😉 If you like this article, kindly consider to visit the sponsors to check out their offers. Cheers!