Repository navigation
Preserve FocusOnNavigate focus when interactive component replaces prerendered DOM - #70308
Conversation
…erendered DOM Co-authored-by: Copilot App <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
A deferred blur callback can clear the new focus selector and prevent focus restoration during interactive rendering, while the test lacks an activation gate to reliably catch focus-stealing regressions during that transition.
2 open issues
What changed in this PR
This PR aims to preserve Blazor’s FocusOnNavigate focus when interactive rendering replaces prerendered elements, while respecting user-initiated focus changes.
Changes:
- Tracks the focus selector and retries focus after DOM mutations.
- Adds an interactive focus test page and navigation link.
- Adds browser tests for focus restoration and preservation of user-selected focus.
| File | Description |
|---|---|
src/Components/Web.JS/src/Rendering/FocusOnNavigate.ts |
Adds focus tracking, mutation observation, and blur handling. |
src/Components/test/testassets/Components.TestServer/RazorComponents/Shared/FocusOnNavigateLayout.razor |
Links to the interactive test page. |
src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/FocusOnNavigate/InteractivelyRendered.razor |
Adds an interactive component and an alternative focus target. |
src/Components/test/E2ETest/ServerRenderingTests/FocusOnNavigateTest.cs |
Adds browser scenarios for focus across interactive rendering. |
Review effort: Balanced
Co-authored-by: Copilot App <[email protected]>
|
/review |
PureWeen
left a comment
There was a problem hiding this comment.
One focus-restoration edge case remains at this commit.
Human-directed and GitHub Copilot-generated.
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]>
|
/review |
PureWeen
left a comment
There was a problem hiding this comment.
One source-supported focus-restoration issue remains at this commit. The HTML focus-removal contract supports the event-ordering analysis. I did not run a browser reproduction.
Human-directed and GitHub Copilot-generated.
Co-authored-by: Copilot App <[email protected]>
|
/review |
There was a problem hiding this comment.
Source-only review of frozen head 73e0add0a221d425d835529a1fc3e3941ceae154.
One candidate is published inline as unresolved, not as a verified defect: frozen source establishes the prerender-clear call path, but the bundle does not establish the required browser custom-element callback timing or a binding contract for focus preservation through initial interactive takeover.
Guide completion
docs/CrossCuttingGuidance.md: complete for in-scope source checks; execution and security assessment excluded.docs/BlazorComponentsGuidance.md: complete for in-scope source checks; execution and security assessment excluded.
Existing feedback coverage: No frozen feedback is equivalent to the unresolved disconnectedCallback candidate. Earlier feedback addresses separate prior-iteration timing, selector-arming, and test-gating concerns; deduplication is not endorsement of those comments.
Unresolved: Whether DOM removal synchronously invokes this custom element's disconnectedCallback before batch restoration and leaves the body active, and whether frozen target contracts require this exact activation behavior.
Test boundary: No target code, browser workflow, CI, or tests were run. The appropriate permanent validation surface is the Components Selenium E2E suite, observing document.activeElement after the first interactive render batch.
Uncovered: No materially changed area lacked a routed guide.
Provenance and limitations: Reviewed only the trusted version-2 bundle for PR #70308: head 73e0add0a221d425d835529a1fc3e3941ceae154, merge base 85fbddcb8d83c3c417965ce0f1a5c8d360eaad27, base tip and guidance commit 7a97d0eec339067267fd362341e9d6503618a63d. Proof is source-only and limited to tracked Git bytes; symlinks, submodules, LFS targets, runtime behavior, and feedback after bundle preparation are not observable.
Generated by ASP.NET Core Pull Request Review for #70308 · copilot · gpt56 · 170.2 AIC · ⌖ 19.1 AIC · ⊞ 18.8K
Comment /review to run again
|
Copilot's local review panel found no issues to raise at this commit. Informational [DISCARDED / UNRESOLVED]: structural selectors during prerender replacement ( Posted for informational purposes so the author can confirm the verifier didn't discard or leave unresolved a real finding; this item does not block This records the local panel's result only; existing human feedback and the hosted review's separate unresolved diagnostic remain untouched. Human-directed and GitHub Copilot-generated. |
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]>
Resolve the BrowserRenderer.ts import conflict by keeping main's deferredValuePropname import and the FocusOnNavigate import. Co-authored-by: Copilot App <[email protected]>
|
/review |
There was a problem hiding this comment.
Source-only review of frozen head f5ab6d23b13b340092a5b56a8d8673e666fc2494.
One candidate is published inline as unresolved, not as a verified defect: frozen source establishes that initial focus uses document.querySelector order while hydration restoration uses descendant-before-parent logical traversal, but the assigned evidence does not establish the binding hydration-order contract or exact browser behavior required to verify the consumer-visible effect.
Validated new findings: One unresolved P2 advisory at src/Components/Web.JS/src/Rendering/FocusOnNavigate.ts:113; no verified findings or intent questions.
Guide completion: docs/CrossCuttingGuidance.md and docs/BlazorComponentsGuidance.md completed all in-scope source checks. Runtime execution, tests, CI, browser workflows, and samples were excluded by the source-only boundary; security assessment was excluded by assigned policy.
Existing feedback coverage: No frozen feedback is equivalent to the nested-selector-order candidate. Earlier threads concern prior implementations, later-batch behavior, and test gating; deduplication is not endorsement of those comments. Feedback posted after bundle preparation is not observable.
Unresolved: Whether a binding issue/API/base-tip contract requires hydration restoration to preserve document-order selector semantics, plus target-version DOM evidence for querySelector, Element.matches, and focus/activeElement behavior on the exact nested input.
Test boundary: No target code, browser workflow, CI, or tests were run. The permanent validation surface is the Components Selenium E2E suite with nested selector matches across first interactive activation.
Uncovered: No materially changed area lacked a routed guide.
Provenance and limitations: Proof is source-only from the trusted version-2 frozen bundle: merge base/base tip/guidance commit 60ef0101bf97cfccb49ede034b38659c15474d1b. It is limited to tracked Git bytes; symlink, submodule, and LFS targets, runtime behavior, and feedback after preparation are not observable.
Generated by ASP.NET Core Pull Request Review for #70308 · copilot · gpt56 · 144.7 AIC · ⌖ 12.5 AIC · ⊞ 18.8K
Comment /review to run again
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]>
|
/review |
|
/backport to release/11.0 |
|
Started backporting to |
|
✅ No documentation PR was created. Confidence that documentation is needed: 10% No docs needed: internal JS bug fix and E2E tests; no signals detected. Documentation surfaces considered
|


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 is the template setup in which a static router contains a page 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:FocusOnNavigate.tsrecords the element it focused, along with the selector and the<blazor-focus-on-navigate>element that requested it.BrowserRendererclears a root component's prerendered content on its first render, it captures that record if the element is still focused and belongs to the root being cleared.renderBatchapplies every component diff in the batch, focus moves to the first element in document order inside the same root that matches the selector (which is a default rule inFocusOnNavigate. If users add new element in hydration process before the previously focused on one, then I think it's reasonable to move focus to a new one). Restoring after the whole batch handles headings rendered by child components and avoids matching elements outside the replaced root.Focus is not restored when:
FocusOnNavigateelement has been removed or its selector has changed.Only replacements present in the first interactive render batch are handled. If the matching element is rendered in a later batch, for example after async
OnInitializedAsyncwork, focus remains ondocument.body.A new Selenium test,
FocusIsRestored_WhenInteractiveRenderReplacesFocusedPrerenderedElement, navigates to anInteractiveServerpage under the static router, waits for the interactive render, and asserts that the replacement heading is focused. It fails with restoration disabled and passes with this change.Fixes https://github.com/dotnet/aspnetcore-scenario-tests/issues/165