ChatGPT解决这个技术问题 Extra ChatGPT

Is there a <meta> tag to turn off caching in all browsers? [duplicate]

This question already has answers here: How do we control web page caching, across all browsers? (29 answers) Closed 5 years ago.

I read that when you don't have access to the web server's headers you can turn off the cache using:

<meta http-equiv="Cache-Control" content="no-store" />

But I also read that this doesn't work in some versions of IE. Are there any set of tags that will turn off cache in all browsers?

a combination of bobince and dpb's answers is your best bet. covers all bases.
i18nguy.com/markup/metatags.html On this site is written to preferable deactivate caching in HTTP, so that the site isn't stored on intermediate servers. Thought it might help someone.
For those struggling with back button and "re-opening closed tab" caching as I am, have a look at this answer from another question. It's hacky, but no header-based solution was working for me and for my purposes this little JS snippet is great (easy to convert to plain JS).
The answers here are all sad. I would add my own, but this is closed. According to MDN: developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control you do in fact most likely want to use <meta http-equiv="Cache-Control" content="no-store"/> as in the question.

C
CommonKnowledge

For modern web browsers (After IE9)

See the Duplicate listed at the top of the page for correct information!

See answer here: How to control web page caching, across all browsers?

For IE9 and before

Do not blindly copy paste this!

The list is just examples of different techniques, it's not for direct insertion. If copied, the second would overwrite the first and the fourth would overwrite the third because of the http-equiv declarations AND fail with the W3C validator. At most, one could have one of each http-equiv declarations; pragma, cache-control and expires. These are completely outdated when using modern up to date browsers. After IE9 anyway. Chrome and Firefox specifically does not work with these as you would expect, if at all.

<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />

Actually do not use these at all!

Caching headers are unreliable in meta elements; for one, any web proxies between the site and the user will completely ignore them. You should always use a real HTTP header for headers such as Cache-Control and Pragma.


More explanation would be nice. Why the repeated cache-control and expires? Why do you need all of these? What's so special about 1980? What's the difference between pragma:no-cache and cache-control:no-cache? More explanation would be nice.
Not 100% sure on this, but I think the repeats are intended to handle different browsers.
Closest I found to an explanation: i18nguy.com/markup/metatags.html
Sometimes we need to break some Validator rules in order to make things work on all browsers.
The list is just examples of different techniques, it's not for direct insertion. If copied, the second would overwrite the first and the fourth would overwrite the third because of the http-equiv declarations AND fail with the W3C validator. At most, one could have one of each http-equiv declarations; pragma, cache-control and expires.
k
katzbatz

According to Independent Security Evaluators' great case study on the industry-wide misunderstanding of controlling caches, only Cache-Control: no-store is recognized by Chrome, Firefox, and IE. IE recognizes other controls, but Chrome and Firefox do not.


I found that Chrome responds better to Cache-Control: no-cache (100% conditional requests afterwards). "no-store" sometimes loaded from cache without even attempting a conditional request. Firefox responds better to "no-store" but still sometimes loads from cache if you reload immediately afterwords. What a mess!
b
bobince

It doesn't work in IE5, but that's not a big issue.

However, cacheing headers are unreliable in meta elements; for one, any web proxies between the site and the user will completely ignore them. You should always use a real HTTP header for headers such as Cache-Control and Pragma.


@bobince, Thanks! I'll keep this in mind if I have any issues with web proxies, but my "team" keeps me completely on the front-end and give me no access to the headers.
Even when this was written in 2009, bringing up IE5 compatibility was irrelevant.
Doesn't work in IE2 either, lol.
K
KJ Saxena

pragma is your best bet:

<meta http-equiv="Pragma" content="no-cache">

...this is old, so presumbably your suggestion is that this is because in newer implementations this will typically be interpreted as the cacheing header cache-control: no-cache. So actually you'd be better to use the more modern
K
Kathir

I noticed some caching issues with service calls when repeating the same service call (long polling). Adding metadata didn't help. One solution is to pass a timestamp to ensure ie thinks it's a different http service request. That worked for me, so adding a server side scripting code snippet to automatically update this tag wouldn't hurt:

<meta http-equiv="expires" content="timestamp">


this really is clever ot workss also on chrome
o
orf

Try using

    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Expires" CONTENT="-1">

Why to try it? Can you explain?
Some explanation would be nice ...
It doesn't work on Chrome.