Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Enable cache-control for asp.net bundling for cloudflare support

Vote:
 

Hey all,

We have asp.net standard bundling in our website which create paths for js/css such as

  1. /bundles/js?v=p40eNl6CcCesxNATLxv2uMmgN_WnURgbfyzXa2-j-mI1

It was raised in some load tests that cloudflare is not caching these path like it would normal js/css based upon the standard caching profiles as below.

      <profiles>
        <add extension=".gif" policy="DontCache" kernelCachePolicy="CacheUntilChange" />
        <add extension=".png" policy="DontCache" kernelCachePolicy="CacheUntilChange" />
        <add extension=".js" policy="DontCache" kernelCachePolicy="CacheUntilChange" />
        <add extension=".css" policy="DontCache" kernelCachePolicy="CacheUntilChange" />
        <add extension=".jpg" policy="DontCache" kernelCachePolicy="CacheUntilChange" />
        <add extension=".jpeg" policy="DontCache" kernelCachePolicy="CacheUntilChange" />
        <add extension=".jpe" policy="DontCache" kernelCachePolicy="CacheUntilChange" />
        <add extension=".svg" policy="DontCache" kernelCachePolicy="CacheUntilChange" />
        <add extension=".woff2" policy="DontCache" kernelCachePolicy="CacheUntilChange" />
        <add extension=".woff" policy="DontCache" kernelCachePolicy="CacheUntilChange" />
        <add extension=".ttf" policy="DontCache" kernelCachePolicy="CacheUntilChange" />
        <add extension=".eot" policy="DontCache" kernelCachePolicy="CacheUntilChange" />
      </profiles>

This looks to be as the cache-control header is set to public. Does anyone know how to get the bundling system to change this cache control header? I've followed some posts online but can't seem to get it working.

Thanks in advance

#204691
Jun 13, 2019 11:19
Vote:
 

Apologies, the profiles section was an example of the bits that are working, the issue is that the bundled references that are generated in asp.net do not have an extension so aren't caught and are being set as public cache-control which is causing them not to be cached by cloudflare. Extension based files are working fine

#204694
Jun 13, 2019 12:29
Vote:
 

Hi Scott,

I may be missing something but, as far as I'm aware, if you want your content to be cached, you want cache control set to public, ideally with a max-age value set to > 0. If you need to change that header and the usual web.config options aren't working you can hook in to the Application_PreSendRequestHeaders event either in global.asax or in an httpmodule and modify the response headers in there.

One thing which I was made aware of recently is the following line from the CloudFlare docs:

"If the Cache-Control header is set to "private", "no-store", "no-cache", or "max-age=0", or if there is a cookie in the response, then Cloudflare will not cache the resource."

I don't know whether this applies to your situation but, if the responses from the bundle requests return a cookie for any reason, they won't be cached, regardless of the headers set. If that's your issue you may need to get the cloudflare config tweaked or do something to strip out the cookies before sending the response.

Hope that helps.

#204698
Jun 13, 2019 13:02
- Dec 01, 2019 9:24
Old post but just commenting on the "Application_PreSendRequestHeaders" event usage to modify headers - DO NOT USE IT (see the warning in MSDN documentation: https://docs.microsoft.com/en-us/dotnet/api/system.web.httpapplication.presendrequestheaders?view=netframework-4.7.2)! global.asax events are generated IHttpModules. Instead use the HttpResponse.AddOnSendingHeaders: https://docs.microsoft.com/en-us/dotnet/api/system.web.httpresponse.addonsendingheaders?view=netframework-4.7.2

So in global.asax in 'Application_BeginRequest' hook up to the current response.AddOnSendingHeaders
Vote:
 

One other thing which comes to mind - by default CloudFlare only caches content which it considers to be static and, as far as I'm aware, it does this by looking at the file extension. In your situation I don't see a file extension and so it may well be that CloudFlare is assuming it's dynamic and not caching it regardless of the headers set. If this is the issue (and you can't change the URLs to include a file extension) you'll need to get a page rule set up in CloudFlare to allow that content to be cached.

#204703
Jun 13, 2019 13:38
Vote:
 

As Paul said. Add a file extension to the bundle name. We had and fixed the same problem this way.

#204712
Jun 13, 2019 17:47
Vote:
 

How do I add a file extension when it's auto generated? This was actually my first thought to make the css bundle have .css and the js one to have .js but couldn't see how to change the name?

#204713
Jun 13, 2019 18:09
Vote:
 

Jesus I've been an idiot, I was trying to sort this issue earlier on and I completely forgot our bundle paths are in constants.

#204714
Jun 13, 2019 18:13
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.