3

I want to tell the browser to act in a specific way using Cache-Control header:

  • Always ask for a fresh version of the page, as you would do with no-store

BUT

  • Cache the content anyway, just in case the server does not respond / responds too slowly (an old version is better than nothing)

I thought that no-cache would have been the right header, but if I use it the browser serves the cached version even if the server is available.

What should I do? Could If-Modified-Since or If-None-Match be useful for my purpose?

Thanks in advance.

user2864740
  • 54,112
  • 10
  • 112
  • 187
Foxel
  • 133
  • 1
  • 6
  • @Joe it helps! Should I put `no-cache` anyway or is `Cache-Control: stale-while-revalidate,stale-if-error=xxx` enough? – Foxel Jun 15 '20 at 18:01
  • I tested it further and it does not seem to work... The problem is that if I do not put `no-store` (which prevents the browser from using `stale-while-revalidate` and `stale-if-error`), when I press the back button my browser serves the cached version anyway (which is not ok for example after a log out) – Foxel Jun 15 '20 at 18:38
  • The correct technique here would be a `max-age` of zero, and a `stale-while-revalidate` of however long you want a response to be reusable. FWIW, I'm not sure `stale-while-revalidate` is simple to apply here, with current browser support. I've linked it because I believe it's the standard that aims to solve this problem. – Joe Jun 16 '20 at 00:08
  • @Joe My problem is that if I put `max-age=0` and/or `no-cache` without `no-store`, pressing the back button on my browser will display the stale content anyway... How can I fix it? Using `Last-Modified: *current date*` does not help... – Foxel Jun 16 '20 at 15:11
  • For back button, see questions like https://stackoverflow.com/questions/7248111/how-to-prevent-content-being-displayed-from-back-forward-cache-in-firefox and https://stackoverflow.com/questions/43043113/how-to-force-reloading-a-page-when-using-browser-back-button – Joe Jun 16 '20 at 23:56
  • @Joe thanks. Looking for the back button using JS is not the cleanest solution but actually the only one at this point... So I think that I will go for that way – Foxel Jun 17 '20 at 10:04

0 Answers0