Search code examples
c#asp.netcachingbundling-and-minificationasp.net-optimization

c# Bundle Transform Break cache


I would like to create a bundle transform that modifies the bundle content and triggers the cache to break, however I am not able to break the cache.

So far I have the following code:-

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        StyleBundle bundle = new StyleBundle("~/Content/Global");
        bundle.Transforms.Add(new CacheBreakTransform());
        bundle.Include("~/css/main.css");
        bundles.Add(bundle);
    }
}

public class CachBreakTransform : IBundleTransform
{
    void IBundleTransform.Process(BundleContext context, BundleResponse response)
    {
        if(UpdateNeeded())
        {
            response.Content = GetUpdatedContent(response.Content);
            //cache break needed here
        }
    }
}

I can update the content of the bundle, but if the file included in the bundle hasn't changed the bundle is still cached and the updated content will not be used. I would like to be able to break the bundle cache so that the updated content is always used.


Solution

  • Two options

    1. Set context.HttpContext.Response.Cache.SetRevalidation

    2. Setting response.Cacheability to NoCache will work, but this would mean that the bundle is never cached