views:

85

answers:

1

What command line tool can I use to automatically insert mtimes into urls in my css files for the purposes of breaking the cache?

/* before */
.example { background: url(example.jpg); }
/* after */
.example { background: url(example.jpg?1271298451); }

Also, I would like this tool to spit out the latest mtime as the css files mtime. (If the css file is still cached then the new urls will not get to the client.)

In searching the web, I have found very few tools that can do this. I am even considering rolling my own, but have found very little in the way of css parsers that are actively maintained. A candidate should be:

  • fast (I don't want to wait 30 seconds on deployment)
  • command line accessible (something like "cat foo.css bar.css | cssmtime > out.css")

What I've found so Far

  • yui compressor - initially I thought I would extend the yui compressor to do this, but found that it is implemented as a bunch of regex's and not a parser.
  • csstidy - last release was in 2007 and development has been suspended, but does have an option for inserting mtimes (also written in php, something I have no experience in)
  • cssutils - python sac implementation - seems to be actively maintained, but also seems like overkill for my needs. Also, written in python which I have experience with
  • csspool - ruby sac implementation - I don't know much ruby, but would like to learn
  • other sac implementations - There are several java implementations, and a c implementation neither of which I know much about

What's your experience?

Have you used any of these libraries? Was the experience positive? Would you recommend I go with them for my purposes?

A: 

If rolling your own is an option, you could make it a simple find/replace, no need for complex parsing here. For example if you specified which files need re-caching by adding the querystring yourself, you would do this:

.example { background: url(example.jpg?<MTIME>); }

Then your code just needs to find <MTIME> and replace. It would be about as fast as you could get and give you control over images that need re-caching and ones that don't.

Nick Craver
I like this approach actually. I've been using a quick awk script to do find and replace.
brad