David Gourde
David Gourde

Reputation: 3914

How to ask browsers to refresh (flush) the cache of my website?

I searched everywhere and I can only find solutions that I do not understand (I am not really skilled in web development).

If I understand correctly, the best method is the versionning of javascript and css calls. But I don't understand how I am supposed to implement this in my website or server.

Would someone have an idea or a link for a beginner like me? (the easiest method is the best for me I suppose)

Right now my users have problems with the website, but all problems vanish when the cache is cleared. It's a big problem.

Thanks anyway guys. Have a nice day.

Upvotes: 0

Views: 219

Answers (2)

RichardB
RichardB

Reputation: 2767

Simply add a new parameter to the end of the file every time you update it, eg

<script src="http://www.example.com/javascript.js?reload=1">

and

<script src="http://www.example.com/javascript.js?reload=2">

will both be treated as different files, despite actually being exactly the same..

If you need it never to cache, then change the parameter dynamically every page load using a script (adding a timestamp is the usual approach as that will never get accidental repeats).

Upvotes: 1

imbondbaby
imbondbaby

Reputation: 6411

You can use the PHP header:

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

You can place it where the headers are being called

Or use the meta tag instead:

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

This way your browser won't store cache.

However, cacheing headers are unreliable in meta elements; for one, any web proxies between the site and the user will completely ignore them. You should always use a real HTTP header for headers such as Cache-Control and Pragma.

Upvotes: 1

Related Questions