MVC4 Bundling Cache Headers MVC4 Bundling Cache Headers asp.net asp.net

MVC4 Bundling Cache Headers


This isn't something that we currently expose today. We only expose the Cacheability property on the BundleResponse that a IBundleTransform could change. And yes we explicitly set the following things:

                HttpCachePolicyBase cachePolicy = context.HttpContext.Response.Cache;                cachePolicy.SetCacheability(bundleResponse.Cacheability);                cachePolicy.SetOmitVaryStar(true);                cachePolicy.SetExpires(DateTime.Now.AddYears(1));                cachePolicy.SetValidUntilExpires(true);                cachePolicy.SetLastModified(DateTime.Now);                cachePolicy.VaryByHeaders["User-Agent"] = true;

We have a work item our backlog to open this up and make this more extensible/customizable in the future.


There is a workaround around it as mentioned in janv8000's comment on this response. You need to add the following URL rewrite rule to your web server:

<system.webServer>    <rewrite>        <outboundRules>            <rule name="Cache Bundles" preCondition="IsBundles" patternSyntax="ExactMatch">                <match serverVariable="RESPONSE_Vary" pattern="User-Agent" />                <action type="Rewrite" value="Accept-Encoding" />            </rule>            <preConditions>                <preCondition name="IsBundles" patternSyntax="Wildcard">                    <add input="{URL}" pattern="*/bundles/*" />                </preCondition>            </preConditions>        </outboundRules>    </rewrite></system.webServer>

Obviously you need to pay attention to have all your bundles in your bundles folder or change the IsBundles precondition accordingly.