MVC's HTTP Caching - Last-Modified response header always equals Date
Asked Answered
N

1

7

I'm not fully understanding how .NET MVC's HTTP caching works because it doesn't seem like it's actually retrieving cached resource files. I'm thinking I need to add some additional code somewhere...

First, let's take a look at how I've set up HTTP caching on static content (ie. images). In my web.config, I have the following:

<system.webServer>
    <staticContent>
      <clientCache cacheControlMode="UseExpires" httpExpires="Tue, 19 Jan 2038 03:14:07 GMT" />
    </staticContent>
</system.webServer>

This results in the images in my application to appear to be caching properly. When I look at the response headers for an image, I see this (removed unnecessary headers):

    Date:Thu, 27 Feb 2014 16:27:48 GMT
    ETag:"086f8d199a4ce1:0"
    Expires:Tue, 19 Jan 2038 03:14:07 GMT
    Last-Modified:Thu, 29 Aug 2013 09:26:20 GMT

I'm seeing an ETag value which is good and my Expires is what it should be. Additionally, the Last-Modified date is in the past. I understand the Last-Modified date to be the date the server was last asked for that file.

Now let's look at the response headers for a javascript file that has been optimized by MVC. As a reminder, this article states that "Bundles set the HTTP Expires Header one year from when the bundle is created."

    Cache-Control:public
    Date:Thu, 27 Feb 2014 16:44:16 GMT
    Expires:Fri, 27 Feb 2015 16:44:16 GMT
    Last-Modified:Thu, 27 Feb 2014 16:44:16 GMT
    Vary:User-Agent

The Response Headers for the MVC cached file is missing the ETag for one. There is a Cache-Control value of "public" which wasn't present on the static content response header. Lastly, the Expires is 1 year after the Last-Modified date which is correct, but the Last-Modified date is always the same as the Date value. These response headers to me seem like what they'd be when a resource is requested from the server for the first time and cached, not when it's been subsequently requested and retrieved from cache.

Thanks in advance for any insight.

UPDATE: It actually seems to be caching in IE. The Last-Modified date on subsequent requests remains a value in the past. I'm not seeing this in FF or Chrome, though. I confirmed that in both of those browsers, I haven't disabled caching. What gives?

Number answered 27/2, 2014 at 17:0 Comment(2)
are you behind a proxy or filtering interceptor of any kind? i came across this question while debugging an issue that has many of the same symptoms and determined it was a corporate proxy withholding updates to the bundlesHumanism
@meklarian: Thanks for chiming in. When I logged this, I was indeed likely behind a company proxy!Number
R
0

First of all, system.webServer/staticContent/clientCache is for static resources (files) only. So it works if you directly access pictures.

A bundle is a dynamic resource (a handler is generating the content). That is why the configuration directive does not apply.


Secondly, using ETag, Expires, Last-Modified are three different caching techniques. You should not use both techniques together as they work in a different way.

Expires tells the browser to keep the file in cache until a specified date. The browser will never call the server until that date.

ETag is a dynamic cache mechanism. The browser will always call the server but the server may not respond with the content if it has not changed.

Last-Modified is an ancient dynamic cache mechanism. It work the same way as ETag but is has a problematic requirement of exposing the correct modification date. Which is not a easy thing to find when creating dynamic content.

I think that combining multiple techniques should be reserved for well thought cases only. See this answer for details.

You may want to read the basics: HTTP caching


And then for your issue, I am not sure why so many header are exposed by your app. The presence of Last-Modified on a bundle is strange to explain. A bundle is a virtual thing that exposes various files combined as one. So it has no real date of last modification. Are you using extra caching code/modules?

Refluent answered 12/12, 2017 at 12:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.