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


In reply to cache js code, but detect when it's changed? by bcrowell2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.