Set <staticFile> Configuration in web.config to Ensure Latest Media Files Retrieved Client Side

  • Updated

Media asset caching is controlled by configuration. The associated class that handles this is EPiServer.Web.StaticFileHandler. This class inherits from MediaHandlerBase which is in the same namespace as StaticFileHandler. Within MediaHandlerBase there is a method called SetCachePolicy which takes into account the configuration in web.config.

Here is the comment in the decompiled source:

    /// <summary>
    /// Configures cache policy of a static file based on configuration rules
    /// </summary>
    /// <remarks>Configuration setting expirationTime in web.config states the Expires date that is
    /// added to the Resonse (which browsers typically uses to control how long to cache the file before a new request).
    /// If TimeSpan.Zero is specified in expirationTime no Expires date is added which most browsers will
    /// interpret so they make a request for the file each time but since Last-Modified is added a 304 (Not modified)
    /// will be returned by server if file has not changed.</remarks>
    /// <param name="context">The HttpContext for the current request.</param>
    /// <param name="fileChangedDate">The changed date for the file that is being requested.</param>

Within <staticFile> you can set expirationTime to some very low value so that client requests will retrieve latest version of the file (look at the above link for an example of how to set <staticFile> in web.config). It's not in there by default, so you'll want to give this a try. 

Related forum post:

What you can do specifically to ensure that at least the client always retrieves the latest file is to create a set a <location> element for "globalAssets" and then set the <staticFile> configuration for this location path (in this case, your media). It would look like:

<location path="globalAssets">
<staticFile expirationTime="-1.0:0:0" />

CDN considerations:

If you are using a CDN, such as Cloudflare or Akamai, you'll need to check for caching configuration on that end in case you've made these config changes, but still see old versions of media files getting loaded. See link: