no 'Last-Modified' HTTP header -> however cached?

11,708

Solution 1

Yes, the image may get cached even without a Last-Modified response header.

The browser will then cache the image until its TTL expires. You can set the image's Time To Live using appropriate response headers, e.g. this would set the TTL to one hour:

Cache-Control: max-age=3600
Date: Tue, 29 Mar 2011 20:18:17 GMT
Expires: Tue, 29 Mar 2011 21:18:17 GMT

Even without any Last-Modified in the response, the browser may still use the Date header for subsequent If-Modified-Since requests.

Solution 2

Generally speaking, responses can be cached unless they explicitly say that they can't (e.g., with cache-control: no-store).

However, most caches will not store responses that don't have something that they can base freshness on, e.g., Cache-Control, Expires, or Last-Modified.

For the complete rules, see: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-p6-cache-13#section-2.1

See: http://www.mnot.net/blog/2009/02/24/unintended_caching for an example of how this can surprise some people.

Solution 3

I disabled the Last-Modified header on a large site and FF 13 doesn't take the contents from cache, although a max-age is given etc. Contents without a Last-Modified header ALWAYS get a status 200 ok when requested, not a 304. So the browser looks for it in the cache.

Share:
11,708
abernier
Author by

abernier

Hi, I'm Antoine Bernier — JS fullstack webdev with 15y of experience. I love animation, unix, design and opensource. Feel free to contact me 👋 http://be.net/abernier

Updated on June 07, 2022

Comments

  • abernier
    abernier almost 2 years

    From a browser perspective,

    What occur if a component (image, script, stylesheet...) is served without a Last-Modified HTTP header field...

    Is it however cached by the browser even if it won't be able to perform a validity check(If-Modified-Since) in future, due to his lack of date/time information?

    Eg:

    GET /foo.png HTTP/1.1
    Host: example.org
    --
    200 OK
    Content-Type: image/png
    
    ...
    

    Is foo.png however cached?

    --

    Would you know any online service to serve my raw HTTP response that I can write myself in order to test what I'm asking ?

    Thank you.

    • abernier
      abernier about 13 years
      This question was prior to this presentation I now share with you. Thanks for your help.
  • abernier
    abernier about 13 years
    Is Date mandatory HTTP header field? I mean, if no date/time information is provided in the response (at all), is it still cached and what about subsequent requests? thank you
  • Martin
    Martin about 13 years
    @abernier Yes, the image can be cached if you specify the TTL with max-age, even if Date and Last-Modified are absent. Date is not mandatory.
  • abernier
    abernier about 13 years
    Thanks Martin — a last question before accepting your answer: do you know any web-service where I can create a resources but with my own HTTP response so I can effectively test it?
  • Martin
    Martin about 13 years
    @abernier your best option would probably be to run an Apache server on your own machine and use mod_asis to create a raw response
  • user66001
    user66001 about 10 years
    By the info provided in the latter half of your answer, shouldn't FF13 have gotten a HTTP200 and presented the pages of your large site with Last-Modified header removed, from cache??