Feature Request: Cache-Control Header For 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
Assessment
This issue has not been assessed yet.