Skip to content

Commit b7dd580

Browse files
hakonamatataMarkPieszak
authored andcommitted
browser caching (TrilonIO#542)
closes TrilonIO#541
1 parent 45d6eaa commit b7dd580

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

Startup.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using Microsoft.EntityFrameworkCore;
1010
using AspCoreServer.Data;
1111
using Swashbuckle.AspNetCore.Swagger;
12+
using Microsoft.AspNetCore.Http;
13+
using Microsoft.Net.Http.Headers;
1214

1315
namespace AspCoreServer
1416
{
@@ -64,7 +66,30 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
6466
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
6567
loggerFactory.AddDebug();
6668

67-
app.UseStaticFiles();
69+
// app.UseStaticFiles();
70+
71+
app.UseStaticFiles(new StaticFileOptions()
72+
{
73+
OnPrepareResponse = c =>
74+
{
75+
//Do not add cache to json files. We need to have new versions when we add new translations.
76+
77+
if (!c.Context.Request.Path.Value.Contains(".json"))
78+
{
79+
c.Context.Response.GetTypedHeaders().CacheControl = new CacheControlHeaderValue()
80+
{
81+
MaxAge = TimeSpan.FromDays(30) // Cache everything except json for 30 days
82+
};
83+
}
84+
else
85+
{
86+
c.Context.Response.GetTypedHeaders().CacheControl = new CacheControlHeaderValue()
87+
{
88+
MaxAge = TimeSpan.FromMinutes(15) // Cache json for 15 minutes
89+
};
90+
}
91+
}
92+
});
6893

6994
DbInitializer.Initialize(context);
7095

0 commit comments

Comments
 (0)