From e79d9859ab2be14c9085d8424a0fc8754af6d23d Mon Sep 17 00:00:00 2001 From: Mackinnon Buck Date: Thu, 19 Sep 2024 12:05:30 -0700 Subject: [PATCH 1/3] Serve 'Blazor-Environment' header --- .../DevServer/src/Server/Startup.cs | 23 +++++++++++-------- .../Tests/WebAssemblyConfigurationTest.cs | 3 +++ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/Components/WebAssembly/DevServer/src/Server/Startup.cs b/src/Components/WebAssembly/DevServer/src/Server/Startup.cs index 342370171656..7a5c05f6af2e 100644 --- a/src/Components/WebAssembly/DevServer/src/Server/Startup.cs +++ b/src/Components/WebAssembly/DevServer/src/Server/Startup.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -29,27 +30,29 @@ public static void Configure(IApplicationBuilder app, IConfiguration configurati app.UseWebAssemblyDebugging(); - bool applyCopHeaders = configuration.GetValue("ApplyCopHeaders"); + var webHostEnvironment = app.ApplicationServices.GetRequiredService(); + var applyCopHeaders = configuration.GetValue("ApplyCopHeaders"); - if (applyCopHeaders) + app.Use(async (ctx, next) => { - app.Use(async (ctx, next) => + if (ctx.Request.Path.StartsWithSegments("/_framework") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.server.js") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.web.js")) { - if (ctx.Request.Path.StartsWithSegments("/_framework") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.server.js") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.web.js")) + ctx.Response.Headers.Append("Blazor-Environment", webHostEnvironment.EnvironmentName); + + if (applyCopHeaders) { - string fileExtension = Path.GetExtension(ctx.Request.Path); - if (string.Equals(fileExtension, ".js")) + var fileExtension = Path.GetExtension(ctx.Request.Path); + if (string.Equals(fileExtension, ".js", StringComparison.Ordinal)) { // Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer. ApplyCrossOriginPolicyHeaders(ctx); } } + } - await next(ctx); - }); - } + await next(ctx); + }); - //app.UseBlazorFrameworkFiles(); app.UseRouting(); app.UseStaticFiles(new StaticFileOptions diff --git a/src/Components/test/E2ETest/Tests/WebAssemblyConfigurationTest.cs b/src/Components/test/E2ETest/Tests/WebAssemblyConfigurationTest.cs index aee9648a4109..5c298cb4c32e 100644 --- a/src/Components/test/E2ETest/Tests/WebAssemblyConfigurationTest.cs +++ b/src/Components/test/E2ETest/Tests/WebAssemblyConfigurationTest.cs @@ -37,6 +37,9 @@ public void WebAssemblyConfiguration_Works() // Verify values from the default 'appsettings.json' are read. Browser.Equal("Default key1-value", () => _appElement.FindElement(By.Id("key1")).Text); + // Verify that the dev server always correctly serves the 'Blazor-Environment: Development' header. + Browser.Equal("Development", () => _appElement.FindElement(By.Id("environment")).Text); + if (_serverFixture.TestTrimmedOrMultithreadingApps) { // Verify values overriden by an environment specific 'appsettings.$(Environment).json are read From f3e8442ee83e2f49ea7f56fbf36c2bdbc5cbf090 Mon Sep 17 00:00:00 2001 From: Mackinnon Buck Date: Thu, 19 Sep 2024 13:32:58 -0700 Subject: [PATCH 2/3] PR feedback --- .../WebAssembly/DevServer/src/Server/Startup.cs | 17 ++++++++--------- .../Tests/WebAssemblyConfigurationTest.cs | 9 ++++++--- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/Components/WebAssembly/DevServer/src/Server/Startup.cs b/src/Components/WebAssembly/DevServer/src/Server/Startup.cs index 7a5c05f6af2e..b81891c478ec 100644 --- a/src/Components/WebAssembly/DevServer/src/Server/Startup.cs +++ b/src/Components/WebAssembly/DevServer/src/Server/Startup.cs @@ -35,18 +35,17 @@ public static void Configure(IApplicationBuilder app, IConfiguration configurati app.Use(async (ctx, next) => { - if (ctx.Request.Path.StartsWithSegments("/_framework") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.server.js") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.web.js")) + if (ctx.Request.Path.StartsWithSegments("/_framework/blazor.boot.json")) { ctx.Response.Headers.Append("Blazor-Environment", webHostEnvironment.EnvironmentName); - - if (applyCopHeaders) + } + else if (applyCopHeaders && ctx.Request.Path.StartsWithSegments("/_framework") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.server.js") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.web.js")) + { + var fileExtension = Path.GetExtension(ctx.Request.Path); + if (string.Equals(fileExtension, ".js", StringComparison.Ordinal)) { - var fileExtension = Path.GetExtension(ctx.Request.Path); - if (string.Equals(fileExtension, ".js", StringComparison.Ordinal)) - { - // Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer. - ApplyCrossOriginPolicyHeaders(ctx); - } + // Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer. + ApplyCrossOriginPolicyHeaders(ctx); } } diff --git a/src/Components/test/E2ETest/Tests/WebAssemblyConfigurationTest.cs b/src/Components/test/E2ETest/Tests/WebAssemblyConfigurationTest.cs index 5c298cb4c32e..b231545db2aa 100644 --- a/src/Components/test/E2ETest/Tests/WebAssemblyConfigurationTest.cs +++ b/src/Components/test/E2ETest/Tests/WebAssemblyConfigurationTest.cs @@ -37,11 +37,11 @@ public void WebAssemblyConfiguration_Works() // Verify values from the default 'appsettings.json' are read. Browser.Equal("Default key1-value", () => _appElement.FindElement(By.Id("key1")).Text); - // Verify that the dev server always correctly serves the 'Blazor-Environment: Development' header. - Browser.Equal("Development", () => _appElement.FindElement(By.Id("environment")).Text); - if (_serverFixture.TestTrimmedOrMultithreadingApps) { + // Verify that the environment gets detected as 'Production'. + Browser.Equal("Production", () => _appElement.FindElement(By.Id("environment")).Text); + // Verify values overriden by an environment specific 'appsettings.$(Environment).json are read Assert.Equal("Prod key2-value", _appElement.FindElement(By.Id("key2")).Text); @@ -50,6 +50,9 @@ public void WebAssemblyConfiguration_Works() } else { + // Verify that the dev server always correctly serves the 'Blazor-Environment: Development' header. + Browser.Equal("Development", () => _appElement.FindElement(By.Id("environment")).Text); + // Verify values overriden by an environment specific 'appsettings.$(Environment).json are read Assert.Equal("Development key2-value", _appElement.FindElement(By.Id("key2")).Text); From c1a5afbf562b843f8791a7a6ecaf6b8a31c5e5e1 Mon Sep 17 00:00:00 2001 From: Mackinnon Buck Date: Fri, 20 Sep 2024 09:12:42 -0700 Subject: [PATCH 3/3] Update src/Components/WebAssembly/DevServer/src/Server/Startup.cs --- src/Components/WebAssembly/DevServer/src/Server/Startup.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Components/WebAssembly/DevServer/src/Server/Startup.cs b/src/Components/WebAssembly/DevServer/src/Server/Startup.cs index b81891c478ec..046031a29f79 100644 --- a/src/Components/WebAssembly/DevServer/src/Server/Startup.cs +++ b/src/Components/WebAssembly/DevServer/src/Server/Startup.cs @@ -42,7 +42,7 @@ public static void Configure(IApplicationBuilder app, IConfiguration configurati else if (applyCopHeaders && ctx.Request.Path.StartsWithSegments("/_framework") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.server.js") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.web.js")) { var fileExtension = Path.GetExtension(ctx.Request.Path); - if (string.Equals(fileExtension, ".js", StringComparison.Ordinal)) + if (string.Equals(fileExtension, ".js", StringComparison.OrdinalIgnoreCase)) { // Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer. ApplyCrossOriginPolicyHeaders(ctx);