Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tell Browsers to update the cache if cache date is over a certain date

Is there a meta tag or something I can use that tells the browser to not use a cache from before a certain date.

e.g.

<meta "only use cache if cache is AFTER 4/4/2013">

If the cache is old it needs to download all the new Javascript, CSS, Images, etc..

I make a lot of updates to the site and then it screws up anyone who still has a cache (and you can't expect them to know to press ctrl+f5 or ctrl+r).

like image 454
Talon Avatar asked Oct 05 '22 12:10

Talon


2 Answers

I usually just add a get parameter on the end with the version of the script I am presenting them

so if you have your javascript at

www.example.com/script.js

use the url

www.example.com/script.js?foo=1

when I increment the value of foo everytime, this forces the browser to refetch the script.

like image 131
Richard Deurwaarder Avatar answered Oct 13 '22 10:10

Richard Deurwaarder


There is not a meta tag for setting cache only after a certain date but if you are using any server language (PHP, .NET, ruby, phyton) you can set cache-control to no-cache and then dynamically set the headers to start caching after a certain date.

For example using PHP:

<?php
$cdate = date('Ymd');
if ($date > '20130404') {
  header('Cache-Control: max-age=28800'); //cache lifetime to 8 hours
} else {
  header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
}
?>
like image 20
erocha Avatar answered Oct 13 '22 10:10

erocha