views:

68

answers:

3

I'm currently playing around with a build / deployment script for minifying static resources. Following good practice I'd like to set an expire header far into the future for most of my javascript, stylesheet and images.

To my question, when one or more of the static files has changed clients should ask for the newest version of the file. Will adding something like /css/style.css ?1235 after the url be enough to trigger a new request? Or do I have to rename all my static files for each build (something like /css/style _12345.css)?

Update: Just to clarify, the reason that I'm asking is that I've noticed that a lot of other deployment scripts seems to take the "hard" path by renaming each file.

+3  A: 

Will adding something like /css/style.css ?1235 after the url be enough to trigger a new request?

Yes, of course.

You can do it like this:

style.css?UNIXTIMESTAMP

where UNIXTIMESTAMP is the time when the file was created in unix timestamp format

Balon
I'm probably going to use the revision number from my vcs.
Kimble
Just remember that "/css/style.css" is different than "/css/style.css?12345" inside of the browser cache. What that means is that if pages A and B both link to the former, if you update page A with the latter, page B will still point to the former. Then if you change page B to be "/css/style.css?6789" you'll have a third copy in the cache.
Chris Haas
A: 

if you can use mod_rewrite i add a rule like this:

RewriteRule ^/((.*)_[0-9]+(\.(?:js|css|swf).*))$ /$2$3 [NC,L]

which rewrites any javascript, css or flash file back to the original name so

/css/style_1234.css

gets rewritten back to

/css/style.css
Josh
Overkill!!!!!!! There is no need to do it in this way!! All you need to do is that what I wrote in my answer. Nothing more. Simple add `?something` after the name of the file.
Balon
for a large dynamic site you can program the "_number" to follow a version number and so update a number of files at the same time to the latest version without worrying about caching issues.
Josh
You are right, I've forgot about caching issues. Anyway, as you said, it can be best solution for "a large dynamic site".
Balon
@Balon Overkill, but perfectly acceptable, and it'll work just fine.
ceejayoz
+2  A: 

RFC 2616 3.2 Uniform Resource Identifiers says:

As far as HTTP is concerned, Uniform Resource Identifiers are simply formatted strings which identify--via name, location, or any other characteristic--a resource.

So, http://foo/bar is a different resource to http://foo/bar?baz.

Some URIs are treated as equivalent, so http://foo/bar?baz is the same resource as http://foo/bar?BAZ (see 3.2.3 URI Comparison).

HTTP does contain some caching exceptions for handling the query part (see 13.9 Side Effects of GET and HEAD). I understand these to only apply to the exact same query.

McDowell