dotnet / dotnet/aspnetcore

Feature Request: Cache-Control Header For Static Files

Open
#64,138 4 comments 1 reaction 0 assignees View on GitHub
area-middleware feature-static-files
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

When serving static files, I want to tell the browser how to handle caching, instead of letting it do whatever it pleases.

While [StaticFileMiddleware](https://github.com/dotnet/aspnetcore/blob/057669029e1d2f8a15c16249e28bef3944897776/src/Middleware/StaticFiles/src/StaticFileMiddleware.cs) does set [some caching headers](https://github.com/dotnet/aspnetcore/blob/057669029e1d2f8a15c16249e28bef3944897776/src/Middleware/StaticFiles/src/StaticFileContext.cs#L259), "Cache-Control" is not among them.

This is similar to #44153, but I would like to instruct the browser in production that files should not be expected to change for a significant period of time.

### Describe the solution you'd like

_Originally posted by @Tratcher in [#44153](https://github.com/dotnet/aspnetcore/issues/44153#issuecomment-1271980575)_

> - Add `string? CacheControl` to StaticFileOptions
>
> - Set this to `Cache-Control: max-age=0, must-revalidate` in the default host builder for IsDevelopment, but make sure it's easy to override for other environments.

This would allow easily setting the header per environment with only single line of code.
`builder.Services.Configure(builder.Configuration.GetSection("StaticFileOptions")`

### Additional context

[Work-arounds](https://andrewlock.net/adding-cache-control-headers-to-static-files-in-asp-net-core/) do exist, but are not ideal compared to something integrated.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.