Skip to content

Commit

Permalink
Use of new StaticFileOptions for Modules (#10118)
Browse files Browse the repository at this point in the history
Fixes #6505
Fixes #2966
  • Loading branch information
ns8482e authored Sep 2, 2021
1 parent 5decf3c commit ac1402b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,19 +207,29 @@ private static void AddStaticFiles(OrchardCoreBuilder builder)
{
var fileProvider = serviceProvider.GetRequiredService<IModuleStaticFileProvider>();

var options = serviceProvider.GetRequiredService<IOptions<StaticFileOptions>>().Value;

options.RequestPath = "";
options.FileProvider = fileProvider;

var shellConfiguration = serviceProvider.GetRequiredService<IShellConfiguration>();

var cacheControl = shellConfiguration.GetValue("StaticFileOptions:CacheControl", $"public, max-age={TimeSpan.FromDays(30).TotalSeconds}, s-max-age={TimeSpan.FromDays(365.25).TotalSeconds}");
// Cache static files for a year as they are coming from embedded resources and should not vary.
var cacheControl = shellConfiguration.GetValue("StaticFileOptions:CacheControl", $"public, max-age={TimeSpan.FromDays(30).TotalSeconds}, s-maxage={TimeSpan.FromDays(365.25).TotalSeconds}");

// Cache static files for a year as they are coming from embedded resources and should not vary
options.OnPrepareResponse = ctx =>
// Use the current options values but without mutating the resolved instance.
var options = serviceProvider.GetRequiredService<IOptions<StaticFileOptions>>().Value;
options = new StaticFileOptions
{
ctx.Context.Response.Headers[HeaderNames.CacheControl] = cacheControl;
RequestPath = String.Empty,
FileProvider = fileProvider,

#if NET5_0_OR_GREATER
RedirectToAppendTrailingSlash = options.RedirectToAppendTrailingSlash,
#endif
ContentTypeProvider = options.ContentTypeProvider,
DefaultContentType = options.DefaultContentType,
ServeUnknownFileTypes = options.ServeUnknownFileTypes,
HttpsCompression = options.HttpsCompression,

OnPrepareResponse = ctx =>
{
ctx.Context.Response.Headers[HeaderNames.CacheControl] = cacheControl;
},
};

app.UseStaticFiles(options);
Expand Down
4 changes: 2 additions & 2 deletions src/docs/reference/core/Modules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ You can find a sample application here: [`OrchardCore.Mvc.Web`](../../../../Orch

## Configuration

The following configuration values are used by default and can be customized:
The following configuration values are used by default for module embedded static files and can be customized:

```json
"StaticFileOptions": {
// The CacheControl header sent with any static file served by modules
"CacheControl": "public, max-age=2592000, s-max-age=31557600"
"CacheControl": "public, max-age=2592000, s-maxage=31557600"
}
```

0 comments on commit ac1402b

Please sign in to comment.