Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent caching of HTML page [duplicate]

Tags:

html

caching

I have a HTML page. The problem is that I do not want to have the users to refresh the page each time I put on new content.

I have the following code in order to make sure that the page is not cached:

     <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"/>      <meta http-equiv="Pragma" content="no-cache"/>      <meta http-equiv="Expires" content="0"/> 

The problem is, I still need to do a refresh on the page in order to get the most current content to show up. Am I doing something wrong? Should I be using some other tags?

like image 681
Nate Pet Avatar asked May 23 '13 14:05

Nate Pet


People also ask

How do I stop HTML from caching?

Here's how... When you're in Google Chrome, click on View, then select Developer, then Developer Tools. Alternatively, you can right click on a page in Chrome, then click Inspect. Click on the Network tab, then check the box to Disable cache.

How do I stop caching problems?

Setting a short cache time By asking the Web browser to only cache the file for a very short length of time, you can usually avoid the problem. That's all it takes. The next time the visitor views the file, more than a second will have passed, so the browser won't use the outdated cached copy.

What is http equiv cache-control?

An alternative to the Cache-Control HTTP header is to use META HTTP-Equiv tags. These tags produce the same result as the equivalent HTTP header when used within your channel pages and are processed by the M-Business Sync Server and displayed in M-Business Client.

Can HTML be cached?

Implementing user personalization in scripts allows caching of the page's HTML. Then the scripts can modify the page after loading asynchronously. Beyond using JavaScript for personalization, The Gap is caching HTML.


2 Answers

The Codesnippet you showed makes the browser load the website everytime it accesses it, which is useful if you perform frequent updates, but still have a static page.

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"/> <meta http-equiv="Pragma" content="no-cache"/> <meta http-equiv="Expires" content="0"/> 

In case you want it to perform live updates, like it does for example in a (g)mail account, you need to make it refresh (parts of the page) itself. Use Javascript in this case, like it is shown in this question or an ajax call.

$('#something').click(function() {     location.reload(); }); 
like image 62
Garrin Avatar answered Sep 22 '22 04:09

Garrin


The values you have there are OK, but meta http-equiv is highly unreliable. You should be using real HTTP headers (the specifics of how you do this will depend on your server, e.g. for Apache).

like image 27
Quentin Avatar answered Sep 24 '22 04:09

Quentin