Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

Prevent website from caching media files

Vote:
 

Hi, we have an issue where media files are getting cached. We have a scheduled job that replaces an excel file every day, and a page that links to the file. If a user has previously visited the page and revisits the page to download the newly updated file, the user will get the cached file instead. How to prevent caching from happening, or at least set the caching period?


#311866
Nov 02, 2023 10:01
Vote:
 

Could you add some Middleware to taget your particular file and add some No Caching Rules 

You can do something like this but change to your requirements 

    public class NoCacheMiddleware
    {
        private readonly RequestDelegate _next;

        public NoCacheMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext context)
        {
            context.Response.OnStarting(state =>
            {
                var httpContext = (HttpContext)state;
                httpContext.Response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
                httpContext.Response.Headers.Add("Pragma", "no-cache");
                httpContext.Response.Headers.Add("Expires", "0");
                return Task.CompletedTask;
            }, context);

            await _next(context);
        }
    }
#311867
Nov 02, 2023 10:23
Vote:
 

Or without Middleware and directly in Startup 

        app.UseStaticFiles(new StaticFileOptions
        {
            OnPrepareResponse = context =>
            {
                if (context.File.Name.EndsWith(".xlsx"))
                {
                    context.Context.Response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
                    context.Context.Response.Headers.Add("Pragma", "no-cache");
                    context.Context.Response.Headers.Add("Expires", "0");
                }
            }
        });
#311868
Edited, Nov 02, 2023 10:29
Tam Duc Ha Vo - Nov 02, 2023 10:40
Will this work if the file is uploaded to assets pane? And the file is dragged over to an xhtmlstring area?
Quan Mai - Nov 02, 2023 11:19
this is at .net/cdn/browser level so it does not really matter where it's used or even the platform. it is telling the CDN/browser that this file should not be cached - always get latest from the server. however note that this will affect all files with xlsx extension, that might or might not be the behavior you'd like
Vote:
 

There's an open source addon that tries to solve it in a different way - you can refer to sources for inspiration https://github.com/episerver/EPiServer.CdnSupport

Basically, it modifies URL to include last modification date as a first URL segment - so it still be cached and delivered by CDN, but when it will be changed, new URL will be generated.

Won't work for your case if users bookmark URL; but will work if they use a download page with URLs generated by CMS.

Another similar approach, to not touch this globally - just add any query parameter to generated URL, like ?t={file_last_modified_date} - different query forces CDN to download file directly from server.

#311872
Nov 02, 2023 11:49
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.