1

I've carried out some testing with etags (used to determine if content has changed since a browser last requested it: https://sdn.sitecore.net/upload/sitecore6/sc62keywords/cache_configuration_reference_a4.pdf) and Sitecore returns an etag for media items like so:

ETag:9c8e1229037c470899e369e903945bcf

However if I update some properties on the image such as the alt text the tag does not update.

If I update the image though and add a different image (even with the same name) then this does update.

Is this the expected behaviour and are there any settings for this other than 'MediaResponse.Cacheability'?

1 Answer 1

2

Expected Behaviour?

That, I can't tell you. My first thought was, that the ETAG on a media request would be set to the Revision field of the underlying item. That's not what happens.

Here is the snippet of code responsible, inside MediaRequestHandler, under the SetMediaHeaders() method.

cache.SetETag(media.MediaData.MediaId);

So basically it takes the MediaId of the underlying Media blob. Not the Media Item itself.

This seems 100% in line with what you're experiencing yourself - updating the Media will provoke a new ETAG. Updating meta information on the image, will not.

But isn't this good enough anyway?

You're trying to prevent a re-download of expensive media data, and if the data hasn't changed (the media itself) - the ETAG should stay the same? no?

I mean, if you output additional meta information about the media on your page - this would come from the Media Item itself and not be restricted by caches.

In summary

I think this works as intended and expected. ETAG updates when the media is updated, not the media meta information.

Workaround, if you want

The method responsible for the ETAG header (and all other headers) is SendMediaHeaders on the MediaRequestHandler. It is implemented as a protected virtual method, so it would be relatively easy for you to override this behaviour if you still require it. Write a new class that overrides this method, and replace the default Sitecore one in Web.config under <handlers>.

  <add verb="*" path="sitecore_media.ashx" type="Sitecore.Resources.Media.MediaRequestHandler, Sitecore.Kernel" name="Sitecore.MediaRequestHandler"/>
2
  • Awesome. thanks Mark - that all makes sense and I think thats fine then. I am unsure from previous testing if my browser is cache was updating correctly though (when I update the image in Sitecore) and showing the new image. Based on the etags updating I'm guessing my browser should invalidate my local cache and return the new image right? This is why I have implemented this solution from JammyKam regarding adding the media revision date to the url: sitecore.stackexchange.com/questions/7133/… Aug 21, 2017 at 15:50
  • 1
    Correct. If the browser recognises an ETAG, it should indeed not request a new download. Whether all browsers respect this unconditionally, I do not know. Edit. A bit of research - IE 5.5 supported it. I think it's relatively safe to assume this has broad support ;-) stackoverflow.com/questions/6247761/browser-support-for-etags Aug 21, 2017 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.