Skip to content

Commit

Permalink
[release/9.0-rc2] [Blazor] Update WebAssembly.DevServer to serve th…
Browse files Browse the repository at this point in the history
…e `Blazor-Environment` header (#57974)

* Serve 'Blazor-Environment' header

* PR feedback

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

---------

Co-authored-by: Mackinnon Buck <mackinnon.buck@gmail.com>
  • Loading branch information
github-actions[bot] and MackinnonBuck committed Sep 20, 2024
1 parent a717e7f commit 8371724
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/Components/WebAssembly/DevServer/src/Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -29,27 +30,28 @@ public static void Configure(IApplicationBuilder app, IConfiguration configurati

app.UseWebAssemblyDebugging();

bool applyCopHeaders = configuration.GetValue<bool>("ApplyCopHeaders");
var webHostEnvironment = app.ApplicationServices.GetRequiredService<IWebHostEnvironment>();
var applyCopHeaders = configuration.GetValue<bool>("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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand Down

0 comments on commit 8371724

Please sign in to comment.