bcrowell2 has asked for the wisdom of the Perl Monks concerning the following question:

O Monks,

I have a perl cgi application that is kind of big and slow, and I'm trying to push as much of the work as possible off onto the client, using javascript. I have a bunch of the code in a particular file, call it foo.js, and the cgi generates html code like this:

<script type="text/javascript" src="foo.js"></script>
The good thing about this is that it gives good performance. Even if foo.js gets pretty big, the user's browser will have it cached, so there won't be any load on the server to keep on sending the code, and the client won't have to wait for the code to come over the internet.

However, it gets awkward when I want to change foo.js, because the user is going to have a cached copy. If the change isn't vital, it's not a big deal -- some users will be using the old code for a while, until their cached copy expires. But it's possible that I will, for example, take more functionality out of the perl cgi and shift it into foo.js, in which case it breaks if the user is using the old, cached version of foo.js.

IIRC, there's some way to configure apache so that it won't cache certain files, but I actually like the caching. Is there some technique I can use in the javascript code that will check whether it's the right version, and fix itself dynamically if necessary? For example, the cgi could emit html code like this:

<script>var version_of_foo_must_be = 2.37;</script> <script type="text/javascript" src="foo.js"></script>
Then foo.js could have code like
var what_version_i_am = 2.36; if (what_version_i_am<version_of_foo_must_be) { // scream and panic }

My real question is what the "scream and panic" should be. It could just display a message to the user, like "Please clear your cache," but that would probably confuse a lot of users. Is there any way I could just dynamically make the code update itself?

TIA!

Ben

Replies are listed 'Best First'.
Re: cache js code, but detect when it's changed?
by Corion (Patriarch) on Oct 06, 2007 at 18:03 UTC

    I think the traditional way is to encode the version in the URL:

    <script type="text/javascript" src="/js/foo/2.37/foo.js"></script>

    That way, when you need a new version, you automatically get it. You should make all older version URLs redirect to the newest version.

      That's sensible -- thanks.

      I'd kind of like to avoid the need to rename files a lot, but this would certainly work. It just seems as though it ought to be possible to do something ajaxy that would get around this. E.g., the js script could detect that it was out of date, and then download the new code. But I guess no matter what, I still have to remember to change the version number in at least two places.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: cache js code, but detect when it's changed?
by Cop (Initiate) on Oct 06, 2007 at 18:16 UTC
    <script type="text/javascript" src="foo.js?1.0"></script>

      Some browsers will not correctly cache URLs with a query, it's better to have the version be part of the URL. Here's what I did to one site:

      I have the javascript in a folder called /js/ and I have a symbolic link to that folder with the version number, so the application spits out:

      <script src="/js4/foo.js"></script>

      He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.
      Chady | http://chady.net/
      Are you a Linux user in Lebanon? join the Lebanese GNU/Linux User Group.

        Hey, I am interested in this, do you mind point out the browser that does that? thanks.

A reply falls below the community's threshold of quality. You may see it by logging in.