From 8371724b2f349b5817558c005e831d0d00ca66ed Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 20 Sep 2024 12:14:26 -0700 Subject: [PATCH] [release/9.0-rc2] [Blazor] Update `WebAssembly.DevServer` to serve the `Blazor-Environment` header (#57974) * Serve 'Blazor-Environment' header * PR feedback * Update src/Components/WebAssembly/DevServer/src/Server/Startup.cs --------- Co-authored-by: Mackinnon Buck --- .../DevServer/src/Server/Startup.cs | 30 ++++++++++--------- .../Tests/WebAssemblyConfigurationTest.cs | 6 ++++ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/Components/WebAssembly/DevServer/src/Server/Startup.cs b/src/Components/WebAssembly/DevServer/src/Server/Startup.cs index 342370171656..046031a29f79 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,28 @@ 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/blazor.boot.json")) { - 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); + } + 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.OrdinalIgnoreCase)) { - string fileExtension = Path.GetExtension(ctx.Request.Path); - if (string.Equals(fileExtension, ".js")) - { - // 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); } + } - 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..b231545db2aa 100644 --- a/src/Components/test/E2ETest/Tests/WebAssemblyConfigurationTest.cs +++ b/src/Components/test/E2ETest/Tests/WebAssemblyConfigurationTest.cs @@ -39,6 +39,9 @@ public void WebAssemblyConfiguration_Works() 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); @@ -47,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);