Don’t Use jquery-latest.js!!!!!!!1

I’m guilty.

So guilty.

For years I have pointed pages to jquery-latest.js. Of course, I don’t think anyone would classify hack.premo.biz as a “production site.” I just want the latest damn version of code. I like to experiment with new features.

But I hate babysitting blogs, downloading point releases, and uploading them to my web server. So I used jquery-latest.js.

But now jQuery has taken this away. Sort of. jquery-latest.js is now frozen in time at version 1.11.1.

Well that’s no good. I can’t putter around on an old version for the rest of my life. But I hate babysitting blogs. And downloading point releases. And uploading them to my web server.

What’s a modern coder to do?

Scrape that s.

I made a two part solution. Part one scans the file structure on includes.premo.biz for files that look like jquery 2.x and returns the newest one it finds. It also sends a custom header so the browser reads it as JavaScript. I’m not going to give the URL here because I don’t want to be the next jquery-latest.js. But here’s the (PHP) code:


header( 'Content-Type: application/javascript' );
$contents = scandir( './' );
foreach ($contents as $file) {
if ($file !== '.' && $file !== '..') {
$ext = '';
$parts = explode('.', $file);
$ext = $parts[count($parts) - 1];
if ($ext == 'js') {
if ($parts[0] == 'jquery-2') {
include $file;
}
}
}
}

The second part is more complicated. It pulls in the jQuery Blog RSS feed, looks for a post about a new release, reads the version number, determines if it is newer than the version my server already has and if so pulls it down, then archives the older version. Here’s the PHP for that:


// run on cron.
// check jquery blog rss for updated version news
// http://blog.jquery.com/feed/
header('Content-Type: text/html; charset=UTF-8');
// what's the latest version we've got? (current?)
$ourVersion = '0';
$contents = scandir( './' );
foreach ($contents as $file) {
//if( strpos( $file,'.' ) !== 0 ) {
if ($file !== '.' && $file !== '..') {
$ext = '';
$parts = explode('.', $file);
$ext = $parts[count($parts) - 1];
if ($ext == 'js') {
if ($parts[0] == 'jquery-2') {
//include $file;
$ver = $file;
$ver = str_ireplace('jquery-', '', $ver);
$ver = str_ireplace('.min.js', '', $ver);
$ver = str_ireplace('.js', '', $ver);
$ourVersion = $ver;
}
}
}
}
echo 'ourVersion : ' . $ourVersion;
$rssString = file_get_contents('http://blog.jquery.com/feed/');
$xmle = simplexml_load_string($rssString);
//print_r($xmle);
$articles = $xmle->channel->item;
foreach ($articles as $article) {
$titleLength = strlen($article->title);
$check = trim(substr($article->title, (strlen($article->title) - 8)));
if (trim(substr($article->title, (strlen($article->title) - 8))) == 'Released') {
if (strstr($article->title, 'RC')) {
continue;
echo 'release candidate. BOO!';
}
if (stristr($article->title, 'beta')) {
continue;
echo 'beta. BOO!';
}
echo 'RELEASE POST';
echo $article->title;
// find '2.'
$words = explode(' ', $article->title);
$ver = '0';
foreach ($words as $word) {
if (substr($word, 0, 2) == '2.') {
$ver = $word;
if (strlen($ver) == 3) {
$ver = $ver . '.0';
}
echo 'version : ' . $ver;
if ($ver > $ourVersion) {
echo $ver . ' is greater than ' . $ourVersion . '!';
$jqFilename = 'jquery-' . $ver . '.min.js';
$jqUrl = 'http://code.jquery.com/' . $jqFilename;
echo $jqUrl;
// http://code.jquery.com/jquery-2.1.1.js
$jqueryContents = file_get_contents($jqUrl);
file_put_contents($jqFilename, $jqueryContents);
// move old version
rename('jquery-' . $ourVersion . '.min.js', './jquery_archive/jquery-' . $ourVersion . '.min.js');
break 2;
} else {
echo $ver . ' is NOT greater than ' . $ourVersion . '!';
}
}
}
echo '----------';
}
}

And that’s that. My very own jquery-latest.js.

Don’t Use jquery-latest.js!!!!!!!1