-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Reshape BrowserConfiguration API per review (BrowserOptions) while preserving the JS wire format #67337
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
Merged
Merged
Reshape BrowserConfiguration API per review (BrowserOptions) while preserving the JS wire format #67337
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
26 changes: 0 additions & 26 deletions
26
src/Components/Components/src/BrowserConfiguration/BrowserConfiguration.cs
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
src/Components/Components/src/BrowserConfiguration/SsrBrowserOptions.cs
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/Components/Endpoints/src/BrowserConfiguration/BrowserOptions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Microsoft.AspNetCore.Components; | ||
|
|
||
| /// <summary> | ||
| /// Options that flow from the server to the Blazor client in the browser | ||
| /// via a DOM comment. Only serializable options are included; callbacks stay | ||
| /// with JS initializers. | ||
| /// </summary> | ||
| public sealed class BrowserOptions | ||
| { | ||
| /// <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 server (circuit) specific options.</summary> | ||
| public InteractiveServerBrowserOptions Server { get; } = new(); | ||
|
|
||
| /// <summary>Gets the SSR-specific options.</summary> | ||
| public SsrBrowserOptions Ssr { get; } = new(); | ||
| } | ||
22 changes: 12 additions & 10 deletions
22
...wserConfigurationHttpContextExtensions.cs → ...on/BrowserOptionsHttpContextExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,36 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.AspNetCore.Components; | ||
| using Microsoft.AspNetCore.Http; | ||
|
|
||
| namespace Microsoft.AspNetCore.Http; | ||
| namespace Microsoft.AspNetCore.Components; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods on <see cref="HttpContext"/> for accessing <see cref="BrowserConfiguration"/>. | ||
| /// Extension methods on <see cref="HttpContext"/> for accessing <see cref="BrowserOptions"/>. | ||
| /// </summary> | ||
| public static class BrowserConfigurationHttpContextExtensions | ||
| public static class BrowserOptionsHttpContextExtensions | ||
| { | ||
| private static readonly object Key = new(); | ||
|
|
||
| /// <summary> | ||
| /// Gets the <see cref="BrowserConfiguration"/> for the current request. | ||
| /// 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="BrowserConfiguration"/> for the current request.</returns> | ||
| public static BrowserConfiguration GetBrowserConfiguration(this HttpContext context) | ||
| /// <returns>The <see cref="BrowserOptions"/> for the current request.</returns> | ||
| public static BrowserOptions GetBrowserOptions(this HttpContext context) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(context); | ||
|
|
||
| if (!context.Items.TryGetValue(Key, out var result)) | ||
| { | ||
| // Seed from endpoint metadata if available | ||
| var metadataConfig = context.GetEndpoint()?.Metadata.GetMetadata<BrowserConfiguration>(); | ||
| var config = metadataConfig ?? new BrowserConfiguration(); | ||
| var metadataConfig = context.GetEndpoint()?.Metadata.GetMetadata<BrowserOptions>(); | ||
| var config = metadataConfig ?? new BrowserOptions(); | ||
| context.Items[Key] = config; | ||
| return config; | ||
| } | ||
|
|
||
| return (BrowserConfiguration)result!; | ||
| return (BrowserOptions)result!; | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
src/Components/Endpoints/src/BrowserConfiguration/BrowserOptionsJsonConverters.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // 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 System.Text.Json.Serialization; | ||
|
|
||
| namespace Microsoft.AspNetCore.Components; | ||
|
|
||
| // Serializes a nullable <see cref="TimeSpan"/> as whole milliseconds, matching the | ||
| // numeric shape expected by the Blazor JS runtime (e.g. retryIntervalMilliseconds). | ||
| internal sealed class TimeSpanMillisecondsJsonConverter : JsonConverter<TimeSpan?> | ||
| { | ||
| public override TimeSpan? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| => reader.TokenType is JsonTokenType.Null ? null : TimeSpan.FromMilliseconds(reader.GetDouble()); | ||
|
|
||
| public override void Write(Utf8JsonWriter writer, TimeSpan? value, JsonSerializerOptions options) | ||
| { | ||
| if (value is { } timeSpan) | ||
| { | ||
| writer.WriteNumberValue((long)timeSpan.TotalMilliseconds); | ||
| } | ||
| else | ||
| { | ||
| writer.WriteNullValue(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Serializes a positive nullable boolean (e.g. PreserveDom) as its negated JS form | ||
| // (e.g. disableDomPreservation), keeping the public API idiomatic while the wire | ||
| // stays aligned with the JS runtime. | ||
| internal sealed class NegatedBooleanJsonConverter : JsonConverter<bool?> | ||
| { | ||
| public override bool? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| => reader.TokenType is JsonTokenType.Null ? null : !reader.GetBoolean(); | ||
|
|
||
| public override void Write(Utf8JsonWriter writer, bool? value, JsonSerializerOptions options) | ||
| { | ||
| if (value is { } boolean) | ||
| { | ||
| writer.WriteBooleanValue(!boolean); | ||
| } | ||
| else | ||
| { | ||
| writer.WriteNullValue(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 10 additions & 6 deletions
16
...wserConfiguration/ServerBrowserOptions.cs → ...ration/InteractiveServerBrowserOptions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/Components/Endpoints/src/BrowserConfiguration/SsrBrowserOptions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // 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; | ||
|
|
||
| namespace Microsoft.AspNetCore.Components; | ||
|
|
||
| /// <summary> | ||
| /// Serializable subset of <c>SsrStartOptions</c>. | ||
| /// </summary> | ||
| public sealed class SsrBrowserOptions | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets a value indicating whether the DOM is preserved during enhanced navigation. | ||
| /// When <see langword="false"/>, DOM preservation is disabled. <see langword="null"/> leaves the value unset. | ||
| /// Maps to <c>SsrStartOptions.disableDomPreservation</c>. | ||
| /// </summary> | ||
| [JsonPropertyName("disableDomPreservation")] | ||
| [JsonConverter(typeof(NegatedBooleanJsonConverter))] | ||
| public bool? PreserveDom { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the timeout before an inactive circuit is disposed. | ||
| /// Maps to <c>SsrStartOptions.circuitInactivityTimeoutMs</c>. | ||
| /// </summary> | ||
| [JsonPropertyName("circuitInactivityTimeoutMs")] | ||
| [JsonConverter(typeof(TimeSpanMillisecondsJsonConverter))] | ||
| public TimeSpan? CircuitInactivityTimeout { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.