How to add version number to HTML file (not only to css and js files)

16,014

Usually the browser takes always the newer version of your HTML.

If you wish to prevent any cache you can use those tricks:

The correct minimum set of headers that works across the most important browsers:

Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0

HTML

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

.htaccess (Apache)

<IfModule mod_headers.c>
  Header set Cache-Control "no-cache, no-store, must-revalidate"
  Header set Pragma "no-cache"
  Header set Expires 0
</IfModule>

Java Servlet

response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);

PHP

header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');

ASP

Response.addHeader "Cache-Control", "no-cache, no-store, must-revalidate"
Response.addHeader "Pragma", "no-cache"
Response.addHeader "Expires", "0"

ASP.NET

Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
Response.AppendHeader("Pragma", "no-cache");
Response.AppendHeader("Expires", "0");

Ruby on Rails

response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '0'

Python on Flask

resp.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
resp.headers["Pragma"] = "no-cache"
resp.headers["Expires"] = "0"

Google Go

responseWriter.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
responseWriter.Header().Set("Pragma", "no-cache")
responseWriter.Header().Set("Expires", "0")

source: http://cristian.sulea.net/blog.php?p=2014-01-14-disable-browser-caching-with-meta-html-tags

Share:
16,014
spartan81
Author by

spartan81

Updated on June 25, 2022

Comments

  • spartan81
    spartan81 almost 2 years

    Many people use version numbers on css and js files to force non-cached versions to load up on webpages when updates are published:

    CSS example:

    <link rel="stylesheet" type="text/css" href="style.css?v=2017-03-17">
    

    JS example:

    <script type="text/javascript" src="js/myscript.js?v=2017-03-17"></script>
    

    I've noticed that by adding version numbers to css/js files, the web browsers will also load up the HTML file (from which the css/js files are referenced from) from scratch and not use the cached version.

    Is this a sufficient way of ensuring that HTML-files are displayed from scratch in all web browsers, or is there a way to set a version number to the HTML file as well, to ensure newly updated HTML-documents are not loaded from a browser's cache?