Home > OS >  browsers don't resend If-Modified-Since & If-None-Match request headers after receiving one 304
browsers don't resend If-Modified-Since & If-None-Match request headers after receiving one 304

Time:11-05

server (local development LAMP server) side is php , browsers tested are chromium & librewolf (firefox flavour), both are showing the same behaviour therefore I assume there is something wrong with my http headers.

headers sent on first request :

by my code:

      "Connection    : close"
      "Content-Type  : text/html; charset=UTF-8"
      "Date          : ".gmdate("D, d M Y H:i:s")." GMT";
      "Last-Modified : ".$lastmod;
      "Etag          : ".$etag;
      "Expires       : 1" //can't have the browser doesn't check if file was modified on server
      "Pragma        : public"
      "Cache-Control : max-age=1,must-revalidate"

by ob_start("ob_gzhandler")

     "Content-Encoding : gzip"

by apache server :

     "Connection        : Keep-Alive"
     "Keep-Alive        : timeout=5, max=99"
     "Server            : Apache/2.4.46 (Unix) OpenSSL/1.1.1j PHP/8.0.3 mod_perl/2.0.11 Perl/v5.32.1"
     "Transfer-Encoding : chunked"
     "Vary              : Accept-Encoding"
     "X-Powered-By      : PHP/8.0.3"

server checking if client has file cached :

if (
    ((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) 
      &&  $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $lastmod )
    || 
    (!isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) 
    && isset($_SERVER['HTTP_IF_NONE_MATCH'])
    && trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag)
    )
  )
    {
       
       header("HTTP/1.1 304 Not Modified");
       header("Content-Length:0");
       header('Etag:'. $etag);
       header('Last-Modified:'.$lastmod);

       exit;

     }

first time librewolf(firefox) & chromium re-requests a page, they send as expected the If-Modified-Since and/or If-None-Match request headers & receive a 304 not modified header as expected.

However subsequently to that first re-request, after receiving one 304 not modified response, they don't send any more those If-Modified-Since and/or If-None-Match request headers making this caching system half of the time useless.

How can I tell the client browser to always send those If-Modified-Since and/or If-None-Match request headers instead of just one time ?

CodePudding user response:

I've found out, finally !

The headers need to be exactly the same & sent in the exact same order as the first time the file was sent and possibly before content-length & response code (maybe that is less strict than that but I've spent enough time on this and I'm not going to investigate any more)

Therefore in my case I had to send :

        header("Connection:close");
        header("Date:".gmdate('D, d M Y H:i:s')." GMT");
        header('Last-Modified:'.$lastmod);
        header('Etag:'. $etag);
        header("Expires:".gmdate('D, d M Y H:i:s',parent::$requesttime self::$expire));
        header("Pragma:public");
        header("Cache-Control:max-age=".self::$expire.",  must-revalidate");
        header("Content-Length:0");
        header("HTTP/1.1 304 Not Modified");

& bingo at last... no more unnecessary 200 !

Hopefully this will help somebody one day.

  • Related