1

I have a very simple question, which I have been unable to find a clear answer to. I have a web page which is generated dynamically (so I absolutely do not want it cached), which loads in several thousand images. Since there are so many images, and they never change, I very definitely want the user's browser to cache these images.

I'm applying headers to the HTML page to prevent caching, by following the advice in this solution: How to control web page caching, across all browsers?

My question is: Will this cause the user's browser to also not-cache any images this page contains, or will it cache them? Thank you.

Community
  • 1
  • 1
Ravenswd
  • 85
  • 6
  • No, caching is managed per resource. But if you actually load thousands images there might be some cache evictions just because of huge amount of images. Which, of course, is not related to caching headers. – Andrew Feb 25 '16 at 16:51

1 Answers1

1

TL;DR the answer is not clear because it is complicated.


There is an ongoing struggle between a drive to do the "right" thing (i.e., follow the standards... which themselves have changed) and a drive to "improve" the standards to achieve better performances or smoother navigation experience for users. So from the application point of view you need to properly use headers such as ETag, If-Modified-Since and Expires, together with cache hinting pragmas, but the browser - or something in the middle such as a proxy - might still decide to override what would be the "clear thing to do".

On the latest Firefox, directly attached to an Apache 2.4 on virtual Ubuntu machine, I have tried with a page (test.html) referring to an image (test.jpg).

When the page is cached, server side I see a single request for the HTML and nothing for the image. What is probably happening is that the "rendering" part of Firefox does request the image (it has to!), but that is entirely supplied by the local cache. This makes sense; if the page has not changed, its content hasn't changed.

When the page is not cached, I see two requests, one for the page and one for the image, to which the server responds with a 304, but that is because I also send the image's Last-Modified header. This also makes sense - if the page has changed, the images might have changed too, so the browser has to know whether this is the case, and can only do so by asking the server (unless the Expires header is used to "assure" the client that the image will not change).

I have not yet tried with an uncached page that responds with a 304. I expect it to generate a single request (no image request to the server), for the same reasons.

What you might want to consider is that your way you will not cache the HTML page but might still perform a thousand image requests (which will yield one thousand 304's, but still). Performances on this kind of event depend on whether the requests are sent independently or back-to-back by using the Keep-Alive HTTP/1.1 extension (has to be enabled and advertised server side).

You should then use the Expires header on the images to tell the client that those resources will not go stale anytime soon.

You might perhaps also want to explore a different approach:

  • the HTML is cached
  • images are cached too
  • the HTML also references a (cached?) Javascript
  • variable content is loaded by the Javascript via AJAX. That request can be made cache-unfriendly by including a timestamp, without involving the server at all.

This way you can configure the server for caching everything everywhere, except where you make sure it can't via a single crafted request.

LSerni
  • 49,775
  • 9
  • 56
  • 97