5

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).

Talon
  • 4,937
  • 10
  • 43
  • 57
  • you cannot force browsers to reuse cache. you can get creative and have cache expire dates set in the past or something, but thats about it. – PlantTheIdea Apr 05 '13 at 21:34
  • on business networks i'm sure you can accomplish this via the GPOs. – RandomUs1r Apr 05 '13 at 21:36
  • you can try to turn off the caching completely http://stackoverflow.com/questions/1341089/using-meta-tags-to-turn-off-caching-in-all-browsers – btevfik Apr 05 '13 at 21:38

4 Answers4

3

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.

Richard Deurwaarder
  • 2,023
  • 1
  • 26
  • 40
  • This is the best answer to the posters question. It allows you to effectively invalidate the cache when _you_ want it, independent of previously set headers. Only drawback is that your page is not testable from the filesystem but only from a webserver, since the filesystem does recognize ?param=bla as part of the filename. You can fix this by incorporating a version number in the filename, but that has other drawbacks: It probably means you have to change the css tags in a lot of your html pages, or you need to use a server-side include system to fix that, which will also require a server. – Simon Groenewolt Apr 06 '13 at 09:36
1

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
}
?>
erocha
  • 23
  • 6
0

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>
Santiago Baigorria
  • 700
  • 1
  • 6
  • 15
0

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:

  1. Rename all changed JS, GEOJSON, and CSS files.
  2. Edit calls to the files (SCRIPT tags) as needed, and save the calling HTML file with a new name.
  3. Edit the original page to be a meta redirect to the new page.

Using this method should load only the changed data and NOT force a fresh download of unchanged (and cached) data.

bkepl
  • 1