Skip to content

Add Navigator to Microsoft.AspNetCore.Components.Platform - #69968

Merged
ilonatommy merged 8 commits into
dotnet:mainfrom
jsakamotoIGJP:components-platform-navigator
Nov 10, 2026
Merged

ilonatommy merged 8 commits into
dotnet:mainfrom
jsakamotoIGJP:components-platform-navigator

Conversation

@jsakamotoIGJP

@jsakamotoIGJP jsakamotoIGJP commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor
  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Add Navigator to Microsoft.AspNetCore.Components.Platform

Description

This implements the API proposal #69967, the first part of the browser context family (#69872). The proposal explains the public API. This description covers the implementation.

How later families hang off Navigator

Navigator is the first object below Window that other families will hang off, such as Clipboard, Storage Manager, and Web Locks. For them, Navigator has an internal method.

internal T GetOrAddFeature<T>(Func<FeatureContext, T> factory) where T : class

It delegates to Window.GetOrAddFeature. So every projection of the scope stays in the one cache on Window, keeps a stable identity, and is disposed when the scope ends.

A Clipboard PR would then add only an extension property like this.

public static class NavigatorClipboardExtensions
{
    extension(Navigator navigator)
    {
        public Clipboard Clipboard
            => navigator.GetOrAddFeature(static context => new Clipboard(context.JSRuntime));
    }
}

Clipboard would acquire its own JS reference with the path "navigator.clipboard", not through the reference that Navigator holds. JS interop resolves dotted identifiers, so the first call is still one round trip, and a failed acquisition stays inside the family. A missing navigator.clipboard, such as on an insecure page, still becomes NotSupportedException, as in Cookie Store.

Nothing calls Navigator.GetOrAddFeature in this PR, so the unit tests call it directly. This is why the library now has InternalsVisibleTo for its test project.

Why FeatureContext

The factory receives the FeatureContext that Window owns since #69993, the same one that families on Window get. So a child family gets the parts that the whole scope shares, such as the ones events will add (#69922). Passing the Window itself would also work. But then a child family could reach everything on Window, such as other families and window listeners.

No closure on each access

Navigator.GetOrAddFeature passes the factory to Window.GetOrAddFeature as it is. So child families such as Clipboard use a static lambda and allocate nothing on each access.

Navigator itself needs the Window to delegate to, so a plain factory for it would capture window and allocate a closure on each read of Window.Navigator. To avoid this, Window gets an internal overload that passes an argument to the factory, in the style of ConcurrentDictionary.GetOrAdd.

internal T GetOrAddFeature<T, TArg>(Func<FeatureContext, TArg, T> factory, TArg factoryArgument) where T : class

Window.Navigator calls it with window as the argument and a static lambda. The existing overloads now go through the same code path.

ImmutableArray<string> for languages

GetLanguagesAsync returns ImmutableArray<string>. This 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 #69967, because it keeps the promise of FrozenArray that nobody changes the list. The lists that the merged families return will change to ImmutableArray<T> in a follow-up PR.

The method wraps the deserialized string[] with ImmutableCollectionsMarshal.AsImmutableArray, so it does not copy the array. Nothing else holds a reference to that array.

Tests

  • The E2E tests use a browser context with a non-default locale, user agent, and touch support, so each result has to come from the browser. They use the same result marker as the Cookie Store tests, so a step cannot pass on the text of the previous step.
  • The onLine test switches the browser offline and back. It runs in WebAssembly only, because going offline also cuts the Interactive Server circuit.
  • I checked trimming with a trimmed WebAssembly app that calls only GetLanguageAsync(). The other Navigator methods and the Web Storage types were removed.
  • I ran the unit tests and the E2E tests locally, both from the build output and from the output of dotnet publish.

Contributes to #69967
Contributes to #69872

Navigator is the first part of the browser context family. It also owns
later families such as Clipboard and Storage Manager, so it comes first.

- Add Navigator with the Baseline metadata attributes language,
  languages, onLine, cookieEnabled, userAgent, hardwareConcurrency, and
  maxTouchPoints, and the Window.Navigator extension property. It works
  like Storage. Window shares one instance, the JS reference is acquired
  on first use, and the DI scope disposes it.
- Add the internal Navigator.GetOrAddFeature and FeatureContext, so later
  families can hang off Navigator with a static factory. The projections
  stay in the Window cache. Reading Window.Navigator allocates a closure
  for now; this will be fixed for all families once events are settled.
- Add unit tests, the InternalsVisibleTo they need, and the Navigator
  types to PublicApiTest.
- Add demo pages and E2E tests for both render modes. A non-default
  locale, user agent, and touch support show that each value comes from
  the browser, and the WebAssembly test also reads onLine while offline.
@github-actions github-actions Bot added the area-blazor Includes: Blazor, Razor Components label Oct 22, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 22, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @jsakamotoIGJP. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@ilonatommy ilonatommy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming the design won't be changed, the implementation and coverage looks good. Let's first finish the discussion in #69967 (comment) before merging, though.

…avigator

