IIS Output Caching for Blazor Server Self Contained App

179 views Asked by At

I'm hosting a Blazor Server app (Self contained publish/deployment mode) in IIS 10.

I'm familiar with setting up Output Caching for older ASP.net projects, and would like to use it to cache static files inside the Blazor project (css, js, png, jpg).

After setting up User Mode & Kernel Mode caching in IIS, I run the following command to confirm cache entries

netsh http show cachestate

Result:

There were no cache entries corresponding to the provided URL

I'm suspecting that is because the project was published as Self Contained.

Should I be using ASP.NET core's Middleware Output caching for static files like JS, CSS, JPG?

1

There are 1 answers

3
DM-98 On

Try this caching strategy of static files inside wwwroot:

app.UseStaticFiles(new StaticFileOptions
{
    OnPrepareResponse = ctx =>
    {
        const int durationInSeconds = 60 * 60 * 24;
        ctx.Context.Response.Headers[HeaderNames.CacheControl] = "public,max-age=" + durationInSeconds;
    }
});