1

I have a website that used javascript and HTML pages. When I update any HTML/javascript, the user doesn't see the change unless he deleted all his cookies, browser cache, and history.

I tried adding the following in the HTML Head section. But it doesn't help. Also, I tried adding "?v=1" for each javascript include. But it doesn't help either.

<meta http-equiv="PRAGMA" content="NO-CACHE" />
<meta http-equiv="cache-control" content="max-age=0"/>
<meta http-equiv="cache-control" content="no-cache"/>

What can be done so the browser won't save website cache etc.?

Charlie
  • 22,886
  • 11
  • 59
  • 90
Misha Groiser
  • 133
  • 11

2 Answers2

0

You should try setting the cache-control headers on your server for your HTML file:

Cache-Control "max-age=0, no-cache, no-store, must-revalidate"

Example article for Apache: Proper cache busting for index.html (Apache). Different servers have different ways of setting these headers, but the HTTP headers are the same for all.

Once that is done, you should still change the v= query param on your internal JS/CSS files, to be sure you bust (or use!) their cached version when required.

Yaelet
  • 160
  • 10
  • what about prevent html pages from caching? – Misha Groiser Jul 03 '19 at 08:52
  • These headers are for any HTTP response from your server. See [setHeader](https://nodejs.org/api/http.html#http_response_setheader_name_value) for node.js. [This SO q&a](https://stackoverflow.com/questions/23112316/using-flask-how-do-i-modify-the-cache-control-header-for-all-output) for Flask. etc. – Yaelet Jul 03 '19 at 09:38
0

You should add something unique to the end of the url you are trying to get the assets from

<script src="test.js?version=1"></script>

This is the most reliable way of doing that.

I would maintain a version number somewhere and get JS to append it to the url if I have so many assets to load.

Here is some help -

https://curtistimson.co.uk/post/front-end-dev/what-is-cache-busting/

And please see this question too -

How to prevent caching of my Javascript file?

Charlie
  • 22,886
  • 11
  • 59
  • 90