10

I thought I was having an issue with my javascript being cached and not updated even with an updated version tag, like:

<script type="text/javascript" src="lib/myScript.min.js?v=3"></script>

But I realized that the problem is with my html file is being cached... so the browser doesn't even know there is a new script file.

I don't want to disable caching, but isn't there a way to let the browser know it doesn't have the most up-to-date html file? (And is this something I'd put in my html file, or on my apache2 server?)

Sam Mirrado
  • 336
  • 1
  • 2
  • 9
  • You'd be better off configuring the cache. – Alex Sep 05 '12 at 10:43
  • Possible duplicate of [How to control web page caching, across all browsers?](http://stackoverflow.com/questions/49547/how-to-control-web-page-caching-across-all-browsers) – Josiah Yoder Feb 18 '17 at 17:22

3 Answers3

5
<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" />

Answer from Using tags to turn off caching in all browsers?

Community
  • 1
  • 1
Eirik H
  • 654
  • 2
  • 8
  • 30
  • 1
    Thanks - I guess if I want some level of caching, then I can set an expires tag to expire right before my expected update. – Sam Mirrado Sep 05 '12 at 11:05
  • 10
    will this stop caching of everything on the site like javascript css and images? or just the html page? – Tomer Shemesh Jul 09 '15 at 21:07
  • Note that as of HTML5 using a cache-control `meta` tag is invalid. Cache control should be specified in the HTTP response headers instead. – sallf Oct 21 '20 at 14:42
3

You can try these meta tags.I think it will solve your problem.

<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'>
Gokcen
  • 31
  • 1
2

Besides writing explicitly in html, you actually have two better options: ETAG and Last-Modified. If your html file is a static file, then apache2 will know how to handle its cache, by default. if it's php, then you will have to handle it in your code, or use some php framework.

Since these two headers are not written in html, browsers don't have to download the whole body of HTTP response, and thus reduce traffic. So I suggest you use them.

I believe a little googling may help.

What takes precedence: the ETag or Last-Modified HTTP header?

Community
  • 1
  • 1
OpenGG
  • 4,345
  • 2
  • 24
  • 31
  • I may have selected an answer too quickly... I didn't know what to search for. Looking closer at my apache config, I don't have mod_cache, mod_headers, or mod_expires turned on... but not quite sure which one(s) I need. I just found http://www.mnot.net/cache_docs too. Thanks! – Sam Mirrado Sep 05 '12 at 11:21
  • 1
    Just google apache2, ETAG and Last-Modified . These two features are turn on by default, you can use wget/firebug to display headers. `wget --spider --server-response http://localhost/` – OpenGG Sep 05 '12 at 11:28