2

How can I detect when the client hits F5 / refresh? Are there any headers sent to the server to indicate this that I can grab? I'm doing some server-side caching and I'd like to automatically expire the cache when a refresh is detected. Thanks.

ensnare
  • 34,050
  • 49
  • 140
  • 211

2 Answers2

1

ETags may be a more correct solution, but simply looking for the Cache-Control header is enough, since the browser will use it to indicate caches should not be used.

Chrome sends Cache-Control: max-age=0, but some other browsers send Cache-Control: no-cache. You may want to expire cache on any cache control header you don't recognize.

Or even:

if any(i.lower().startswith('cache-control:') for i in self.headers): #...
otus
  • 4,957
  • 29
  • 46
0

I'd look at ETags and conditional cache validation requests.

If the browser sends a If-None-Match header they had a cached copy and want to see if they can reuse it. Instead of sending a 304 Not Modified you instead clear your cache, produce a full 200 OK response and set a new ETag header.

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997