Skip to content

Fix @onclick:preventDefault not being respected by Blazor enhanced navigation - #70289

Closed
Yuvan111 wants to merge 1 commit into
dotnet:mainfrom
BrundhaVelusamy:52514_preventDefaultNavigation
Closed

Yuvan111 wants to merge 1 commit into
dotnet:mainfrom
BrundhaVelusamy:52514_preventDefaultNavigation

Conversation

@Yuvan111

@Yuvan111 Yuvan111 commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Fix @OnClick:preventDefault not being respected by Blazor enhanced navigation

Description

This PR fixes an issue where Blazor enhanced navigation can navigate or update browser history despite an interactive link specifying @OnClick:preventDefault.

The change ensures enhanced navigation processes clicks after interactive renderers have had an opportunity to cancel the default action. This allows prevented clicks to invoke their callbacks without navigating, while preserving enhanced navigation for ordinary links and static server-rendered pages.

Root cause

Enhanced navigation registers its document-level click listener before interactive renderers. It can therefore update browser history before Blazor processes @OnClick:preventDefault. Cancelling the browser’s default action afterward does not undo that navigation.

Solution

  • Reorder the enhanced-navigation click listener after renderer click listeners whenever they are registered or re-registered.
  • Defer reordering during click dispatch so the current click still reaches enhanced navigation.
  • Preserve document-level handling for static SSR and navigation after renderer disposal, without introducing public APIs.

Before Fix

52514_BeforeFix.mp4

After Fix

52514_AfterFix.mp4

Test coverage

  • Prevented clicks invoke their callbacks without changing the URL across Server, WebAssembly, Auto, and mixed render modes, with and without prerendering.
  • Additional renderer startup, cold/warm Auto activation, and click-listener re-registration.
  • Enhanced navigation after circuit disposal, with document-level stopPropagation(), and during reentrant listener registration.
  • Ordinary links continue to navigate without a full reload.

Fixes #52514

@github-actions github-actions Bot added the area-blazor Includes: Blazor, Razor Components label Nov 4, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 4, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

}
}

var firstId = ids[0];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Additional coverage gaps
toggles prevention off and immediately back on—it never clicks while prevention is false. The prevented-click assertions also check URL and callback count, but not history mutations when the URL remains unchanged.


function onClickListenerRegistered(isDispatchingClick: boolean) {
// Removing a listener during dispatch skips it for that click. Let it run before reordering.
if (isDispatchingClick) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nested clicks can bypass enhanced navigation

If a synchronous interactive click re-registers a renderer’s click listener, then triggers another element’s .click(), the inner navigation listener clears the pending reorder. This removes the navigation listener from the outer event’s dispatch snapshot. The outer link then performs a full page load, losing enhanced-navigation behavior.

This reproduced 10/10 times in Chromium using the PR’s event-delegation and navigation-listener code. Controls without the nested click retained interception. Managed interop and enhanced-page fetching were stubbed, so this is DOM-level evidence, not full Blazor E2E verification.

Reordering should wait until no outer managed click remains active. Add a Selenium regression covering this sequence.

@Yuvan111

Yuvan111 commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

Closed, follow this PR: #70299

@Yuvan111 Yuvan111 closed this Nov 4, 2026

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 69bd7e99 Deployed Nov 4, 2026 by dotnet-policy-service[bot] via pat_pool #2151
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Blazor] preventDefault does not function in Auto render mode, even if the page is marked with InteractiveWebAssembly

3 participants