Skip to content

[API Proposal] Add the Navigator to the typed browser platform APIs for Blazor #69967

Description

@jsakamotoIGJP

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 IJSRuntime for that.
  • Many later families live under it, such as navigator.clipboard (Window.Navigator.Clipboard), navigator.storage, navigator.locks, and navigator.credentials. So Navigator has 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 Navigator from Window: "WindowNavigatorExtensions.extension(Window!).Navigator.get -> Navigator!" (PublicAPI.Unshipped.txt, namespaces left out)
  • unsigned long long becomes ulong: "readonly attribute unsigned long long hardwareConcurrency;" (html.idl), "GetValueAsync<ulong>("hardwareConcurrency")" (Navigator.cs)
  • FrozenArray<DOMString> becomes ImmutableArray<string>, not IReadOnlyList<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.Navigator allocates 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)
  • cookieEnabled has been Baseline newly available since September 2024: ""baseline": "low"", ""baseline_low_date": "2024-09-16"" (cookie-enabled in web-features)

Activity

  1. added
    area-blazor
    api-suggestion
    on Oct 22, 2026
  2. added this to the .NET 12 Planning milestone on Oct 22, 2026
  3. ilonatommy commented on Nov 3, 2026

    @ilonatommy
    Member

    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:

    “Popularity determines scenario order, documentation emphasis, and test depth; it does not permit an eligible lower-popularity API to disappear without an explicit exclusion.”

    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).

    API What the counter measures Page loads
    sendBeacon() Calling sendBeacon 54.88%
    permissions Calling permissions.query() 21.24%
    userActivation Reading hasBeenActive 19.15%
    serviceWorker Page controlled by a service worker—not a direct Navigator call 18.84%
    mediaDevices Calling enumerateDevices() 14.62%
    pdfViewerEnabled Reading the property 8.29%
    mediaCapabilities Calling decodingInfo() 6.65%
    mediaSession Calling setActionHandler() 3.31%
    geolocation Calling getCurrentPosition() 2.50%
    getGamepads() Calling the method 2.09%
    wakeLock Requesting a screen wake lock 1.47%

    Since the APIs included in the design currently hit 39-91% of loads, we could consider sendBeacon and as possible extension of the current design. However, that has a prerequisite of having Blob, FormData, URLSearchParams (almost done), ArrayBuffer, typed arrays, and DataView wrappers ready.

    Conclusion: the chosen set is correct.

  4. ilonatommy commented on Nov 3, 2026

    @ilonatommy
    Member

    FrozenArray becomes IReadOnlyList.

    Why not ImmutableArray<string>? It feels closer to the JS's original type: they both prevent replacing, adding, or removing elements.

  5. jsakamotoIGJP commented on Nov 5, 2026

    @jsakamotoIGJP
    ContributorAuthor

    @ilonatommy

    Why not ImmutableArray<string>? It feels closer to the JS's original type: they both prevent replacing, adding, or removing elements.

    Good point. ImmutableArray<T> also tells the caller that the producer will never change the list, and IReadOnlyList<T> does not. So I agree it fits better.

    Actually, I picked IReadOnlyList<T> only to follow existing Blazor APIs such as InputFileChangeEventArgs.GetMultipleFiles(). There was no deeper reason.

    However, I think there are two things to keep in mind before we switch.

    • If I'm not mistaken, no public API in Blazor returns ImmutableArray<T> today. This would be the first one.
    • To stay consistent, we should use it for every list the package returns, not only for FrozenArray. That means changing merged code too, such as CookieStore.GetAllAsync() and UrlSearchParams.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?

  6. jsakamotoIGJP commented on Nov 9, 2026

    @jsakamotoIGJP
    ContributorAuthor

    I updated the issue description.

  7. ilonatommy commented on Nov 9, 2026

    @ilonatommy
    Member

    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?

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-proposalapi-suggestionarea-blazor

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions