mirror of
https://github.com/bitwarden/server.git
synced 2024-11-22 12:15:36 +01:00
add static files caching
This commit is contained in:
parent
05b8c4e489
commit
36cf628a63
@ -1,4 +1,7 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@ -7,6 +10,15 @@ namespace Bit.Server
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
private readonly List<string> _longCachedPaths = new List<string>
|
||||
{
|
||||
"/app/", "/locales/", "/fonts/", "/connectors/", "/scripts/"
|
||||
};
|
||||
private readonly List<string> _mediumCachedPaths = new List<string>
|
||||
{
|
||||
"/images/"
|
||||
};
|
||||
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{ }
|
||||
|
||||
@ -30,7 +42,31 @@ namespace Bit.Server
|
||||
}
|
||||
else
|
||||
{
|
||||
app.UseFileServer();
|
||||
var options = new DefaultFilesOptions();
|
||||
options.DefaultFileNames.Clear();
|
||||
options.DefaultFileNames.Add("index.html");
|
||||
app.UseDefaultFiles(options);
|
||||
app.UseStaticFiles(new StaticFileOptions
|
||||
{
|
||||
OnPrepareResponse = ctx =>
|
||||
{
|
||||
if(!ctx.Context.Request.Path.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var path = ctx.Context.Request.Path.Value;
|
||||
if(_longCachedPaths.Any(ext => path.StartsWith(ext)))
|
||||
{
|
||||
// 14 days
|
||||
ctx.Context.Response.Headers.Append("Cache-Control", "max-age=1209600");
|
||||
}
|
||||
if(_mediumCachedPaths.Any(ext => path.StartsWith(ext)))
|
||||
{
|
||||
// 7 days
|
||||
ctx.Context.Response.Headers.Append("Cache-Control", "max-age=604800");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user