Reputation: 4995
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).
Upvotes: 6
Views: 1720
Reputation: 101
I have been thinking about this a lot lately. I recently started working with Leaflet maps and GEOJSON (a JSON file with geographical information). A few of the GEOJSON files are quite large, so I don’t want to force a re-download of the data if it hasn’t changed, but as I’m developing a map I may modify other JS, GEOJSON, or CSS files so I will want that modified data to download. I just want the new data to be there. So, I came up with this scenario:
Using this method should load only the changed data and NOT force a fresh download of unchanged (and cached) data.
Upvotes: 0
Reputation: 23
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
}
?>
Upvotes: 1
Reputation: 696
This is configured from the .htaccess file with ExpiryDates
(Example of mine below. My host runs a PHP / Apache Server - 000webhost.com)
<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType text/css "access plus 1 week"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
</IfModule>
Upvotes: 0
Reputation: 2040
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.
Upvotes: 4