31

I converted a plain vanilla HTML page to HMTL5/CSS3 with a responsive layout, and for security reasons (dictated by the security people) the page must never cache.

The page previously used <meta http-equiv="Pragma" content="no-cache"> and <meta http-equiv="Expires" content="-1"> to prevent the page from being cached.

What replaces this in HTML5? How do you prevent an html page from caching in the client?

I've spent a week reading about manifest files, but they seem to do exactly opposite of what I want as attaching a manifest file explicitly causes the page it is attached to to cache.

And please don't refer me back to the w3c definition of which meta elements are now allowed — I understand that HTML5 does not include the cache-control or Pragma in meta elements.

I need to know what it does include that will prevent a page from being cached.

1

4 Answers 4

36

In the beginning of code you need to use this:

<!DOCTYPE html>
<html manifest="manifest.appcache">
...

Then create manifest.appcache with such content:

CACHE MANIFEST

# Cache manifest version 1.0

# no cache

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

7 Comments

And to add to @n1ckolas answer, check out w3.org/TR/html5/browsers.html#manifests for specifics.
Thanks, that work to prevent the file from caching, but now it pops up a info bar in Firefox stating: "This web site is asking to store data on your computer for offline use." Which again is exactly the opposite of what I want to happen. Very annoying and will not be too user friendly for our novice users -- we cater to healthcare professionals who are less than Internet savvy. Anyway to keep the manifest and ditch the info bar or is this just the way it is with firefox?
I'm not actually sure... You can move it into separate question, I'd glad to see the answer :)
an equivalent solution is to reference a manifest file that doesn't exist: <html manifest="/doesnt-exist.appcache"> but this will show up in the console in chrome developer tools.
This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Avoid using it and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time. developer.mozilla.org/en-US/docs/Web/HTML/…
|
5

I dislike appcache a tremendous amount. It almost works well but can be a real unforgiving pain. While doing some code refactoring, I realized that after logout, i could browse back to the the last page. Of course, refreshing browser it would force user back to login but this is not desired.

After searching around and seeing the options, I started to get a bit frustrated. I do not want to use appcache. I then realized that my code was redirecting to login page after destroying the session and got an idea, what if, I redirect to the home page instead? And voila, page was loaded, session checked (and of course not there), user redirected to login. Problem solved.

Comments

5

I have been struggling with the same issue for quite some time. What works for me - at least so far - in Chrome, FF and IE is doing the following:

1) reference the manifest file <html lang="nl" manifest="filename.appcache"> From what I understand, this will cache everything that follows in this HTML document, hence a manifest file is needed to prevent this from happening:

2) use a manifest file filename.appcache with the following content which basically says: for all files, do not read from cache but from network server:

CACHE MANIFEST
# 2015-09-25 time 20:33 UTC v 1.01 
NETWORK:
*


3) a third step is required: each time you upload a (partial) update of your website, also change the manifest file by changing the date and time stamp in the comment(#) line. Why? Because if you do not change the manifest file, it will not be read and it will default to step 1 and thus cache and read from cache. The fact that the manifest file is changed, however, enforces the manifest file to be read again, and thus enforces that the "do not read from cache but read from network server" instruction therein, is applied again.

1 Comment

Beware, this Cache Manifest feature has been removed from the Web standards.
1

The previous answer may not consistently work to prevent caching or to clear existing cache in chrome but there is a work around.

1) To clear existing cache in chrome, it may be necessary to update all files of the website (eg by linking to a new css file on every page) along with an update of the cache-manifest before existing cache in chrome gets cleared upon the second visit of a page (because of the "flow" of the way in which a page is rendered: the first time a page is visited, the browser reads and caches the manifest , while proceeding with loading from existing cache. Only upon the second visit will the newly stored updated manifest be read and applied).

2) and if none of that helps, it is possible to include a script in the manifest file itself to check for a new manifest and if found, reload it and use the new manifest. This did the trick and resolved all remaining cases I tested for where files had remained persistently cached in chrome. I found this script on this page by Jason Stimpel.

<script type="text/javascript">
window.addEventListener('load', function (e) {
window.applicationCache.addEventListener('updateready', function (e) {
        window.location.reload();
    }, false);
}, false);
</script>

Comments

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.