Getting file version with GetImage transformation method

Bryan Johnson asked on October 23, 2018 16:34

I'm hoping to get some ideas to deal with browser caching of images. When a transformation is written to retrieve an attached image and create an html img element using the GetImage transformation method, it does not provide file version information in the URL. If a Kentico editor updates the image, browsers are unaware of the change, since the image URL provided by GetImage remains the same, and continue to serve the browser's cached version of the image file. Other transformation methods that retrieve attachments have the same problem.
Have any of you found a solution to this problem? Is there a way to retrieve an attachment URL including its version information using out-of-the-box kentico transformation methods? Or is it necessary to write a custom transformation method to do this? Thank you!

Correct Answer

Peter Mogilnitski answered on October 24, 2018 00:50

it depends on your performance settings (settings -> system -> performance).

Is Allow client cache revalidation checked? If enabled, the server adds the 'must-revalidate' directive to the 'Cache-control' response header of file requests. Ensures that caching clients always perform revalidation when requesting expired files (most clients revalidate by default even without the must-revalidate directive).

Take a look at your as well at Server-side file caching .

0 votesVote for this answer Unmark Correct answer

Recent Answers


Bryan Johnson answered on October 24, 2018 17:51 (last edited on October 24, 2018 23:40)

Thanks, Peter, for your insight. It turned out that the caching problem I was seeing was limited to a unique case in my page design. I'm updating a set of img elements with ajax after initial page load for a kiosk SPA. When the img elements are updated by ajax, and the supporting image files are not actually re-requested, the browser has no way of knowing that there is a new updated image available for download. So it looks like I'll have to version the image file URLs after all.

Here is the method I created to build a version number from the Attachment's AttachmentLastModified converted to ticks, in case someone run's into the same issue. It can be used to populate a version number as a query string parameter on the attachment URL in your transformation.

    private string GetAttachmentLastModifiedDateAsTicks(string attachmentGuid)
    {
        return AttachmentInfoProvider.GetAttachmentInfo(attachmentGuid, SiteProvider.SiteContext.CurrentSiteName).AttachmentLastModified.Ticks.ToString();
    }
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.