tags:

views:

203

answers:

3

Hi all,
I'm developing a PHP site that serves static content from a cookie free domain (Thanks SO Blog!), this domain serves content with a high cache which among other things means that I'm unable to change content (JS, CSS and images) without invalidating that cache. Currently I do this by appending ?revision (e.g. style.css?19) to the end of the static URL, currently I'm doing this manually which is obviously far from convenient.

What would the recommended way to complete this be? I'm using Subversion and from my understanding there's various tools I can use to grab the revision number - but how would I go about getting this into PHP (it doesn't seem very performance wise to run this every page load?). I'm guessing I should most likely be using some sort of build system.

Any recommendations would be greatly appreciated.

Thanks

+3  A: 

SVN keyword substitution may be the solution. You would use it like this: first add the following code to your PHP code, somewhere near the place where you want to use the version. Like this:

$svn_version = '$Revision$'; // remember to use single quotes!

Now commit that file. And then re-open it. Observe that the above line has now changed to look something like:

$svn_version = '$Revision: 66232 $';

SVN recognized the keyword tag and replaced it with the revision number. This tag will from now on be updated with the latest version number every time you commit that file, it will also contain the version number when you do a checkout or export.

Now, you would just have to do a little string manipulation to get that number out of this string. I'm sure you can figure that out yourself.

Of course, this wont work, when you update the actual css/js files without touching PHP. And that cannot easily be solved. You could change PHP every time you change CSS/JS. Or yes inded, you could go for some build system.

Anyway, I hope you got some ideas out of this.

Anti Veeranna
Thanks for the idea Anti, but as you said the PHP file would have to be updated also, which is often not the case (there's 10's of files that reference to static content); I may as well edit the number in manually.
Adam Gibbins
but then, what about adding the current timestamp to the end of the static url? this would effectively bypass the cache.
Anti Veeranna
Current timestamp? But then the cache would be invalidated every page load, I only want it changed when the static stuff actually changes.
Adam Gibbins
+1  A: 

An idea is to have a text file which only contains a number (i.e. the revision number). Every update, you increment this number (as a SVN version ID or just a counter, as you want).

So you've just to proceed as you proposed ; you read the number in the file, and pass it as GET argument to your CSSs or other files. One implemented, you have just to update the revision number (and maybe clean the cache if you use it) to update on every page. Quite easy :)

swordofpain
+1  A: 

There is an excellent PHP script and set of .htaccess which can be used for versioning of CSS / Javascript files. It works by rewriting all CSS / JS requests to a file, combine.php, which then checks to see if the file has been modified. If it has, it creates a new cached version of the file. If it has not been modified, it can simply send a previously cached version of the file. This script also has the benefit of minimising the number of http requests, which is another performance improvement.

Images are more difficult - at a basic level you could search / replace through your css files for image names and update them. This could be incorporated into a build script. SASS is a css processing system that supports variables within CSS, meaning you would only need to update image names in one place.

Finally, I have noticed when using a tool such as Fiddler, if a query string is included in a Request, the browser can ignore expires headers and contact the server anyway, so it may be better to place in a subdirectory, ie instead of

/css/main.css?16

/css/16/main.css
timmow