Combined files not being cached on client

Nov 6, 2012 at 11:14 PM

First and foremost, great module.

However, the combined files don't appear to be cached on the client. I am always seeing a response 200 with cache-control private.

Not sure if this is due to my setup, but is returning the desired 304... interestingly, this site does not include the timestamp in the query string.

As a quick initial test, I did try removing the addition of the timestamp in CombinatorService.cs but still no client side caching.

Any thoughts would be appreciated.

Nov 6, 2012 at 11:20 PM

What version of Orchard are you using? I'm using combinator and the cache control is being set properly on the combined resources. If you are using plain IIS (as opposed to Azure), the cache control headers should be controlled by the Media/web.config in the Orchard.Web project. Here is the snippet that controls it: 

      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />

If you are using Azure I think you have to take special measures to set the cache control headers, because the combined files are served from Blob storage. I'm not sure what the defaults are. 

Nov 6, 2012 at 11:30 PM

Version 1.6 with the Media Folder module pointing to an alternative path (point at the local media folder on dev and a unc path to a fileshare on prod). All running on plain IIS... the Media Folder module may be a clue come to think of it. Neither dev nor prod return a 304.



Nov 6, 2012 at 11:48 PM

I don't get a 304 in chrome dev tools, it shows up as 200, but with "(from cache)" in the "Size" column. The browser knows the max-age hasn't passed yet so it uses the copy from its cache instead of sending an HTTP request. If you are testing caching in the browser make sure you haven't disabled cache. 

Nov 7, 2012 at 11:32 AM

Good to see you guys figuring it out :-). Combinator is not dealing with cache headers so yes, if you want to set them, you may want to do it from the Web.config.

Nov 8, 2012 at 12:47 AM

Thanks for your comments. I haven't had much of a chance to look into this further, but I will be focussing on the Media Folder module to resolve this. All Web.config etc. are in place. In my situation IE, FF and Chrome all return a 200 and no indication of "from cache" as mentioned. 

We are building an interesting setup with load-balanced / failover boxes, quite succesfully, but the devil is in the detail. It now turns out we had the issue of not caching media files before, but this was never really an issue due to all links to media files (other than static files such as css and js) being rewritten to a very well behaved CDN when it comes to cache headers.

I am happy that Combinator is not behind this issue. Though I am still curious why the Orchard Gallery site is not including the timestamp?


Nov 8, 2012 at 1:06 AM

The orchard gallery site is running on Azure, and the file storage provider for Orchard under Azure is blob storage. Blob storage is separate from the Media folder, and doesn't get controlled by the web.config settings for the Media folder (nor by any web.config folder settings in a .NET site that references files in blob storage). 

You can set http headers for items in Azure blob storage, but you'll have to write code to do it, see here for an example: By default blob stored items have no expires or cache-control headers set, so you are at the mercy of browsers' default logic. 

Nov 8, 2012 at 8:28 AM

@anoordende: what you build looks very interesting. Combinator is designed to run on multi-server environents without problems and concurrency issues. I'd be very interested how it works out for you (more precisely: even in a multi-node environment Combinator should only process resources once and store them globally accessible if the Media folder and DB is properly shared).

The Orchard Gallery most possible uses an older version of Combinator. Before a few versions Combinator didn't have the browser cache-bashing feature of timestamps in urls.

Also I think your browser should use a cached version of the files by default (I don't know whether there's a specification for that but I've seen this is the common behaviour), even if no cache control headers were set.

Nov 10, 2012 at 12:59 AM

It's been a fun-filled few days but I thought a quick update with the solution is on order. After trying various ways of getting IIS to play nice with Web.config, either in the alternative Media location or in the default Media location, turns out that yes with a lot of tinkering it *might* be possible but that would be at the cost of the Media Folder module being "plug & play", e.g. we would have to configure virtual directories or a seperate site from which we can serve the media directly.

Core challenge was that the alternative Media path is orchestrated completely within Orchard and therefore IIS still looks for any media files under the default Media folder, including corresponding Web.config files. An added challenge was that the Media Folder module drops the tenant name, e.g. "Default" from the URL (which is btw a very nice feature!), but the tenant name is still used internally to map to the Media files.

I have created a fork anoordende/clientcache (although changes not yet committed) under the Media Folder project that resolves the issue by re-utilising the clientCache section from the Web.config in the default Media location and by the implementation of an "IMediaClientCache" which can also be overriden by any other module using a priority.

The default implementation of the IMediaClientCache sets the correct Cache-control, LastModified, etc. and returns a 304 as required if the client includes the If-Modified-Since header. A side-effect of this is that we're no longer depending on IIS to monitor UNC files for changes, which can be problematic to set up correctly.

A fantastic side effect for me personally is that all my files stored under media are rewritten to a CDN, therefore in combination with the Combinator Module I get .css and .js served from the CDN thrown in for free. Never seen my sites load so fast. Happy days.

This discussion turned out to be in the wrong place, so I will create a new placeholder under the Media Folder module with a link to here. I will still report back on how I get on with Combinator in my multi server setup.

Nov 10, 2012 at 12:56 PM

Thanks for taking the time to write down your findings. I'm happy that in the end you could find a solution. And awaiting your multi-server experiences!