Welcome to the new design! Please report any bugs or issues, thanks :)
Web Dev + WordPress + Security

How to Cache Mint JavaScript

[ Mint Icon ] Recently, I spent some time addressing a few of the performance issues pointed out by Yahoo!’s very useful YSlow extension for Firebug. Working on performance tip #3, Add an Expires or a Cache-Control Header, I encountered some difficulty while trying to get the JavaScript used by Mint to cache as desired. Apparently, the HTAccess directives used to cache my other scripts do not effect the two PHP-generated JavaScript files used by Mint.

Although I am not entirely certain, I suspect that these files are not being cached along with other scripts because the way in which they are called via query string parameter (Apache is far from perfect when it comes to dealing with anything associated with query strings). In any case, I really wanted to have these Mint scripts cached by the browser, so I ended up editing two files in the Mint core to make it work. For those of you struggling with the same dilemma, here’s how I did it..

Update! This post was written many months ago under the erroneous assumption that caching Mint’s JavaScript was a good idea (for Y-Slow compliance, performance, et al); however, after a brief chat with the man himself, Shaun Inman, I was quickly informed that this was a bad idea: caching Mint JavaScript files will cause Mint to stop functioning. But, for what it’s worth, and for the sake of retaining potentially useful information, I present the original article here for your amusement.. </update>

Cache Mint JavaScript

The two JavaScript files used by a default installation of Mint 2 are called from record/js.php and record/index.php. These two files are not cached by default, and require the addition of explicit expires headers in order to cache properly. To implement expires headers for the two default Mint JavaScript files, first open the record/index.php file and edit the following PHP code as indicated below (beginning on line 15):

Change this:

// Prevent caching (was in js.php)
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

..to this:

// Enable caching (at your demise!)
$offset = 365 * 24 * 60 * 60;
$expire = "expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT";
header ($expire);

For the second file, record/js.php, add the same code just below line 15:

// Enable caching (at your demise!)
$offset = 365 * 24 * 60 * 60;
$expire = "expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT";
header ($expire);

And that should do it — YSlow will now be happy, but unfortunately (as discussed in the note above) caching these two files will result in Mint failing to record data. Have fun, and.. be careful with that cache, Eugene!

Jeff Starr
About the Author
Jeff Starr = Creative thinker. Passionate about free and open Web.
USP Pro: Unlimited front-end forms for user-submitted posts and more.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
GA Pro: Add Google Analytics to WordPress like a pro.
Thoughts
Checking in to anyone listening. Stay safe. Pay attention. Don't get lazy.
What's up with Plesk UI lately? Especially on Chrome it looks just awful, all kinds of broken. Come on Plesk devs get it together.
Things get stressful, I try to pray. Not always easy, but always helps to relax and regain focus.
Nice new speed checker at fastorslow.com.
Easy way to exclude certain tests from WP Site Health: Site Health Tool Manager
Excellent (and free) tool for getting tons of site SSL infos: whynopadlock.com
Everyone just stay home and hide forever. Brilliant idea.