4

I've got an image on S3, that has a Cache-Control header set to public, max-age=3600. So, downstream systems (proxy, browser, etc) should cache it for 1 hour.

I can see the browser returns 304 (not modified) after subsequent requests.

I've then gone and updated that image on S3 (the resource isn't versioned..the URL hasn't changed, just the _content), but the browser went and got the new version. (e.g HTTP 200).

Here's the chrome network traffic:

enter image description here

Requests:

  1. 200 (ok)
  2. 304 (not modified)

~ image changed behind the scenes ~

  1. 200 (ok)
  2. 304 (not modified)

Here's the response header: enter image description here

How is this possible? Am i not setting the correct header?

Two things are confusing me:

  1. The response should still be 304 (not modified)? How did the browser know to go and get the image? max-age hasn't been reached?
  2. Shouldn't chrome show "from disk cache" or whatever? (and not actually go to the server?)

My guess is - i'm not setting the right cache-control header or something. So can someone please shed some light on this behaviour, and let me know what i need to do to cache resources for 1 hour :)

Thanks!

RPM1984
  • 72,246
  • 58
  • 225
  • 350
  • Even though @zerkms has given a great answer, this nice SO answer adds some nice extra context to the story: https://stackoverflow.com/a/26339940/30674 – Pure.Krome Jul 10 '18 at 03:59

1 Answers1

3

There is ETag specified in the response.

The ETag is explained as such in the spec

An entity-tag can be more reliable for validation than a modification date in situations where it is inconvenient to store modification
dates, where the one-second resolution of HTTP date values is not
sufficient, or where modification dates are not consistently
maintained.

so browsers prefer it over the date-based expiration.

Hence, your browser makes a request every time, and unless the content has changed - http 304 is returned. Otherwise - it's http 200.

If you only want time-based expiration - remove ETag.

References:

Community
  • 1
  • 1
zerkms
  • 249,484
  • 69
  • 436
  • 539
  • ah.. i had a suspicion it was the etag. So that takes precedence? E.g if that's changed, it'll go and get the image, despite the max-age? Basically, if you have an ETag, no point even setting the max-age? – RPM1984 Jul 10 '18 at 03:55
  • @RPM1984 it makes a request every time, regardless of `max-age` – zerkms Jul 10 '18 at 03:56
  • "if you have an ETag, no point even setting the max-age" --- yep, I would say so – zerkms Jul 10 '18 at 03:56
  • but why is my browser still going to the server, even for the 304? shouldn't it say (from disk cache)? (e.g point 2 in my question). what conditions would it _not_ go to server? – RPM1984 Jul 10 '18 at 04:03
  • @RPM1984 presence of `ETag` makes it go every time. "shouldn't it say (from disk cache)?" --- it should not, since the response was served from a server with HTTP 304, only contents came from disk cache. http 304 implies the contents came from a cache. – zerkms Jul 10 '18 at 04:23