Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I force a file type to not be cached?

Status
Not open for further replies.

Einstein47

Programmer
Nov 29, 2001
737
US
I have asked this in the JavaScript forum, but here seems to be a better place.

I have an external javascript file (.js) that I have updated, but in testing I have found that many of the clients' browsers are reading the cached version of the .js file. This is bad because it is causing servlet errors when I know that I have fixed that functionality.

I have been looking up ways to better control the caching of files by using the ExpiresByType directive. However, I do want the .js files to be cached. I really want the caching to determine if the file has been modified since last access and if so then force the client's browser to get the new file. Right now this isn't happening.

I fear that when I deploy the app, the help desk will get flooded with calls when all they users will need to do is force-refresh their browsers with SHIFT+Reload. Or clear their local cache.

I would much rather control this on the server than with each individual client. Any advice would be appreciated.

Einstein47
(Some people make things happen. Some people watch things happen. Some people wonder what happened. - Unknown)
 
You can use the cache-control headers, but a lot of browsers ignore it.
 
What I ended up doing that works as I need it is as follows:
I had to include LoadModule mod_expires.so and AddModule mod_expires.c
I then added the following to my VirtualHost section:
ExpiresActive on
ExpiresByType application/x-javascript "modification"
ExpiresDefault "modification plus 1 day"


I then restarted my http server, and touched the *.js files to make sure they had new timestamps.

When I got to the page that included the external files the first time they were reloaded from the server (with a 200 code in the access_log). and then when I got to another page that included them, they were read from the cache (304 in the access_log).

I need to make these changes to the production server, but that means that the files will be able to be updated and loaded into the visitors browser correctly. WooHoo! This is good news for me.

Einstein47
(Some people make things happen. Some people watch things happen. Some people wonder what happened. - Unknown)
 
I think if u put <meta http-equiv="PRAGMA" content="NO-CACHE"> On each HTML Page it wont caache it or just <CONTENT="NO-CACHE">
 
That's my problem - I was already using the PRAGMA / NO-CACHE tag. It wasn't affecting the external JavaScript files. I had put a newer .js file out on the server, but my pages were continuing to use the file that was in my local cache.

The problem wasn't with the HTML on my pages, but the way the Apache server was handling the caching of various external files (images, .css, .js, etc.). There are certain files you want to be cached even though you want your HTML pages to not cache so they give the user the most current information.

I'm glad I found it was very helpful.

Einstein47
(Some people make things happen. Some people watch things happen. Some people wonder what happened. - Unknown)
 
I had a similar problem with an application I helped write. We found that certain routers would only pass on the HTTP headers of returned files, but not the message body. This may not work in all cases, but it solved the problem we were having. In the httpd.conf file, I added the following:

Header add Cache-Control: "no-store, no-transform"

Works like a charm now. :)

Best of luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top