MVC4 Bundling Cache Headers
Asked Answered
M

2

23

I want to change the cache headers sent from a bundle request. Currently it is varying by User-Agent but I don't want it to, is there a way to change the headers sent by a bundle request?

After a quick look in the System.Web.Optimization assembly I can see the headers get set in Bundle.SetHeaders which is a private static function so I don't think its possible although I would love to be proven wrong.

Myel answered 25/10, 2012 at 9:15 Comment(3)
I'd also be interested in any thoughts from anyone on the logic for adding this header in the first place - e.g. does the bundling code itself behave differently depending on the user agent? Or is it just a precaution?Attributive
In regards to why we do this, its needed for the VS Page Inspector feature which sends a custom user agent using IE, so to prevent IE's cache from messing up requests to either page inspector, or serving page inspector bundles to IE outside of VS.Calpac
@HaoKung I have the same problem, I want to be able to set the cache-control header value to public,max-age=31536000 instead of private, and to also add an Etag header. It looks like both of these are not currently possible?Egoist
C
12

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.

Calpac answered 25/10, 2012 at 17:43 Comment(3)
is there any indication as to the timeline on this change, the impact of this header plays havoc with CDN integrationCodex
Issue tracker url: aspnetoptimization.codeplex.com/workitem/136 . Also mentions a possible workaround.Oceangoing
It's been almost six years. Any progress on fixing this?Fitch
M
2

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.

Matchlock answered 10/9, 2017 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.