Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser keeps rendering its cached version. I want to always force a GET

How do I prevent the client browser from rendering its cached version for a page, so that it must always perform a GET when the visitor visits the page?

I am using Django's @never_cache decorator in the view, which adds "Cache-Control:max-age=0" to the HTTP GET header. However when I visit the page (in Google Chrome and Firefox, the only browsers I have tested so far), the cached version is inevitably rendered. (Confirmed by visiting the Network tab for the request, which reports "200 OK (from cache)".)

If I now click the Refresh button, it will render the fresh content from the server (Network tab for the request says "200 OK" and the headers as shown below.)

In lieu of setting "Cache-Control:max-age=0" I also tried setting the "Expires" HTTP header parameter to be a date in the past. This did not work either.

Request Method:GET
Status Code:200 OK

Request Headers
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
If-Modified-Since:Fri, 17 Feb 2012 15:25:21 GMT
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11

Response Headers
Cache-Control:max-age=0
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html; charset=utf-8
Date:Fri, 17 Feb 2012 15:55:11 GMT
ETag:"[removed]"
Expires:Fri, 17 Feb 2012 15:55:11 GMT
Last-Modified:Fri, 17 Feb 2012 15:55:11 GMT
Server:nginx
Transfer-Encodindg:chunked
Vary:Cookie,Accept-Encoding
X-Handled-By:127.0.0.1:8000
like image 877
ram1 Avatar asked Feb 17 '12 16:02

ram1


2 Answers

In your response set these:

response['Cache-Control'] = 'no-cache, no-store, max-age=0, must-revalidate' 
response['Expires'] = 'Fri, 01 Jan 2010 00:00:00 GMT'

It's how Google Docs enforces pages refresh at all times.

Then you can play with it to find the ideal behaviour for your purposes.

like image 147
Tomasz Zieliński Avatar answered Oct 27 '22 01:10

Tomasz Zieliński


Have you tried this in your htaccess file:

<FilesMatch "\.(css|gif|html|jpg|js|php|png)$">
Header set Cache-Control: "max-age=0, no-store"
</FilesMatch>

You can adjust the files match line for a specific page/path and also for specific assets

like image 43
Alexander Holsgrove Avatar answered Oct 27 '22 01:10

Alexander Holsgrove