1

We are hosting a PHP application with multiple javascript files. Currently we bust the cache by appending a get parameter (timestamp) to the url of the javascript files, ie: http://ourserver.com/scripts/something.js?cachebust=1289438903

As the number of files increase the performance of the application decreases. What are the various solutions we could use to mitigate this problem, from js build systems to apache2 configurations? What options require the least amount of changes in our application code and our current development workflow?

Sean
  • 537
  • 1
  • 6
  • 15
  • I am not fully clear on what the problem is. You are having problems with items not being cached the way you expect them to be? Are you intentionally trying to not cache your javascript files for some reason? – Mike Brant Dec 17 '12 at 22:17
  • 3
    If you want JS to be cached, simply STOP doing this. – Diodeus - James MacFarlane Dec 17 '12 at 22:18
  • 2
    I assume the cachebust timestamp is the time the file last changed (or maybe the time of the last deploy)? If it's the current time that'd be bonkers! – John Carter Dec 17 '12 at 22:21
  • I agree with @Diodeus. You need not append a different url param for every request. You do this only if there's an update to the js file and you want the client to start using the new updated version of the js. – Sandeep Dec 17 '12 at 22:22

1 Answers1

2

What we do is have a php script that takes the files that need to be minified and combined and creates an url of those. The script does that and also creates a hash of the concatenation of the version numbers of all the files. The hash is appended to the url as a &hash=<hash> parameter. The script generated something like this:

http://path/to/minifier/script.php?files=core.js,somecode.js,someothercode.js&hash=da39a3ee5e6b4b0d3255bfef95601890afd80709.

This is sent tot the client and the browser does the request. The minifier script takes the parameters, minifies them (or takes them from cache), concatenates them and sends them back. Because the hash is a function of the version of the scripts it will be renewed when one of the files change. And because the version only changes when the file changes no unnecessary cache busts will take place. There are multiple scripts that do this. We use wro4j (in java), but php scripts exist.

Edit: To be able to bugfix in production we also have a parameters debug=true that will tell the minifier script to not actually minify and just return the concatenated script.

Koen Peters
  • 12,798
  • 6
  • 36
  • 59
  • Do you shell out to some sort of build program such as yui minifier or do you perform the concatenation and minification in php? – Sean Dec 20 '12 at 18:35
  • What we do is concatenate the minified files at runtime, but cache the minified files. Concatenation takes hardly any time, minifying does. And because we use CMS we want to prevent to have to kick off some buidl process every time the end users changes a js or css file. We use dotCMS as our CMS and have open sourced our minification plugin for that platform. I wrote up a more detailed explanation and some tips and tricks we have found since we have developed the solution. You can find that and the source code here: http://geekyplugins.com/dot-cms/minifier.dot – Koen Peters Dec 21 '12 at 08:35
  • By the way: the hashing part of the solution has not been released in the plugin on the site I linked to. That will be added shortly. – Koen Peters Dec 21 '12 at 08:48
  • The hashing part has been released late last year. The java dotCMS solution we provide on geekyplugins works now as described. – Koen Peters Jan 10 '13 at 20:41