24

I have a file that I link to from my website like

<a href="http://example.com/myfile.txt>View!</a>

However, this file changes very frequently and when the link is clicked, the browser loads the cached version of the file, not the actual file. Is there a way so that clicking on that link will bypass the cache for that page?

Something nice like <a bypassCache href=""> would be wishful thinking.

dukevin
  • 22,384
  • 36
  • 82
  • 111

5 Answers5

27

Something nice like would be wishful thinking.

Indeed, there is something you can do from within the link: Add a random GET parameter.

<a href="http://example.com/myfile.txt?a=193834923283943842923">View!</a>

You could use JavaScript (or of course a server-side scripting language like PHP) to do this on a dynamic basis.

However, the far superior way would be to configure the text file's caching headers correctly in the first place on server side. Stealing the header info from Best way to disable client caching, a .htaccess file like this should work:

<Files myfile.txt>
FileETag None
<IfModule mod_headers.c>
Header unset ETag
Header set Cache-Control "store, no-cache, must-revalidate, post-check=0, pre-check=0"
Header set Pragma "no-cache"
Header set Expires "Sun, 19 Nov 1978 05:00:00 GMT"
</IfModule>
</FilesMatch>
Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • No! Never add random parameter to avoid caching. If you are using cache proxies like Varnish then you are filling cache with useless entries that will never be used. You should make a use of Cache-Control and Pragma header parameters. – mickiewicz Jan 18 '17 at 13:32
  • On the above example replace with – tsig Nov 13 '18 at 05:58
4

Just put

<meta http-equiv="expires" content="0">

Into the head section of your target page and check again

jeremy
  • 9,965
  • 4
  • 39
  • 59
Job
  • 57
  • 1
  • 1
  • While this answer is rated relatively high, one concern might be that you do not want to avoid caching all the time for better performance, I think this head tag is going to put more load on the server unnecessarily if you are looking for some specific files, I think generating random number must be the ideal way to go.. – Naga Feb 23 '23 at 15:08
3

The best way is to tell apache/(web server) to tell browser not allow caching of that file, if you don't have controll over that server, you could avoid cache by alter the parameters send to it, just add some numbers behind ?, for exemple the time when you created the link, this makes each url diferent, so the browser going to ignore the cache, but all links to the same file, as long as the server ignore the extra parameter. in php:

echo "<a href='http://example.com/myfile.txt?" . time() . "'>View!</a>"
Puggan Se
  • 5,738
  • 2
  • 22
  • 48
0

Add a random number after the hyper link, such as <a href="http://example.com/myfile.txt?rand=12312321321">View!</a>

Generate a new random number each time the page loads.

Raptor
  • 53,206
  • 45
  • 230
  • 366
  • but I'm not the one that will always update the file. That means I'll have to give access to everyone who edits the file and instruct them to generate the random number – dukevin Aug 15 '11 at 07:51
0

You can solve your problem on server level. Set the special expiration date for txt resources (or that particular one) that fit to your requirements. If you use apache you can read about mod_expiry for apache here

mkk
  • 7,583
  • 7
  • 46
  • 62