Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/9.0-rc2] [Blazor] Update WebAssembly.DevServer to serve the Blazor-Environment header #57974

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.Ordinal))
MackinnonBuck marked this conversation as resolved.
Show resolved Hide resolved
{
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
Loading