0

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.

Joe
  • 1,847
  • 2
  • 17
  • 26
  • Setting [response.Cacheability](https://msdn.microsoft.com/en-us/library/system.web.optimization.bundleresponse.cacheability(v=vs.110).aspx) to [NoCache](https://msdn.microsoft.com/en-us/library/system.web.httpcacheability(v=vs.110).aspx) will work, but this would mean that the bundle is never cached, is that fine? – Hari Hara Chandan Aug 05 '15 at 10:39
  • Not really, my real Transform searches for url properties in css files and replaces the url with a versioned url if the file has changed since it was last cached. So I would like to break the cache only when a file linked from the css file has changed. Thanks for the tip though. – Joe Aug 05 '15 at 11:09
  • If so, how about context.HttpContext.Response.Cache.[SetRevalidation](https://msdn.microsoft.com/en-us/library/system.web.httpcachepolicy.setrevalidation(v=vs.110).aspx)? – Hari Hara Chandan Aug 05 '15 at 12:16
  • This seems to work well, thanks for pointing it out! If you want to add an answer I'll accept it. – Joe Aug 06 '15 at 09:12

1 Answers1

0

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