views:

437

answers:

2

I have an old version of a JS file cached on users' browsers, with expiration set to 10 years (since then, I have learned how to set expires headers correctly on my web server). I have made updates to the JS file, and I want my users to benefit from them.

  • Is there any way my web server can force users' browsers to clear the cache for this one file, short of serving a differently named JS file?
  • In the future, if expires headers are not set correctly (paranoia), can my JS file automatically expire itself and force a reload after, say, a day has passed since it was cached?

EDIT: Ideally I want to solve this problem without changing HTML markup on the page that hosts the script.

+2  A: 

You can add something to the end of the source address of the script tag. Browsers will treat this as a different file to the one they have currently cached.

<script src="/js/something.js?version=2"></script>

Not sure about your other options.

Matt
The downside of this approach is that it forces me to change markup. Some other websites have already deployed my script, and I hate having to ask them to change their code.
Bilal Aslam
Profit by writing a confessional blog post about the dangers of making brash content caching decisions. I'll reddit it up for you.
Pointy
+1  A: 

You could add a dummy parameter to your URLs

<script src='oldscriptname.js?foo=bar'></script>

[e: f; b]

The main problem is that if you set up the expiration with a simple "Expires" header, then the browsers that have the file cached won't even bother to contact you for it. Even if there were a way for the script to whack the browser in the head and clear the cache, your old script doesn't do that, so you have no way to get that functionality out to the clients.

Pointy