Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace Microsoft.AspNetCore.Components;
Expand All @@ -12,18 +14,45 @@ namespace Microsoft.AspNetCore.Components;
/// </summary>
public sealed class BrowserOptions
{
private static readonly object Key = new();

/// <summary>
/// Gets or sets the log level for the Blazor JS runtime. Applies to all render modes.
/// Maps to <c>WebStartOptions.logLevel</c>.
/// </summary>
public LogLevel? LogLevel { get; set; }

/// <summary>Gets the WebAssembly-specific options.</summary>
public WebAssemblyBrowserOptions WebAssembly { get; } = new();
/// <summary>Gets the interactive WebAssembly-specific options.</summary>
[JsonPropertyName("webAssembly")]
public InteractiveWebAssemblyBrowserOptions InteractiveWebAssembly { get; } = new();

/// <summary>Gets the interactive server (circuit) specific options.</summary>
public InteractiveServerBrowserOptions Server { get; } = new();
[JsonPropertyName("server")]
public InteractiveServerBrowserOptions InteractiveServer { get; } = new();

/// <summary>Gets the static server (SSR) specific options.</summary>
[JsonPropertyName("ssr")]
public StaticServerBrowserOptions StaticServer { get; } = new();

/// <summary>
/// Gets the <see cref="BrowserOptions"/> for the current request.
/// If not already set, seeds from endpoint metadata or creates a new instance.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/>.</param>
/// <returns>The <see cref="BrowserOptions"/> for the current request.</returns>
public static BrowserOptions GetBrowserOptions(HttpContext context)
{
ArgumentNullException.ThrowIfNull(context);

if (!context.Items.TryGetValue(Key, out var result))
{
// Seed from endpoint metadata if available
var metadataOptions = context.GetEndpoint()?.Metadata.GetMetadata<BrowserOptions>();
var options = metadataOptions ?? new BrowserOptions();
context.Items[Key] = options;
return options;
}

/// <summary>Gets the SSR-specific options.</summary>
public SsrBrowserOptions Ssr { get; } = new();
return (BrowserOptions)result!;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ public sealed class ConfigureBrowser : IComponent
[Parameter, EditorRequired]
public BrowserOptions Options { get; set; } = default!;

/// <summary>
/// Gets or sets the <see cref="HttpContext"/> for the current request.
/// </summary>
[CascadingParameter]
public HttpContext? HttpContext { get; set; }
internal HttpContext? HttpContext { get; set; }

void IComponent.Attach(RenderHandle renderHandle)
{
Expand All @@ -38,7 +35,7 @@ Task IComponent.SetParametersAsync(ParameterView parameters)

if (HttpContext is not null)
{
var existing = HttpContext.GetBrowserOptions();
var existing = BrowserOptions.GetBrowserOptions(HttpContext);
MergeInto(existing, Options);
}

Expand All @@ -49,25 +46,25 @@ internal static void MergeInto(BrowserOptions target, BrowserOptions source)
{
target.LogLevel = source.LogLevel ?? target.LogLevel;

// WebAssembly
target.WebAssembly.EnvironmentName = source.WebAssembly.EnvironmentName ?? target.WebAssembly.EnvironmentName;
target.WebAssembly.ApplicationCulture = source.WebAssembly.ApplicationCulture ?? target.WebAssembly.ApplicationCulture;
foreach (var kvp in source.WebAssembly.EnvironmentVariables)
// Interactive WebAssembly
target.InteractiveWebAssembly.EnvironmentName = source.InteractiveWebAssembly.EnvironmentName ?? target.InteractiveWebAssembly.EnvironmentName;
target.InteractiveWebAssembly.ApplicationCulture = source.InteractiveWebAssembly.ApplicationCulture ?? target.InteractiveWebAssembly.ApplicationCulture;
foreach (var kvp in source.InteractiveWebAssembly.EnvironmentVariables)
{
target.WebAssembly.EnvironmentVariables[kvp.Key] = kvp.Value;
target.InteractiveWebAssembly.EnvironmentVariables[kvp.Key] = kvp.Value;
}

// Server
target.Server.ReconnectionMaxRetries = source.Server.ReconnectionMaxRetries ?? target.Server.ReconnectionMaxRetries;
target.Server.ReconnectionRetryInterval = source.Server.ReconnectionRetryInterval ?? target.Server.ReconnectionRetryInterval;
target.Server.ReconnectionDialogId = source.Server.ReconnectionDialogId ?? target.Server.ReconnectionDialogId;
foreach (var kvp in source.Server.Extensions)
// Interactive server
target.InteractiveServer.ReconnectionMaxRetries = source.InteractiveServer.ReconnectionMaxRetries ?? target.InteractiveServer.ReconnectionMaxRetries;
target.InteractiveServer.ReconnectionRetryInterval = source.InteractiveServer.ReconnectionRetryInterval ?? target.InteractiveServer.ReconnectionRetryInterval;
target.InteractiveServer.ReconnectionDialogId = source.InteractiveServer.ReconnectionDialogId ?? target.InteractiveServer.ReconnectionDialogId;
foreach (var kvp in source.InteractiveServer.Extensions)
{
target.Server.Extensions[kvp.Key] = kvp.Value;
target.InteractiveServer.Extensions[kvp.Key] = kvp.Value;
}

// SSR
target.Ssr.PreserveDom = source.Ssr.PreserveDom ?? target.Ssr.PreserveDom;
target.Ssr.CircuitInactivityTimeout = source.Ssr.CircuitInactivityTimeout ?? target.Ssr.CircuitInactivityTimeout;
// Static server (SSR)
target.StaticServer.PreserveDom = source.StaticServer.PreserveDom ?? target.StaticServer.PreserveDom;
target.StaticServer.CircuitInactivityTimeout = source.StaticServer.CircuitInactivityTimeout ?? target.StaticServer.CircuitInactivityTimeout;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Components;
/// Non-serializable options (<c>loadBootResource</c>, <c>configureRuntime</c>,
/// <c>initializers</c>) must use <c>Blazor.start()</c> or JS initializers.
/// </summary>
public sealed class WebAssemblyBrowserOptions
public sealed class InteractiveWebAssemblyBrowserOptions
{
/// <summary>
/// Gets or sets the hosting environment name (e.g., "Development", "Production").
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
namespace Microsoft.AspNetCore.Components;

/// <summary>
/// Serializable subset of <c>SsrStartOptions</c>.
/// Serializable subset of <c>SsrStartOptions</c> for the static server render mode.
/// </summary>
public sealed class SsrBrowserOptions
public sealed class StaticServerBrowserOptions
{
/// <summary>
/// Gets or sets a value indicating whether the DOM is preserved during enhanced navigation.
Expand Down
37 changes: 17 additions & 20 deletions src/Components/Endpoints/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,15 @@ Microsoft.AspNetCore.Components.ConfigureBrowser
Microsoft.AspNetCore.Components.ConfigureBrowser.ConfigureBrowser() -> void
Microsoft.AspNetCore.Components.ConfigureBrowser.Options.get -> Microsoft.AspNetCore.Components.BrowserOptions!
Microsoft.AspNetCore.Components.ConfigureBrowser.Options.set -> void
Microsoft.AspNetCore.Components.ConfigureBrowser.HttpContext.get -> Microsoft.AspNetCore.Http.HttpContext?
Microsoft.AspNetCore.Components.ConfigureBrowser.HttpContext.set -> void
Microsoft.AspNetCore.Components.BrowserOptionsHttpContextExtensions
static Microsoft.AspNetCore.Components.BrowserOptionsHttpContextExtensions.GetBrowserOptions(this Microsoft.AspNetCore.Http.HttpContext! context) -> Microsoft.AspNetCore.Components.BrowserOptions!
static Microsoft.AspNetCore.Builder.RazorComponentsEndpointConventionBuilderExtensions.WithBrowserOptions(this Microsoft.AspNetCore.Builder.RazorComponentsEndpointConventionBuilder! builder, System.Action<Microsoft.AspNetCore.Components.BrowserOptions!>! configureOptions) -> Microsoft.AspNetCore.Builder.RazorComponentsEndpointConventionBuilder!
Microsoft.AspNetCore.Components.BrowserOptions
Microsoft.AspNetCore.Components.BrowserOptions.BrowserOptions() -> void
Microsoft.AspNetCore.Components.BrowserOptions.LogLevel.get -> Microsoft.Extensions.Logging.LogLevel?
Microsoft.AspNetCore.Components.BrowserOptions.LogLevel.set -> void
Microsoft.AspNetCore.Components.BrowserOptions.Server.get -> Microsoft.AspNetCore.Components.InteractiveServerBrowserOptions!
Microsoft.AspNetCore.Components.BrowserOptions.Ssr.get -> Microsoft.AspNetCore.Components.SsrBrowserOptions!
Microsoft.AspNetCore.Components.BrowserOptions.WebAssembly.get -> Microsoft.AspNetCore.Components.WebAssemblyBrowserOptions!
Microsoft.AspNetCore.Components.BrowserOptions.InteractiveServer.get -> Microsoft.AspNetCore.Components.InteractiveServerBrowserOptions!
Microsoft.AspNetCore.Components.BrowserOptions.StaticServer.get -> Microsoft.AspNetCore.Components.StaticServerBrowserOptions!
Microsoft.AspNetCore.Components.BrowserOptions.InteractiveWebAssembly.get -> Microsoft.AspNetCore.Components.InteractiveWebAssemblyBrowserOptions!
static Microsoft.AspNetCore.Components.BrowserOptions.GetBrowserOptions(Microsoft.AspNetCore.Http.HttpContext! context) -> Microsoft.AspNetCore.Components.BrowserOptions!
Microsoft.AspNetCore.Components.InteractiveServerBrowserOptions
Microsoft.AspNetCore.Components.InteractiveServerBrowserOptions.InteractiveServerBrowserOptions() -> void
Microsoft.AspNetCore.Components.InteractiveServerBrowserOptions.ReconnectionDialogId.get -> string?
Expand All @@ -73,19 +70,19 @@ Microsoft.AspNetCore.Components.InteractiveServerBrowserOptions.ReconnectionMaxR
Microsoft.AspNetCore.Components.InteractiveServerBrowserOptions.ReconnectionRetryInterval.get -> System.TimeSpan?
Microsoft.AspNetCore.Components.InteractiveServerBrowserOptions.ReconnectionRetryInterval.set -> void
Microsoft.AspNetCore.Components.InteractiveServerBrowserOptions.Extensions.get -> System.Collections.Generic.IDictionary<string!, System.Text.Json.JsonElement>!
Microsoft.AspNetCore.Components.SsrBrowserOptions
Microsoft.AspNetCore.Components.SsrBrowserOptions.SsrBrowserOptions() -> void
Microsoft.AspNetCore.Components.SsrBrowserOptions.CircuitInactivityTimeout.get -> System.TimeSpan?
Microsoft.AspNetCore.Components.SsrBrowserOptions.CircuitInactivityTimeout.set -> void
Microsoft.AspNetCore.Components.SsrBrowserOptions.PreserveDom.get -> bool?
Microsoft.AspNetCore.Components.SsrBrowserOptions.PreserveDom.set -> void
Microsoft.AspNetCore.Components.WebAssemblyBrowserOptions
Microsoft.AspNetCore.Components.WebAssemblyBrowserOptions.WebAssemblyBrowserOptions() -> void
Microsoft.AspNetCore.Components.WebAssemblyBrowserOptions.ApplicationCulture.get -> string?
Microsoft.AspNetCore.Components.WebAssemblyBrowserOptions.ApplicationCulture.set -> void
Microsoft.AspNetCore.Components.WebAssemblyBrowserOptions.EnvironmentName.get -> string?
Microsoft.AspNetCore.Components.WebAssemblyBrowserOptions.EnvironmentName.set -> void
Microsoft.AspNetCore.Components.WebAssemblyBrowserOptions.EnvironmentVariables.get -> System.Collections.Generic.IDictionary<string!, string!>!
Microsoft.AspNetCore.Components.StaticServerBrowserOptions
Microsoft.AspNetCore.Components.StaticServerBrowserOptions.StaticServerBrowserOptions() -> void
Microsoft.AspNetCore.Components.StaticServerBrowserOptions.CircuitInactivityTimeout.get -> System.TimeSpan?
Microsoft.AspNetCore.Components.StaticServerBrowserOptions.CircuitInactivityTimeout.set -> void
Microsoft.AspNetCore.Components.StaticServerBrowserOptions.PreserveDom.get -> bool?
Microsoft.AspNetCore.Components.StaticServerBrowserOptions.PreserveDom.set -> void
Microsoft.AspNetCore.Components.InteractiveWebAssemblyBrowserOptions
Microsoft.AspNetCore.Components.InteractiveWebAssemblyBrowserOptions.InteractiveWebAssemblyBrowserOptions() -> void
Microsoft.AspNetCore.Components.InteractiveWebAssemblyBrowserOptions.ApplicationCulture.get -> string?
Microsoft.AspNetCore.Components.InteractiveWebAssemblyBrowserOptions.ApplicationCulture.set -> void
Microsoft.AspNetCore.Components.InteractiveWebAssemblyBrowserOptions.EnvironmentName.get -> string?
Microsoft.AspNetCore.Components.InteractiveWebAssemblyBrowserOptions.EnvironmentName.set -> void
Microsoft.AspNetCore.Components.InteractiveWebAssemblyBrowserOptions.EnvironmentVariables.get -> System.Collections.Generic.IDictionary<string!, string!>!
Microsoft.AspNetCore.Components.Forms.ClientValidationRule
Microsoft.AspNetCore.Components.Forms.ClientValidationRule.Name.get -> string!
Microsoft.AspNetCore.Components.Forms.ClientValidationRule.Parameters.get -> System.Collections.Generic.IReadOnlyDictionary<string!, string!>!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,20 +443,20 @@ private void EmitBrowserConfigurationOnce(TextWriter output)

_browserConfigurationEmitted = true;

var config = _httpContext.GetBrowserOptions();
var config = BrowserOptions.GetBrowserOptions(_httpContext);

// Apply framework defaults: environment name and tooling env vars
var hostEnvironment = _httpContext.RequestServices.GetRequiredService<IHostEnvironment>();
config.WebAssembly.EnvironmentName ??= hostEnvironment.EnvironmentName;
config.InteractiveWebAssembly.EnvironmentName ??= hostEnvironment.EnvironmentName;

if (s_dotnetModifiableAssemblies != null)
{
config.WebAssembly.EnvironmentVariables.TryAdd("DOTNET_MODIFIABLE_ASSEMBLIES", s_dotnetModifiableAssemblies);
config.InteractiveWebAssembly.EnvironmentVariables.TryAdd("DOTNET_MODIFIABLE_ASSEMBLIES", s_dotnetModifiableAssemblies);
}

if (s_aspnetcoreBrowserTools != null)
{
config.WebAssembly.EnvironmentVariables.TryAdd("__ASPNETCORE_BROWSER_TOOLS", s_aspnetcoreBrowserTools);
config.InteractiveWebAssembly.EnvironmentVariables.TryAdd("__ASPNETCORE_BROWSER_TOOLS", s_aspnetcoreBrowserTools);
}

var configJson = JsonSerializer.Serialize(config, BrowserOptionsJsonContext.Default.BrowserOptions);
Expand Down
137 changes: 137 additions & 0 deletions src/Components/Endpoints/test/BrowserOptionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace Microsoft.AspNetCore.Components.Endpoints;

public class BrowserOptionsTest
{
[Fact]
public void SubOptions_ArePreInitialized()
{
var options = new BrowserOptions();

Assert.NotNull(options.InteractiveServer);
Assert.NotNull(options.StaticServer);
Assert.NotNull(options.InteractiveWebAssembly);
}

[Fact]
public void Serialization_PreservesTopLevelWireNames()
{
var options = new BrowserOptions
{
LogLevel = LogLevel.Warning,
};
options.InteractiveServer.ReconnectionMaxRetries = 5;
options.StaticServer.PreserveDom = true;
options.InteractiveWebAssembly.EnvironmentName = "Staging";

using var document = SerializeToDocument(options);
var root = document.RootElement;

Assert.True(root.TryGetProperty("logLevel", out _));
Assert.True(root.TryGetProperty("webAssembly", out _));
Assert.True(root.TryGetProperty("server", out _));
Assert.True(root.TryGetProperty("ssr", out _));
}

[Fact]
public void Serialization_InteractiveServer_UsesMillisecondsForReconnectionRetryInterval()
{
var options = new BrowserOptions();
options.InteractiveServer.ReconnectionRetryInterval = TimeSpan.FromSeconds(1.5);

using var document = SerializeToDocument(options);
var server = document.RootElement.GetProperty("server");

Assert.Equal(1500, server.GetProperty("reconnectionRetryIntervalMilliseconds").GetInt32());
}

[Fact]
public void Serialization_StaticServer_UsesNegatedDisableDomPreservation()
{
var options = new BrowserOptions();
options.StaticServer.PreserveDom = true;
options.StaticServer.CircuitInactivityTimeout = TimeSpan.FromSeconds(2);

using var document = SerializeToDocument(options);
var ssr = document.RootElement.GetProperty("ssr");

Assert.False(ssr.GetProperty("disableDomPreservation").GetBoolean());
Assert.Equal(2000, ssr.GetProperty("circuitInactivityTimeoutMs").GetInt32());
}

[Fact]
public void Serialization_InteractiveServer_Extensions_AreWrittenAsFlatKeys()
{
var options = new BrowserOptions();
options.InteractiveServer.Extensions["autoPauseEnabled"] = JsonSerializer.SerializeToElement(true);

using var document = SerializeToDocument(options);
var server = document.RootElement.GetProperty("server");

Assert.True(server.GetProperty("autoPauseEnabled").GetBoolean());
}

[Fact]
public void GetBrowserOptions_ReturnsSameInstanceOnSubsequentCalls()
{
var context = new DefaultHttpContext();

var first = BrowserOptions.GetBrowserOptions(context);
var second = BrowserOptions.GetBrowserOptions(context);

Assert.Same(first, second);
}

[Fact]
public void GetBrowserOptions_SeedsFromEndpointMetadata()
{
var metadataOptions = new BrowserOptions();
var context = new DefaultHttpContext();
context.SetEndpoint(new Endpoint(
requestDelegate: null,
metadata: new EndpointMetadataCollection(metadataOptions),
displayName: "Test"));

var result = BrowserOptions.GetBrowserOptions(context);

Assert.Same(metadataOptions, result);
}

[Fact]
public void GetBrowserOptions_ThrowsForNullContext()
{
Assert.Throws<ArgumentNullException>(() => BrowserOptions.GetBrowserOptions(null));
}

[Fact]
public void MergeInto_OnlyOverridesSetValues()
{
var target = new BrowserOptions
{
LogLevel = LogLevel.Warning,
};
target.InteractiveServer.ReconnectionMaxRetries = 3;
target.InteractiveWebAssembly.EnvironmentName = "Production";

var source = new BrowserOptions();
source.InteractiveServer.ReconnectionMaxRetries = 10;

ConfigureBrowser.MergeInto(target, source);

Assert.Equal(LogLevel.Warning, target.LogLevel);
Assert.Equal(10, target.InteractiveServer.ReconnectionMaxRetries);
Assert.Equal("Production", target.InteractiveWebAssembly.EnvironmentName);
}

private static JsonDocument SerializeToDocument(BrowserOptions options)
{
var json = JsonSerializer.Serialize(options, BrowserOptionsJsonContext.Default.BrowserOptions);
return JsonDocument.Parse(json);
}
}
Loading
Loading