0

In an attempt to master HTTP cache, I'm currently trying to craft an HTTP response saying "Once you've got it, cache it forever". Here's what it looks like (PHP):

<?php
header('Cache-Control: max-age=31536000, immutable, only-if-cached');
sleep(2);
echo date('H:i:s');
exit;

Unfortunately, neither Chrome (when hit directly), neither Cloudflare (when proxied) wants to serve from cache.

Full response headers received from origin:

HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Mon, 30 Mar 2020 16:54:17 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: max-age=31536000, immutable, only-if-cached
Content-Encoding: gzip

What am I doing wrong?

Ben
  • 845
  • 1
  • 8
  • 18
  • Please edit your question to expand on "neither... wants to serve from cache". What is the evidence of that? Note that there are a number of debugging techniques that will affect the cache (e.g. you can turn off the cache in developer tools, reloading and history result in different cache behavior from normal navigation, etc.). I just answered [this related question](https://stackoverflow.com/questions/60902008/why-does-firefox-ignore-cache-headers-and-revalidate-on-refresh/). – Kevin Christopher Henry Mar 30 '20 at 18:21
  • Hello Kevin, the behavior I'm actually trying to achieve is: get it, cache it, don't even revalidate, so that I could even shutdown the server without you noticing. Neither Chrome (devtools closed or open w/ "disable cache" unchecked) neither Cloudflare seems to cache the origin resource. I tried with immutable only, max-age only, same. – Ben Mar 30 '20 at 18:48
  • I understand what you're trying to do, the problem is that you haven't included enough information for someone to answer the question. Why do you think caching isn't working? Are you seeing hits in the server log? Have you confirmed that you haven't turned off caching in developer tools? Are you seeing this on page refresh or when using history or when following a link? – Kevin Christopher Henry Mar 30 '20 at 19:06
  • What I can say is that, yes, the `max-age` directive should cause your response to be cached. Note that Chrome doesn't support the experimental `immutable` directive, and that `only-if-cached` is a request directive, so doesn't mean anything in a response header. – Kevin Christopher Henry Mar 30 '20 at 19:07
  • I can see this because of the content (current hour with seconds) and the `sleep` function (which causes a delayed response that should not happen when cached). Confirmed cache isn't turned off in chrome dev tools, tried by re-entering same url in address bar, devtools off, and by putting a Cloudflare proxy in the middle (Cloudflare doesn't cache the origin server response, always serves fresh - same empiric approach). No Vary header. Just max-age should do the trick, but it doesn't. – Ben Mar 31 '20 at 20:24
  • I wonder if this has something to do with the chunked `Transfer-Encoding`? See [here](https://stackoverflow.com/a/26789163/2395796), for example. – Kevin Christopher Henry Apr 01 '20 at 00:33

0 Answers0