Skip to content

Commit 8371724

Browse files
[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 <[email protected]>
1 parent a717e7f commit 8371724

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

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

Lines changed: 16 additions & 14 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,28 @@ 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/blazor.boot.json"))
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+
else if (applyCopHeaders && ctx.Request.Path.StartsWithSegments("/_framework") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.server.js") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.web.js"))
43+
{
44+
var fileExtension = Path.GetExtension(ctx.Request.Path);
45+
if (string.Equals(fileExtension, ".js", StringComparison.OrdinalIgnoreCase))
3946
{
40-
string fileExtension = Path.GetExtension(ctx.Request.Path);
41-
if (string.Equals(fileExtension, ".js"))
42-
{
43-
// Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer.
44-
ApplyCrossOriginPolicyHeaders(ctx);
45-
}
47+
// Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer.
48+
ApplyCrossOriginPolicyHeaders(ctx);
4649
}
50+
}
4751

48-
await next(ctx);
49-
});
50-
}
52+
await next(ctx);
53+
});
5154

52-
//app.UseBlazorFrameworkFiles();
5355
app.UseRouting();
5456

5557
app.UseStaticFiles(new StaticFileOptions

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public void WebAssemblyConfiguration_Works()
3939

4040
if (_serverFixture.TestTrimmedOrMultithreadingApps)
4141
{
42+
// Verify that the environment gets detected as 'Production'.
43+
Browser.Equal("Production", () => _appElement.FindElement(By.Id("environment")).Text);
44+
4245
// Verify values overriden by an environment specific 'appsettings.$(Environment).json are read
4346
Assert.Equal("Prod key2-value", _appElement.FindElement(By.Id("key2")).Text);
4447

@@ -47,6 +50,9 @@ public void WebAssemblyConfiguration_Works()
4750
}
4851
else
4952
{
53+
// Verify that the dev server always correctly serves the 'Blazor-Environment: Development' header.
54+
Browser.Equal("Development", () => _appElement.FindElement(By.Id("environment")).Text);
55+
5056
// Verify values overriden by an environment specific 'appsettings.$(Environment).json are read
5157
Assert.Equal("Development key2-value", _appElement.FindElement(By.Id("key2")).Text);
5258

0 commit comments

Comments
 (0)