overstimulate

The solution to Web apps + AJAX = distribution problem?

Thu, 08 Sep 2005 javascript rails comments

A better solution to fix the caching problem:

I added some lightweight code in my template to check the modified time of all css/script files, and link to them in the form "filename?mtime". The extra argument is ignored by the web server, so you don't have to rename the files, and since its based on the modification time the link only changes when the file does.

Thanks to Abe, whom I met while visiting JotSpot to talk about Dojo.

In rails, you can create/modify the stylesheet_link_tag, javascript_link_tag commands to use the File.stat command and append the ?#### to the link url.

  def my_javascript_tag( script )
    script_ver = File.stat( RAILS_ROOT + "public/javascripts/#{script}.js" ).mtime.to_i
    "<script src=\"/javascripts/#{script}.js?#{script_ver}\" type=\"text/javascript\"></script>"
  end

Responses to "The solution to Web apps + AJAX = distribution problem?"

  1. Fri, 09 Sep 2005 Abe says:
    I'm glad you liked that solution :-). And it's cool to see how easy it is to implement in rails!
  2. Fri, 09 Sep 2005 Alex says:
    Brilliant! It's so simple - I am always amazed at what a fresh perspective on a problem can bring. Even for an issue that on the surface seems so trivial. I like this solution much better. Thanks Abe for the idea and to Jesse for reporting it here - I totally missed Abe's original post in the comments.

Leave a response

My Card Add to your Address Book

Jesse Andrews
open source, web browsers, web services, web sites & folk dancing. contacts/sites

Keep Up To Date

Get updates via RSS or
get email when I blog

Previous Blog Posts