1

I have two types of JavaScript files. One contains static code and the other contains dynamic code which changes from session to session.

The static JavaScript file should be cached whereas the dynamic one should be cached only for that session and then reloaded In next session. The dynamic JavaScript file is generated once per session and I would like the client browser to cache it for the remainder of session.

How do I force the client browser to request a JavaScript file every session? I know that a common practice is to append a request parameter containing a version number, but one can make only so many updates to a file so that you can manually update JavaScript references. You can't really do that with sessions since there can be multiple sessions per day.

1
  • What server side language are you using? Commented Mar 3, 2011 at 0:45

3 Answers 3

2

I don't see what's wrong with placing a random number at the end of the JavaScript url. For example:

http://www.example.com/myjavascript.js?r=1234

Won't necessarily stop it from cache'n, but if the number is different, the browser will load that js file again.

Sign up to request clarification or add additional context in comments.

3 Comments

But sending proper caching headers would be so much nicer... (I don't know how to do that in Java)
You mean place a counter representing a session? If I used a long, it could theoretically serve up to 9,223,372,036,854,775,807 sessions. That's a lot of number, but doesn't it put a hard limit on the number of sessions that can be served in lifetime?
@Tom: Assuming you have a billion sessions per day, your session ID pool will run out in 25 million years. That may be enough time to think about a better solution
1

Could you append the session id to the JavaScript URL? Assuming you're using JSP, it would look kind of like this:

<script src="/script.js?session=<%= // code to get the session ID %>"></script>

I don't know much about JSP, so I can't help with the specifics, but that should give you a single, unique URL for the session.

1 Comment

Thanks. Hey, I've been using JSP for years and you know better than me!
0

Just appending a session id or a random number to the file name would solve your user experience problem, but it also clogs up all the HTTP caches with useless entries. It should be a lot easier just to set the HTTP 1.1 Cache-Control header in your response to "no-cache". If you're using Java Servlets, it's done this way:

response.setHeader("Cache-Control", "no-cache");  

(If some of your traffic will come from legacy browsers, http://onjava.com/pub/a/onjava/excerpt/jebp_3/index2.html gives some other header settings to really make sure nothing gets cached.)

1 Comment

Wouldn't that disable caching completely? I still want the browser to cache the file in memory for one session only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.