17

I can't seem to enable caching of static files in ASP.NET Core 2.2. I have the following in my Configure:

public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
  if (env.IsDevelopment()) {
    app.UseDeveloperExceptionPage();
    app.UseCors(...);
  }
  else {
    app.UseHsts();
  }

  app.UseHttpsRedirection();
  app.UseAuthentication();
  app.UseSignalR(routes => { routes.MapHub<NotifyHub>("/..."); });

  app.UseResponseCompression();
  app.UseStaticFiles();
  app.UseSpaStaticFiles(new StaticFileOptions() {
    OnPrepareResponse = (ctx) => {
      ctx.Context.Response.Headers[HeaderNames.CacheControl] = "public, max-age=31557600"; // cache for 1 year
    }
  });
  app.UseMvc();

  app.UseSpa(spa => {
    spa.Options.SourcePath = "ClientApp";
    if (env.IsDevelopment()) {
      spa.UseVueCli(npmScript: "serve", port: 8080);
    }
  });
}

When I try and Audit the production site on HTTPS using chrome I keep getting "Serve static assets with an efficient cache policy":

audit screenshot

In the network tab there is no mention of caching in the headers, when I press F5 it seems everything is served from disk cache. But, how can I be sure my caching setting is working if the audit is showing its not?

network tab screenshot

2 Answers 2

15

This is working in ASP.NET Core 2.2 to 3.1:

I know this is a bit similar to Fredrik's answer but you don't have to type literal strings in order to get the cache control header

app.UseStaticFiles(new StaticFileOptions()
{
    HttpsCompression = Microsoft.AspNetCore.Http.Features.HttpsCompressionMode.Compress,               
    OnPrepareResponse = (context) =>
    {
        var headers = context.Context.Response.GetTypedHeaders();
        headers.CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue
        {
            Public = true,
            MaxAge = TimeSpan.FromDays(30)
        };
    }
});
3
  • you are missing Expires header.
    – Ali
    Jul 8, 2020 at 7:29
  • 2
    @Ali If a response contains both the Expires header and the max-age directive, max-age takes precedence. Reference: 13.2.4 Expiration Calculations - w3.org/Protocols/rfc2616/rfc2616-sec13.html The max-age directive takes priority over Expires, so if max-age is present in a response, the calculation is simply:
    – webStuff
    Nov 6, 2020 at 1:18
  • 4
    Note: it is not required to set the HttpsCompressionMode!
    – JHBonarius
    Jan 12, 2021 at 18:43
10

I do not know what UseSpaStaticFiles is but you can add cache options in UseStaticFiles. You have missed to set an Expires header.

// Use static files
app.UseStaticFiles(new StaticFileOptions {
    OnPrepareResponse = ctx =>
    {
        // Cache static files for 30 days
        ctx.Context.Response.Headers.Append("Cache-Control", "public,max-age=2592000");
        ctx.Context.Response.Headers.Append("Expires", DateTime.UtcNow.AddDays(30).ToString("R", CultureInfo.InvariantCulture));
    }
});

Beware that you also need a way to invalidate cache when you make changes to static files.

I have written a blog post about this: Minify and cache static files in ASP.NET Core

2
  • Hi FS. Question; for cache invalidation, why did you decide to not use the MVC 'asp-append-version' tag. I.e. --> <script asp-append-version="true" src="..."></script> Jul 9, 2020 at 14:59
  • 1
    Hi, I want to use pure HTML and pure JavaScript as much as possible. Jul 12, 2020 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.