Description
Background and Motivation
Microsoft.AspNetCore.Components.Platform (#69304, implemented in #69305) gives Blazor applications typed access to standard browser APIs. It has Web Storage, the Cookie Store API (#69844), and URL (#69924) today. This proposal adds window.navigator, as the first step of the browser context family (#69872).
The navigator matters for two reasons.
- Its own attributes describe the browser, such as the preferred languages and the online state. Applications read them to adapt their behavior. Today they need
IJSRuntimefor that. - Many later families live under it, such as
navigator.clipboard(Window.Navigator.Clipboard),navigator.storage,navigator.locks, andnavigator.credentials. SoNavigatorhas to exist before those families.
This proposal covers the Baseline metadata that #69872 lists. These are language, languages, onLine, cookieEnabled, userAgent, hardwareConcurrency, and maxTouchPoints. Screen is a separate proposal (#69969), and the other parts of #69872 will be proposed separately.
The implementation is in #69968.
Usage Examples
builder.Services.AddBrowserPlatform();@using System.Collections.Immutable
@using Microsoft.AspNetCore.Components.Platform
@inject IBrowserPlatform Browser
<p>Languages: @string.Join(", ", languages)</p>
<p>Online: @online</p>
@code {
private ImmutableArray<string> languages = [];
private bool online;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
languages = await Browser.Window.Navigator.GetLanguagesAsync();
online = await Browser.Window.Navigator.GetOnLineAsync();
StateHasChanged();
}
}
}The API works in Interactive Server and WebAssembly.
Proposed API
namespace Microsoft.AspNetCore.Components.Platform;
+public sealed class Navigator
+{
+ public ValueTask<string> GetLanguageAsync();
+ public ValueTask<ImmutableArray<string>> GetLanguagesAsync();
+ public ValueTask<bool> GetOnLineAsync();
+ public ValueTask<bool> GetCookieEnabledAsync();
+ public ValueTask<string> GetUserAgentAsync();
+ public ValueTask<ulong> GetHardwareConcurrencyAsync();
+ public ValueTask<int> GetMaxTouchPointsAsync();
+}
+
+public static class WindowNavigatorExtensions
+{
+ extension(Window window)
+ {
+ public Navigator Navigator { get; }
+ }
+}The names and types follow the Web IDL of HTML and Pointer Events (maxTouchPoints). unsigned long long becomes ulong, long becomes int, and FrozenArray<DOMString> becomes ImmutableArray<string>.
ImmutableArray<string> is different from the repo's API conventions (.github/skills/review-public-api/references/conventions.md), which prefer IReadOnlyList<T> for returned lists. It was chosen in this comment on this issue, because it keeps the promise of FrozenArray that nobody changes the list. The other lists that the package returns will move to ImmutableArray<T> too.
Navigator works like Storage. Every caller gets the same instance from Window.Navigator, and the DI scope disposes it.
window.navigator exists in every browser and on every page, so these methods do not throw NotSupportedException.
Internally, Navigator can also create and cache the projections of later families, so each of them only adds an extension property to Navigator. Reading Window.Navigator and the later families under it allocates nothing on each access. This is internal and does not change the public API.
Alternative Designs
Applications can keep using IJSRuntime, such as JS.GetValueAsync<string>("navigator.language").
Navigator could read all attributes in one call and return a snapshot object. That would save round trips in Interactive Server. But a snapshot is not the browser API, and it would go stale for values that change, such as onLine.
Risks
All seven attributes are Baseline. cookieEnabled has been Baseline (newly available) since September 2024, when Safari 18 added it. The others have been Baseline widely available for years.
The online, offline, and languagechange events fire on window, not on navigator. They are not part of this proposal. They will use the events API from #69922 and will be proposed separately.
Like Web Storage, the API is unavailable before interactivity.
Source Justifications
- Applications read browser metadata to adapt their behavior: "Expose browser environment information needed by applications to adapt their behavior." (Add browser context and capability APIs #69872)
- The navigator is part of the browser context family: "
Browser.Navigator" (Add browser context and capability APIs #69872) - The seven attributes are the Navigator metadata of Add browser context and capability APIs #69872: "Language and languages", "Online state", "Cookie support", "User agent", "Hardware concurrency", "Maximum touch points" (Add browser context and capability APIs #69872)
- Applications reach
NavigatorfromWindow: "WindowNavigatorExtensions.extension(Window!).Navigator.get -> Navigator!" (PublicAPI.Unshipped.txt, namespaces left out) unsigned long longbecomesulong: "readonly attribute unsigned long long hardwareConcurrency;" (html.idl), "GetValueAsync<ulong>("hardwareConcurrency")" (Navigator.cs)FrozenArray<DOMString>becomesImmutableArray<string>, notIReadOnlyList<string>: "the gain from getting the representation closer to the original type is worth introducing a new pattern and having to post a few updates to the already merged APIs." (#69967 comment)- Later families get an internal way to hang off
Navigator: "internal T GetOrAddFeature<T>(Func<FeatureContext, T> factory) where T : class" (Navigator.cs) - Reading
Window.Navigatorallocates no closure: "window.GetOrAddFeature(static (context, window) => new Navigator(context.JSRuntime, window), window)" (WindowNavigatorExtensions.cs) - Neither do later families under
Navigator: "return _window.GetOrAddFeature(factory);" (Navigator.cs) - The API works in Interactive Server and WebAssembly: "
NavigatorPaths = ["/navigator-server", "/navigator-wasm"]" (NavigatorTests.cs) cookieEnabledhas been Baseline newly available since September 2024: ""baseline": "low"", ""baseline_low_date": "2024-09-16"" (cookie-enabledin web-features)
Activity
ilonatommy commentedon Nov 3, 2026
It made me wonder what with the other baseline APIs from https://developer.mozilla.org/en-US/docs/Web/API/Navigator#browser_compatibility. I suspect we missed them in the original doc by mistake, they aren't DOM manipulation APIs and the design said in the goals:
Using Chrome's public usage counter I tried to check the popularity of remaining 13 APIs (that are not added into this design issue and are not postponed for later).
Since the APIs included in the design currently hit 39-91% of loads, we could consider
sendBeaconand as possible extension of the current design. However, that has a prerequisite of havingBlob,FormData,URLSearchParams(almost done),ArrayBuffer, typed arrays, andDataViewwrappers ready.Conclusion: the chosen set is correct.
ilonatommy commentedon Nov 3, 2026
Why not
ImmutableArray<string>? It feels closer to the JS's original type: they both prevent replacing, adding, or removing elements.jsakamotoIGJP commentedon Nov 5, 2026
@ilonatommy
Good point.
ImmutableArray<T>also tells the caller that the producer will never change the list, andIReadOnlyList<T>does not. So I agree it fits better.Actually, I picked
IReadOnlyList<T>only to follow existing Blazor APIs such asInputFileChangeEventArgs.GetMultipleFiles(). There was no deeper reason.However, I think there are two things to keep in mind before we switch.
ImmutableArray<T>today. This would be the first one.FrozenArray. That means changing merged code too, such asCookieStore.GetAllAsync()andUrlSearchParams.GetAllAsync(). I don't think it matters, but we should be aware of it.I support the change. With these points in mind, would you like to go with
ImmutableArray<T>or follow the existing public Blazor API customs?jsakamotoIGJP commentedon Nov 9, 2026
I updated the issue description.
ilonatommy commentedon Nov 9, 2026
The concerns are valid but I think the gain from getting the representation closer to the original type is worth introducing a new pattern and having to post a few updates to the already merged APIs.