Skip to content

[Blazor] Router seems to be rendering twice #47058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
javiercn opened this issue Mar 6, 2023 · 2 comments · Fixed by #47880
Closed

[Blazor] Router seems to be rendering twice #47058

javiercn opened this issue Mar 6, 2023 · 2 comments · Fixed by #47880
Assignees
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. feature-blazor-routing Features related to routing in Blazor applications
Milestone

Comments

@javiercn
Copy link
Member

javiercn commented Mar 6, 2023

From what I can tell, when there is no handler passed to the router, it will render the component twice:
https://github.com/dotnet/aspnetcore/blob/main/src/Components/Components/src/Routing/Router.cs#L256-L259

Probably there should be an early return after the highlighted Refresh

@javiercn javiercn added area-blazor Includes: Blazor, Razor Components feature-blazor-routing Features related to routing in Blazor applications labels Mar 6, 2023
@mkArtakMSFT mkArtakMSFT added the bug This issue describes a behavior which is not expected - a bug. label Mar 7, 2023
@mkArtakMSFT mkArtakMSFT added this to the 8.0-preview3 milestone Mar 7, 2023
@MarvinKlein1508
Copy link

Could this possibly has something todo with all those errors we see in our Eventviewer?

Category: Microsoft.AspNetCore.Components.Server.Circuits.RemoteNavigationManager
EventId: 4
SpanId: f84e4ab5046d12a9
TraceId: f886dd67d7c1fa947063c8bd2e5152bc
ParentId: 0000000000000000
RequestId: 8000049b-0000-e400-b63f-84710c7967bb
RequestPath: /_blazor
TransportConnectionId: JNgCuYUT_el1KKsXUpt-cQ

Navigation failed when changing the location to https://besucherportal.scholzdom.local/Account/Login

Exception: 
System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, Object[] args)
   at Microsoft.JSInterop.JSRuntimeExtensions.InvokeVoidAsync(IJSRuntime jsRuntime, String identifier, Object[] args)
   at Microsoft.AspNetCore.Components.Server.Circuits.RemoteNavigationManager.<>c__DisplayClass13_0.<<NavigateToCore>g__PerformNavigationAsync|0>d.MoveNext()

We get those all the time when someone switches from a razor component page to a razor page in our app. For example: from Index.razor to /Account/Login.cshtml

@surayya-MS
Copy link
Member

surayya-MS commented Apr 25, 2023

@javiercn I checked this in BlazorServerApp and found out that Router renders the component only once. This "return" happens here and the comment explains it.

However when I was working on Unified app model routing issue I also noticed that Router renders the component twice. I checked same thing in BlazorUnitedApp in my other branch with unified app model and found out that it is true. This happens inside SetParametersAsync here because previously the component was already rendered once here. The fix is to put that Refresh inside else block.

The reason why this happened in BlazorUnitedApp and not in BlazorServerApp (turned out that it also happens but only once) is because BlazorUnitedApp renders App.razor every time and there is Router inside. Router.SetParametersAsync is called every time. Whereas in BlazorServerApp Router component is rendered once and SetParametersAsync is not called every time the page renders. Inerestingly in BlazorServerApp SetParametersAsync is called twice not once as in BlazorUnitedApp and the component was actually rendered 4 times on the start.

@surayya-MS surayya-MS modified the milestones: 8.0-preview4, 8.0-preview5 Apr 26, 2023
@ghost ghost locked as resolved and limited conversation to collaborators May 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. feature-blazor-routing Features related to routing in Blazor applications
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
@javiercn @MarvinKlein1508 @mkArtakMSFT @surayya-MS and others