2

I have a method for resizing images in my application. The method returns an ImageResult like it is shown below.

    [CachingActionFilter(_maxAgeDuration)]
    public ActionResult GetStaticImage(string filePath, int w, int h)
    {
        try
        {
            FileInfo fileInfo = new FileInfo(Server.MapPath(filePath));
            if (!fileInfo.Exists)
            {
                LogException($"Could not find {fileInfo.FullName}. GetStaticImage() returns HttpNotFoundResult");
                return new HttpNotFoundResult();
            }

            var img = new WebImage(fileInfo.FullName).Resize(w, h, false, true);
            return new ImageResult(new MemoryStream(img.GetBytes()), "binary/octet-stream");
        }
        catch (Exception ex)
        {
            LogException(ex);
            return new HttpNotFoundResult();
        }
    }

The caching filter works as it is shown below.

    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        var httpContext = filterContext.RequestContext.HttpContext;

        // Some validations not relative to the issue 

        httpContext.Response.Cache.SetCacheability(HttpCacheability.Private);
        httpContext.Response.Cache.SetMaxAge(new TimeSpan(0, 0, _maxAgeSeconds));

        base.OnResultExecuting(filterContext);
    }

Now I run the application in Chrome I see in firebug (after the F5 refresh) in my Network tab that the request is under the Img tab and is cached from the client

When I run the application in Firefox I see (after the F5 refresh) in my Network tab that the request is under the Others tab and is not cached.

The things I tried are:

1.Use the OutputCache for the caching (with both server and client side caching) of the Action and the result was the same

[OutputCache(Duration = 1000000, Location = OutputCacheLocation.Server)]

2.Change the contentType of the result from the method from binary/octet-stream to image/jpeg and though I saw the request under the Images tag is still wasn't cached.

Does anyone know what is happening? I should remind if it is not clear that the caching mechanism for a user that just goes from one page to another (where the same request is used) works correctly. The only case it doesn't work is when the user refreshes the page by F5 or from the button (ctrl+R) in Firefox. (Since it is not a hard refresh this shouldn't happen)

Anastasios Selmani
  • 3,579
  • 3
  • 32
  • 48
  • This seems like an implementation choice of FireFox. Your server can add all of the caching headers it wants, but it is up to the implementation of the client whether or not to heed them. AFAIK, the implementation of "refresh" is not something that is specified in web standards, so there may be differences that cannot be normalized between browsers. – NightOwl888 Oct 18 '17 at 09:56
  • I would probably think that the ctrl+f5 mentioned as hard refresh and used explicitly for refreshing the cached items from the browser would point to the simple refresh as not doing that. But from your point of view it could be something that the browser just doesn't want to cache. All I have looked though these days from this perspective haven't led me anywhere. Some mention possibly that ImageResults aren't cached the same way as jsonResults for example and especially the server part caching that doesn't work is the thing that looks strange to me the most. – Anastasios Selmani Oct 18 '17 at 10:15
  • 1
    You mentioned that it "works right when a user goes from one page to another". As far as web development is concerned that is all you can hope for, as the back, forward, and refresh buttons can do whatever the implementer decides. It would be highly unusual for a web application to depend on these buttons rather than supplying its own navigation, anyway. That said, it is not unusual to *disable* caching in some scenarios to ensure, for example, a series of forms that is completed doesn't allow the user to navigate backward to see sensitive data. – NightOwl888 Oct 18 '17 at 14:01

0 Answers0