0

My aim is - whenever the client requests a html page or a pdf file, javascript should check if the file has been requested before and is therefore cached. If it is cached and does not match the current one on the server (checksum?), the cached file should be cleared.

This must be done with Javascript only, no PHP or .htaccess magic.

user828591
  • 1,212
  • 2
  • 17
  • 32
  • 2
    the browser does this automatically. why need JS to do it for you? – Joseph Mar 18 '12 at 22:22
  • Well, in my case it doesn't. If I reload that website, it does check for a new version, but only in that case. – user828591 Mar 18 '12 at 22:25
  • You may want to [read about eTag](http://en.wikipedia.org/wiki/HTTP_ETag) which is one part of how a cooperating browser and server validate cached files without any intervention from you. You can force a new version to be downloaded by appending a unique query parameter to the end of the URL. – jfriend00 Mar 18 '12 at 22:26
  • you can take a look at http://stackoverflow.com/a/9410367/575527, where i listed down ways to prevent cache and another article about cache busting http://html5boilerplate.com/docs/Version-Control-with-Cachebusting/ – Joseph Mar 18 '12 at 22:28

1 Answers1

1

This is not javascript programmer task to do things like that. This is the browsers responsibility to manage cache.

Your only responsibility as a programmer (in this case server side programmer) is to make it possible for the browser to distinguish file versions. Most common way to do so is to add a random string to the resource url and change it each time the resource changes. When the browser sees a new url it downloads the resource.

kubal5003
  • 7,186
  • 8
  • 52
  • 90
  • That's what I've been trying before, it usually worked on html files (I just checked it again), but for PDF files (this webproject mostly consists of PDF files) this is not working correctly. I just tried it again, changing the random string worked, but the browser did not load the new PDF. Maybe PDf files, as they're usually a lot bigger than html files, have a different cache rule? – user828591 Mar 18 '12 at 23:13
  • If the pdf file is loaded into Adobe Plugin then the browser won't download the new one just by changing the url - you have to do it manually (like ajax call,...). Check if Adobe plugin has any API that is available for javascript( who knows, maybe). Generally speaking - show the code and then maybe I can help you more. – kubal5003 Mar 19 '12 at 07:19