Skip to content

[release/11.0] Preserve FocusOnNavigate focus when interactive component replaces prerendered DOM - #70537

Merged
wtgodbe merged 6 commits into
release/11.0from
backport/pr-70308-to-release/11.0
Nov 13, 2026
Merged

wtgodbe merged 6 commits into
release/11.0from
backport/pr-70308-to-release/11.0

Conversation

@github-actions

@github-actions github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Backport of #70308 to release/11.0

/cc @dariatiurina

Preserve FocusOnNavigate focus when interactive component replaces prerendered DOM

Restore FocusOnNavigate focus after an interactive render replaces its prerendered target.

Description

When FocusOnNavigate is rendered statically and focuses an element inside a prerendered interactive component, the first interactive render of that component clears the prerendered DOM. The focused element is removed, the browser moves focus to document.body, and FocusOnNavigate is not notified. This happens in the Blazor Web App template setup, where a static router contains pages with @rendermode InteractiveServer: after enhanced navigation to Counter, its <h1> is focused and then loses focus.

This change keeps the focus that FocusOnNavigate applied across that replacement. Before the first interactive render clears a root's prerendered content, the renderer captures the element FocusOnNavigate focused if it is still focused and inside that root. After the whole render batch is applied, focus moves to the first element in document order that matches the selector inside the same root, which is the same rule FocusOnNavigate uses.

Focus is not restored when the user has already moved focus, when enhanced navigation to a different page has started, or when the FocusOnNavigate element has been removed or its selector changed. If the matching element is only rendered in a later batch, for example after async OnInitializedAsync work, focus remains on document.body.

Fixes https://github.com/dotnet/aspnetcore-scenario-tests/issues/165

Customer Impact

In apps created from the Blazor Web App template with per-page interactivity, keyboard and screen-reader users are not moved to the destination page heading after navigating to an interactive page such as Counter. Focus falls to body, so screen readers do not announce the new page. The issue was reported by the automated accessibility scenario tests for the Blazor Server, Blazor Server with Identity, and Blazor WebAssembly templates. There is no clean workaround in the templates; apps would need custom JS to re-apply focus after interactive startup.

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

The change is limited to Blazor's JavaScript (FocusOnNavigate.ts, BrowserRenderer.ts, Renderer.ts). It runs only on a root component's first interactive render, and only when the element FocusOnNavigate focused is still focused and inside that root; otherwise it does nothing.

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

dariatiurina and others added 6 commits November 12, 2026 13:03
Arm the selector only when navigation focus lands, and restore it from the first interactive root render instead of a document MutationObserver. The user-focus test holds streaming open so hydration stays blocked until focus has moved.

Co-authored-by: Copilot App <[email protected]>
Capture the element FocusOnNavigate focused before the first interactive
render clears its prerendered root, and restore focus to the matching
replacement inside that root after the whole render batch is applied.
Restoration is skipped when the user moved focus, when the tracked element
is not in the replaced root, and when enhanced navigation to another page
has started.

Add a Selenium test that navigates to an interactive Server page under a
static router and asserts that its heading keeps focus after the
interactive render replaces it.

Co-authored-by: Copilot App <[email protected]>
Choose the replacement the same way FocusOnNavigate does: the first
document.querySelectorAll match, limited to the replaced interactive root.
The previous logical-tree traversal visited descendants before their
parents, so nested matches restored focus to the inner element.

Check membership through DOM ancestry. Static Razor markup is inserted as
a block whose descendants are not logical children, so a target nested in
plain markup (for example <div><h1>...</h1></div>) was not found and focus
stayed on the body.

Co-authored-by: Copilot App <[email protected]>
@github-actions
github-actions Bot requested a review from a team as a code owner November 12, 2026 13:03
@dariatiurina dariatiurina self-assigned this Nov 12, 2026
@kotlarmilos
kotlarmilos self-requested a review November 12, 2026 13:08
@dariatiurina dariatiurina added the Servicing-consider Shiproom approval is required for the issue label Nov 12, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hi @github-actions[bot]. Please make sure you've updated the PR description to use the Shiproom Template. Also, make sure this PR is not marked as a draft and is ready-to-merge.

To learn more about how to prepare a servicing PR click here.

@PureWeen

Copy link
Copy Markdown
Member

/review

@dariatiurina dariatiurina added Servicing-approved Shiproom has approved the issue and removed Servicing-consider Shiproom approval is required for the issue labels Nov 13, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hi @github-actions[bot]. This PR was just approved to be included in the upcoming servicing release. Somebody from the @dotnet/aspnet-build team will get it merged when the branches are open. Until then, please make sure all the CI checks pass and the PR is reviewed.

@wtgodbe
wtgodbe merged commit e3af7ed into release/11.0 Nov 13, 2026
45 checks passed
@wtgodbe
wtgodbe deleted the backport/pr-70308-to-release/11.0 branch November 13, 2026 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Servicing-approved Shiproom has approved the issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants