Skip to content

Commit

Permalink
Set Hot Reload controlling headers DOTNET-MODIFIABLE-ASSEMBLIES and A…
Browse files Browse the repository at this point in the history
…SPNETCORE-BROWSER-TOOLS regardless of the environment kind
  • Loading branch information
tmat committed Jun 22, 2023
1 parent 3926560 commit 78f118b
Showing 1 changed file with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;

Expand All @@ -20,6 +19,12 @@ namespace Microsoft.AspNetCore.Builder;
/// </summary>
public static class ComponentsWebAssemblyApplicationBuilderExtensions
{
private static readonly string? s_dotnetModifiableAssemblies = GetNonEmptyEnvironmentVariableValue("DOTNET_MODIFIABLE_ASSEMBLIES");
private static readonly string? s_aspnetcoreBrowserTools = GetNonEmptyEnvironmentVariableValue("__ASPNETCORE_BROWSER_TOOLS");

private static string? GetNonEmptyEnvironmentVariableValue(string name)
=> Environment.GetEnvironmentVariable(name) is { Length: >0 } value ? value : null;

/// <summary>
/// Configures the application to serve Blazor WebAssembly framework files from the path <paramref name="pathPrefix"/>. This path must correspond to a referenced Blazor WebAssembly application project.
/// </summary>
Expand All @@ -42,23 +47,19 @@ public static IApplicationBuilder UseBlazorFrameworkFiles(this IApplicationBuild
{
context.Response.Headers.Append("Blazor-Environment", webHostEnvironment.EnvironmentName);
if (webHostEnvironment.IsDevelopment())
// DOTNET_MODIFIABLE_ASSEMBLIES is used by the runtime to initialize hot-reload specific environment variables and is configured
// by the launching process (dotnet-watch / Visual Studio).
// Always add the header if the environment variable is set, regardless of the kind of environment.
if (s_dotnetModifiableAssemblies != null)
{
context.Response.Headers.Append("DOTNET-MODIFIABLE-ASSEMBLIES", s_dotnetModifiableAssemblies);
}
// See https://github.com/dotnet/aspnetcore/issues/37357#issuecomment-941237000
// Translate the _ASPNETCORE_BROWSER_TOOLS environment configured by the browser tools agent in to a HTTP response header.
if (s_aspnetcoreBrowserTools != null)
{
// DOTNET_MODIFIABLE_ASSEMBLIES is used by the runtime to initialize hot-reload specific environment variables and is configured
// by the launching process (dotnet-watch / Visual Studio).
// In Development, we'll transmit the environment variable to WebAssembly as a HTTP header. The bootstrapping code will read the header
// and configure it as env variable for the wasm app.
if (Environment.GetEnvironmentVariable("DOTNET_MODIFIABLE_ASSEMBLIES") is string dotnetModifiableAssemblies)
{
context.Response.Headers.Append("DOTNET-MODIFIABLE-ASSEMBLIES", dotnetModifiableAssemblies);
}
// See https://github.com/dotnet/aspnetcore/issues/37357#issuecomment-941237000
// Translate the _ASPNETCORE_BROWSER_TOOLS environment configured by the browser tools agent in to a HTTP response header.
if (Environment.GetEnvironmentVariable("__ASPNETCORE_BROWSER_TOOLS") is string blazorWasmHotReload)
{
context.Response.Headers.Append("ASPNETCORE-BROWSER-TOOLS", blazorWasmHotReload);
}
context.Response.Headers.Append("ASPNETCORE-BROWSER-TOOLS", s_aspnetcoreBrowserTools);
}
await next(context);
Expand Down

0 comments on commit 78f118b

Please sign in to comment.