jQuery update in a multisite Drupal environment

Like most Drupal hackers, I love to hate jQuery. The primary reason for this is that the version of jQuery included in Drupal core is always several versions behind where it should be. Unfortunately updating it is a serious undertaking, usually reserved for the next Drupal version. To get us by between releases, there’s a module called jQuery update. It’s pretty rad. It replaces the core jQuery dependencies with a newer jQuery version. It has one caveat though:

The tricky step with this module is that you will need to *replace* the jquery.js file in core with the jquery.js file included in the jquery_update directory…

In Drupal, messing with core files is poor form. Not only does it mess up the upgrade path, it causes all sorts of discontent in a multisite environment. And I spend most of my time in a multisite environment.

There is hope, however. With Apache, it’s entirely possible to make the end users for one, many, or all of your sites think they’re getting the file at misc/jquery.js, but really give them the new and improved version. And I’ve done all the dirty work. Add a few lines to your multisite (or single site) .htaccess file, and you’ve got the jQuery update without changing Drupal core.

Here’s the .htaccess snippet I use to pull off the deception:

RewriteCond %{HTTP_HOST} example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} justinhileman.info$ [NC]
RewriteRule ^misc/(collapse|jquery|tableselect|upload).js$ sites/all/modules/jquery_update/misc/$1.js [L,QSA]

How to use your new superpowers:

  1. Add the above snippet to your .htaccess file AFTER the RewriteBase directive but BEFORE the line that says

    # Rewrite current-style URLs of the form 'index.php?q=x'.
  2. Replace the example domains with any of your domain names.

  3. Add a new condition for every site you want to use the jQuery update on.

  4. Note that every RewriteCond line before the last one should end with [NC,OR], otherwise Apache will match the conditions with AND, i.e. nothing will ever match.

  5. Make sure you have jQuery update module installed and enabled on every site you use the rewrite rule for. Otherwise Drupal could get a bit confused.

Also note that I assume your jQuery update module is located in sites/all/modules.

There will be an error in your status log. That’s because, while every external visitor will see the new version handed to them by the fancy Apache rewrite, the jQuery update module is looking at the hard drive of your server and seeing the old core version. So feel free to ignore the error :)

Update: The jQuery update module has changed a bit. It now includes more than just one file… So use the new RewriteRule (posted above) so the rest of your files work…