Repository navigation
[release/11.0] Preserve FocusOnNavigate focus when interactive component replaces prerendered DOM - #70537
Merged
Conversation
…erendered DOM Co-authored-by: Copilot App <[email protected]>
Co-authored-by: Copilot App <[email protected]>
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]>
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]>
kotlarmilos
approved these changes
Nov 12, 2026
dariatiurina
approved these changes
Nov 12, 2026
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. |
Member
|
/review |
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. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Backport of #70308 to release/11.0
/cc @dariatiurina
Preserve FocusOnNavigate focus when interactive component replaces prerendered DOM
Restore
FocusOnNavigatefocus after an interactive render replaces its prerendered target.Description
When
FocusOnNavigateis 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 todocument.body, andFocusOnNavigateis 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
FocusOnNavigateapplied across that replacement. Before the first interactive render clears a root's prerendered content, the renderer captures the elementFocusOnNavigatefocused 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 ruleFocusOnNavigateuses.Focus is not restored when the user has already moved focus, when enhanced navigation to a different page has started, or when the
FocusOnNavigateelement has been removed or its selector changed. If the matching element is only rendered in a later batch, for example after asyncOnInitializedAsyncwork, focus remains ondocument.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?
Risk
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 elementFocusOnNavigatefocused is still focused and inside that root; otherwise it does nothing.Verification
Packaging changes reviewed?