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.

4
  • 2
    the browser does this automatically. why need JS to do it for you? Commented Mar 18, 2012 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. Commented Mar 18, 2012 at 22:25
  • You may want to read about 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. Commented Mar 18, 2012 at 22:26
  • you can take a look at stackoverflow.com/a/9410367/575527, where i listed down ways to prevent cache and another article about cache busting html5boilerplate.com/docs/Version-Control-with-Cachebusting Commented Mar 18, 2012 at 22:28

1 Answer 1

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.

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

2 Comments

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?
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.

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.