8

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?

1 Answer 1

17

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

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

1 Comment

What do you do to bust a cache that was created when the file served did not have these headers?

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.