# Conflicts:
#	src/Components/Platform/src/Microsoft.AspNetCore.Components.Platform.csproj
#	src/Components/Platform/src/PublicAPI.Unshipped.txt
#	src/Components/Platform/test/PublicApiTest.cs
# Conflicts:
#	src/Components/Platform/src/PublicAPI.Unshipped.txt
#	src/Components/Platform/test/PublicApiTest.cs
@jsakamotoIGJP
jsakamotoIGJP marked this pull request as ready for review November 10, 2026 04:18
@jsakamotoIGJP
jsakamotoIGJP requested a review from a team as a code owner November 10, 2026 04:18
Copilot AI balanced review requested due to automatic review settings November 10, 2026 04:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The review materials omit the frozen head/base identities and existing-feedback bundle required by repository guidance, preventing confirmation of independent review completeness.

No open issues 🎉

What changed in this PR

Adds typed access to Window.Navigator in Microsoft.AspNetCore.Components.Platform, implementing proposal #69967 as the first part of the browser context family. It also provides shared caching support for future Navigator-based APIs.

Changes:

  • Adds seven asynchronous browser metadata getters, including preferred languages as an ImmutableArray<string>.
  • Extends feature factories to accept an argument without capturing closures, while retaining scope-owned caching and disposal.
  • Adds unit tests, public API checks, and browser tests covering Interactive Server and WebAssembly, including online-state changes.
File Description
src/​Components/​Platform/​testassets/​PlatformTestApp/​Components/​Pages/​NavigatorServer.razor Hosts the Navigator demo in Interactive Server mode.
src/​Components/​Platform/​testassets/​PlatformTestApp.E2E.Tests/​Tests/​NavigatorTests.cs Tests browser metadata in both modes and online-state changes in WebAssembly.
src/​Components/​Platform/​testassets/​PlatformTestApp.Client/​Pages/​NavigatorWebAssembly.razor Hosts the Navigator demo in WebAssembly mode.
src/​Components/​Platform/​testassets/​PlatformTestApp.Client/​Components/​NavigatorDemo.razor Provides controls and completion markers for browser tests.
src/​Components/​Platform/​test/​PublicApiTest.cs Includes Navigator in asynchronous-method and disposal-surface checks.
src/​Components/​Platform/​test/​NavigatorTest.cs Covers property reads, caching, acquisition retries, disposal, and child features.
src/​Components/​Platform/​src/​WindowNavigatorExtensions.cs Exposes the cached Window.Navigator property.
src/​Components/​Platform/​src/​Window.cs Adds argument-bearing factories to avoid closure allocations.
src/​Components/​Platform/​src/​PublicAPI.Unshipped.txt Records the new Navigator public API.
src/​Components/​Platform/​src/​Navigator.cs Implements metadata getters, lazy reference acquisition, and scope-owned disposal.
src/​Components/​Platform/​src/​Microsoft.AspNetCore.Components.Platform.csproj Grants the unit-test assembly access to internal feature infrastructure.

Review effort: Balanced

@PureWeen

Copy link
Copy Markdown
Member

/review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Source-only review of frozen head b256c29147ad8a1bae545585aea1d19f3b6d3300 found one validated P2 disposal-race issue: a pending Navigator acquisition can complete after its scoped owner is disposed, allowing browser work to continue and leaving the late IJSObjectReference undisposed.

Both routed guides completed independently with gpt-5.6-sol, and the candidate passed the independent verifier. Existing frozen feedback did not already cover this issue. No other unresolved candidates remained; routine negative checks found the Window factory refactor and public API entries consistent. Public API approval remains human-owned.

The review used only the immutable bundle at head b256c29147ad8a1bae545585aea1d19f3b6d3300, merge base/base tip eeceb660128d3d00ea9b35effe4fad3fac3d42e3, with trusted guidance from commit eeceb660128d3d00ea9b35effe4fad3fac3d42e3. Tests, CI, browser workflows, samples, and target code were not executed; security assessment was excluded by repository guidance. Tracked Git bytes only were available, and feedback posted after bundle preparation was not observable.

Generated by ASP.NET Core Pull Request Review for #69968 · copilot · gpt56 · 155.1 AIC · ⌖ 12 AIC · ⊞ 18.8K
Comment /review to run again

@jsakamotoIGJP

Copy link
Copy Markdown
Contributor Author

@ilonatommy I merged main and changed GetLanguagesAsync to return ImmutableArray<string>, as you chose in #69967. I also marked the PR as ready for review. Could you take another look when you have time?

@ilonatommy
ilonatommy merged commit c7fc007 into dotnet:main Nov 10, 2026
32 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Documentation appears necessary, but a draft PR could not be created.

Confidence that documentation is needed: 80%

Drafted a docs section on the new Navigator API in the Blazor JS interop article, scoped to .NET 12.

Documentation surfaces considered

  • Conceptual article: Required — New public Navigator class and WindowNavigatorExtensions in PublicAPI.Unshipped.txt.
  • Migration guidance: Not required — Additive API; no upgrade action needed.
  • Breaking change: Not required — No removed or changed existing behavior.

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — b256c291 Deployed Nov 10, 2026 by ilonatommy via conclusion #258
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member feature-blazor-jsinterop This issue is related to JSInterop in Blazor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants