Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Safari honor my cache-control directive?

There is a particular page that I would like the browser to always load, particularly when the user presses the browser back button to get to it.

So I use the following 'Cache-Control' directive in the header for this page (taking the PHP directly from my code).

$headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0';

This works for FF, IE and Chrome, but Safari (5.0.1) seems to ignore the directive and fails to reload the page (across multiple users, so it doesn't seem to be a setup specific problem). Searching hasn't revealed any known bugs, so I'm assuming there is something amiss on my end.

Any pointers would be much appreciated.

Update: just found this answer on SO.

Safari browser ignoring my no-cache

Haven't tried it yet, but looks promising. Very strange that it is an addition to the body tag, so Im skeptical.

like image 402
Greg Avatar asked Aug 30 '10 17:08

Greg


People also ask

How do I enable Cache-Control?

To use cache-control in HTML, you use the meta tag, e.g. The value in the content field is defined as one of the four values below. HTTP 1.1. Allowed values = PUBLIC | PRIVATE | NO-CACHE | NO-STORE.

What does no-cache cache directive do?

The no-cache directive means that a browser may cache a response, but must first submit a validation request to an origin server.

What is Cache-Control must revalidate?

The must-revalidate response directive indicates that the response can be stored in caches and can be reused while fresh. If the response becomes stale, it must be validated with the origin server before reuse. Typically, must-revalidate is used with max-age . Cache-Control: max-age=604800, must-revalidate.

How do I change my Cache-Control max-age?

Cache-Control: max-age=<seconds> This directive tells the browser or intermediary cache how long the response can be used from the time it was requested. A max-age of 3600 means the response can be used for the next 60 minutes before it needs to fetch a new response from the origin server.


2 Answers

The answer is to include the following attribute in the body tag to force Safari to reload the page:

onunload=""

As in:

<body onunload="">

I found this here: Safari browser ignoring my no-cache

Which in turn got it from this thread: Is there a cross-browser onload event when clicking the back button?

like image 65
Greg Avatar answered Sep 18 '22 17:09

Greg


Also see this Webkit bug report: https://bugs.webkit.org/show_bug.cgi?id=71509

I am experiencing the same issue, and it appears to be to be a bug in Safari.

like image 32
Robert Brown Avatar answered Sep 21 '22 17:09

Robert Brown