Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML link that bypasses cache?

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.

like image 469
dukevin Avatar asked Aug 15 '11 07:08

dukevin


People also ask

How do I stop HTML from caching?

Here's how... When you're in Google Chrome, click on View, then select Developer, then Developer Tools. Alternatively, you can right click on a page in Chrome, then click Inspect. Click on the Network tab, then check the box to Disable cache.

Can HTML be cached?

Implementing user personalization in scripts allows caching of the page's HTML. Then the scripts can modify the page after loading asynchronously. Beyond using JavaScript for personalization, The Gap is caching HTML.


3 Answers

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>
like image 140
Pekka Avatar answered Oct 04 '22 18:10

Pekka


Just put

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

Into the head section of your target page and check again

like image 35
Job Avatar answered Oct 04 '22 18:10

Job


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>"
like image 32
Puggan Se Avatar answered Oct 04 '22 19:10

Puggan Se