Skip to content

Commit 015eebb

Browse files
committed
response compression
1 parent f3532b7 commit 015eebb

1 file changed

Lines changed: 22 additions & 10 deletions

File tree

src/Website/Program.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using Microsoft.AspNetCore.DataProtection;
44
using Microsoft.AspNetCore.HttpOverrides;
55
using Microsoft.AspNetCore.Mvc.ApplicationModels;
6+
using Microsoft.AspNetCore.ResponseCompression;
7+
using Microsoft.AspNetCore.Server.Kestrel.Core;
68
using Microsoft.EntityFrameworkCore;
79
using Website.Common;
810
using Website.Services;
@@ -28,14 +30,30 @@
2830
services.AddDbContext<DatabaseContext>(options =>
2931
options.UseSqlite(config.GetConnectionString("WebsiteDatabase")));
3032

33+
// Compression
34+
services.AddResponseCompression(options =>
35+
{
36+
options.Providers.Add<BrotliCompressionProvider>();
37+
options.Providers.Add<GzipCompressionProvider>();
38+
});
39+
3140
// Razor pages (most pages on this site)
32-
services.AddRazorPages(options =>
41+
var razorBuilder = services.AddRazorPages(options =>
3342
{
43+
options.Conventions.Add(new PageRouteTransformerConvention(new SlugifyParameterTransformer()));
44+
3445
options.Conventions.AuthorizePage("/Admin");
3546
});
3647

3748
// Allows us to use controllers alongside razor pages
38-
services.AddMvc();
49+
var mvcBuilder = services.AddMvc();
50+
51+
// Allows compiling within development environment
52+
if (environment.IsDevelopment())
53+
{
54+
razorBuilder.AddRazorRuntimeCompilation();
55+
mvcBuilder.AddRazorRuntimeCompilation();
56+
}
3957

4058
// Bundle and minify our JS and CSS
4159
services.AddWebOptimizer(pipeline =>
@@ -73,14 +91,6 @@
7391
services.AddSingleton<SoundByteAuthenticationService>();
7492
services.AddSingleton<R2>();
7593

76-
var mvcBuilder = services.AddRazorPages(options =>
77-
options.Conventions.Add(new PageRouteTransformerConvention(new SlugifyParameterTransformer())));
78-
79-
if (environment.IsDevelopment())
80-
{
81-
mvcBuilder.AddRazorRuntimeCompilation();
82-
}
83-
8494
// ----- App ----- //
8595

8696
var app = builder.Build();
@@ -128,6 +138,8 @@
128138
app.UseAuthentication();
129139
app.UseAuthorization();
130140

141+
app.UseResponseCompression();
142+
131143
app.MapHtmxAntiforgeryScript();
132144

133145
app.MapRazorPages();

0 commit comments

Comments
 (0)