1

I have a simple Node server which sets the Cache-Control max-age to 1200seconds and the client requests an image. Even though the 1200 seconds havent passed, if i change the image from server and refresh the page, the image will be downloaded again (even though the 1200seconds havent passed). It is supposed to read from cache until it expires.

Here is the response header:

enter image description here

Why is this happening?

Always Learning
  • 5,510
  • 2
  • 17
  • 34
George Paouris
  • 525
  • 4
  • 16

1 Answers1

1

When you told the browser to cache, the response header includes a validator called Last-Modified. When the browser is reloading it can include this in the request to the server in an If-Modified-Since request when for the GET or HEAD request. That will cause the server to return 304 if the item has not changed.

So it sounds like things are working as they are supposed to. The fact that you changed the file caused the server to say "yeah, this file was changed so the cache shouldn't be used. here's the latest one" because the Last-Modified value won't match.

If you want to avoid this and rely on the cache timeout even if the file changes, you could change your server to make sure that the ETag and Last-Modified values never change for these files. But I would recommend letting it act this way and allow the new file change to take its place in the cache.

See this tutorial for more details.

Always Learning
  • 5,510
  • 2
  • 17
  • 34
  • If you go here https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching#invalidating_and_updating_cached_responses, in the section "However, what if you want to update or invalidate a cached response?...." Implies that even though an asset has changed to the server, users still not getting the latest until the cached is expired, which this is not the case here. – George Paouris Feb 09 '20 at 20:14
  • the `Last-Modified` probably overrides that. If your server set `Last-Modified` to the same value all the time regardless of when the file actually changed, then it would probably still get the `304` until `max-age` has expired. Note that the page you reference doesn't mention `Last-Modified` at all so may not be exactly correct. – Always Learning Feb 09 '20 at 20:27