Amazon.com Widgets
Twitter » Linkedin » Flickr » DjangoPeople » Feed

This site is now an archive. New posts will be written at gregnewman.org. Please follow the new site and update your feeds.

...not so private reflections of greg.newman
RSS feed - Categories & Search

Django Cachebuster

In a chat with James Tauber he mentioned he needed a cachebuster and for a few projects including Pinax I need to ensure my css and javascript edits are not being cached in the browser. This is a standard feature in Rails but not in Django so I whipped up a simple setup of tag helpers to do the trick.

Introducing the stupidly simple django-cachebuster written while watching the Football Sunday. I'll probably continue to add new tags to simplify my projects. Ideas are welcome.

For now it has two helpers, css_tag and js_tag. Using the template tags will return a full css or javascript tag with the filename and a sha1 hash appended based on the name and edit date of the file. When the file is changed the hash is updated.

Usage

I didn't like the way the code blocks formatted so I'll just let you read the "Usage" over at github.

6 Comments

According to Steve Sounders, the author of "High Performance Websites" and creator of YSlow, it's better to use the path to do this than the query string. Here's the article with the details: http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/

Interesting read, thanks Dan. I will definitely take that into consideration!

super nice greg. I'll bet you'll see some requests come in for generating different types of css (ie. print, etc) busters and then you'll have to go beyond Django's awesomse simpletags and into more the advanced template tag. I'm going to use this on a project starting today. kudos!

Yeah, i've already thought about print Kevin. Good point.
I'm also thinking about images, but they are not as much a priority as css was.

Dan,
Revisiting your comment, I read through that article and that's a very good point he makes and an excellent solution for those of us who have root access to the server to make those kind of changes and additions to the config files. Thanks again!

I've postes my solution on a Wiki page here: http://www.django-hosting.de/wiki/MediaFileExpire/

The description is in German, but the docstring in the code-listing is detailed enough to understand how and why it works. I use this tag to solve the querystring-issue. What do you think about this aproach?