Skip to content

Commit f7f946a

Browse files
committed
Serve 'Blazor-Environment' header
1 parent 542f2b9 commit f7f946a

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/Components/WebAssembly/DevServer/src/Server/Startup.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.AspNetCore.Hosting;
56
using Microsoft.AspNetCore.Http;
67
using Microsoft.Extensions.Configuration;
78
using Microsoft.Extensions.DependencyInjection;
@@ -29,27 +30,29 @@ public static void Configure(IApplicationBuilder app, IConfiguration configurati
2930

3031
app.UseWebAssemblyDebugging();
3132

32-
bool applyCopHeaders = configuration.GetValue<bool>("ApplyCopHeaders");
33+
var webHostEnvironment = app.ApplicationServices.GetRequiredService<IWebHostEnvironment>();
34+
var applyCopHeaders = configuration.GetValue<bool>("ApplyCopHeaders");
3335

34-
if (applyCopHeaders)
36+
app.Use(async (ctx, next) =>
3537
{
36-
app.Use(async (ctx, next) =>
38+
if (ctx.Request.Path.StartsWithSegments("/_framework") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.server.js") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.web.js"))
3739
{
38-
if (ctx.Request.Path.StartsWithSegments("/_framework") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.server.js") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.web.js"))
40+
ctx.Response.Headers.Append("Blazor-Environment", webHostEnvironment.EnvironmentName);
41+
42+
if (applyCopHeaders)
3943
{
40-
string fileExtension = Path.GetExtension(ctx.Request.Path);
41-
if (string.Equals(fileExtension, ".js"))
44+
var fileExtension = Path.GetExtension(ctx.Request.Path);
45+
if (string.Equals(fileExtension, ".js", StringComparison.Ordinal))
4246
{
4347
// Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer.
4448
ApplyCrossOriginPolicyHeaders(ctx);
4549
}
4650
}
51+
}
4752

48-
await next(ctx);
49-
});
50-
}
53+
await next(ctx);
54+
});
5155

52-
//app.UseBlazorFrameworkFiles();
5356
app.UseRouting();
5457

5558
app.UseStaticFiles(new StaticFileOptions

src/Components/test/E2ETest/Tests/WebAssemblyConfigurationTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public void WebAssemblyConfiguration_Works()
3737
// Verify values from the default 'appsettings.json' are read.
3838
Browser.Equal("Default key1-value", () => _appElement.FindElement(By.Id("key1")).Text);
3939

40+
// Verify that the dev server always correctly serves the 'Blazor-Environment: Development' header.
41+
Browser.Equal("Development", () => _appElement.FindElement(By.Id("environment")).Text);
42+
4043
if (_serverFixture.TestTrimmedOrMultithreadingApps)
4144
{
4245
// Verify values overriden by an environment specific 'appsettings.$(Environment).json are read

0 commit comments

Comments
 (0)