Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ilonatommy
Copy link
Member

@ilonatommy ilonatommy commented Oct 14, 2025

Do not prevent rendering for router that has OnNavigateAsync when navigation already started during 404 re-execution.

In #24225 we blocked rendering for router that has OnNavigateAsync that is typically used for lazy loading actions. The reason was that rendering was throwing when triggered before with lazy loading finished: #24211.

With rendering blocked, 404 re-execution mechanism is not able to render the contents of 404 page. We need to implement logic to detect when re-execution has occurred and allow rendering in that specific case.

Description

  • Prevents blocking rendering for routers with OnNavigateAsync in progress if Refresh is triggered by re-execution to a different component type.
  • Implements component-type detection: Router compares the component type that triggered the pending OnNavigateAsync with the current route's component type. If they differ (indicating 404 re-execution to an error page), rendering is allowed. If they match (same-page lazy loading), rendering remains blocked.
  • Added test for the issue: BrowserNavigationToNotExistingPath_ReExecutesTo404.
  • Added comprehensive test for Don't render route component if OnNavigateAsync task in-progress #24225 that validates rendering waits for OnNavigateAsync completion during lazy loading: BrowserNavigationToLazyLoadedRoute_WaitsForOnNavigateAsyncGuard.

Fixes #63933

Possible Edge Cases Discussion

  1. Multiple OnNavigateAsync handlers on same component (lazy-loading):
    E.g. when navigating from /products/1 to /products/2 (both render ProductsPage). Typically the assembly required for rendering is already loaded, so no risk of Lazy loading might encounter issues if host page rerenders during loading #24211 (exceptions during rendering because lazy loaded assembly is missing).

  2. Re-execution to same component type:
    Real-world re-execution always targets dedicated error/404 components (e.g., /invalid-route re-executes to /not-found with NotFoundPage component). Same-component re-execution would require anti-pattern design (same component having @page "/users/{id}" and @page "/error" at the same time). In case of using this anti-pattern, the rendering would still be blocked.

  3. Null RouteData during re-execution:
    IRoutingStateProvider only provides RouteData during endpoint/SSR rendering. After a page becomes interactive (Server Interactive via SignalR or WebAssembly), subsequent navigations are handled client-side by the Router component without going through the endpoint middleware. In these client-side navigation scenarios, RoutingStateProvider is null, which correctly falls back to the original blocking behavior.

  4. Query/hash parameter changes (same route): Navigating from /products?page=1 to /products?page=2 renders the same component (ProductsPage). I was always blocked during pending OnNavigateAsync. Since providerRouteData.PageType == _previousOnNavigateTaskPageType, allowRenderDuringPendingNavigation is false, maintaining the original blocking behavior.

@ilonatommy ilonatommy self-assigned this Oct 14, 2025
@ilonatommy ilonatommy added the area-blazor Includes: Blazor, Razor Components label Oct 14, 2025
@ilonatommy ilonatommy marked this pull request as ready for review October 15, 2025 10:08
@ilonatommy ilonatommy requested a review from a team as a code owner October 15, 2025 10:08
@ilonatommy ilonatommy requested review from Copilot and removed request for a team October 15, 2025 10:08
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an issue where Blazor 404 re-execution was not working properly when the Router component had an OnNavigateAsync handler. The fix allows rendering to proceed during 404 re-execution scenarios while still preventing it during client-side navigation when lazy loading is in progress.

Key changes:

  • Modified the Router component to differentiate between server-side rendering (SSR) scenarios and client-side navigation
  • Updated the navigation blocking logic to check if route data is available from SSR before blocking rendering
  • Added comprehensive test coverage for the 404 re-execution scenario with OnNavigateAsync

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/Components/Components/src/Routing/Router.cs Updated navigation logic to allow rendering during SSR/re-execution while maintaining blocking for client navigation
src/Components/test/testassets/Components.TestServer/RazorComponents/App.razor Added test infrastructure with configurable OnNavigateAsync handler and query parameter support
src/Components/test/E2ETest/ServerRenderingTests/NoInteractivityTest.cs Added end-to-end test to verify 404 re-execution works with OnNavigateAsync

@ilonatommy
Copy link
Member Author

Failures of UserStoreTest are not connected, they happen across different PRs and should be quarantined.

@ilonatommy ilonatommy added the * NO MERGE * Do not merge this PR as long as this label is present. label Oct 21, 2025
@ilonatommy ilonatommy removed the * NO MERGE * Do not merge this PR as long as this label is present. label Oct 22, 2025
@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Oct 30, 2025
@ilonatommy ilonatommy requested a review from oroztocil November 10, 2025 11:52
Copy link
Member

@oroztocil oroztocil left a comment

Choose a reason for hiding this comment

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

Looks good 👍

@ilonatommy
Copy link
Member Author

@javiercn's review: use the information if we are interactive or not instead of putting a flag into route data.

@ilonatommy ilonatommy marked this pull request as draft November 14, 2025 14:29
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 pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Blazor Router NotFoundPage incompatible with OnNavigateAsync

2 participants