Skip to content
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

[Blazor] Cascading RouteData in Layout causes OnInitializedAsync to be called twice when navigating to another Layout #20637

Closed
Charnock opened this issue Apr 8, 2020 · 8 comments
Labels
affected-few This issue impacts only small number of customers area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. feature-blazor-builtin-components Features related to the built in components we ship or could ship in the future Pillar: Technical Debt Priority:1 Work that is critical for the release, but we could probably ship without severity-minor This label is used by an internal tool

Comments

@Charnock
Copy link

Charnock commented Apr 8, 2020

Describe the bug

When navigating between pages with two different layouts, if the page you're navigating from has a CascadingParameter for RouteData, OnInitializedAsync will be called twice on the page you're navigating to.

To Reproduce

I've included a minimal repro here. I've added logging to OnInitializedAsync of the Index, Counter, and Fetch Data pages.

  • Start the project and navigate from the Index page to the Counter page.
  • Then navigate from Counter to either the Index or Fetch Data
  • Observe that OnInitializedAsync is called twice for Index or Fetch Data

This behavior does not occur when Navigating between Index and Fetch Data.

Additionally, I've commented out a second CascadingValue in App.razor which cascades a string. Uncommenting that CascadingValue appears to resolve the issue, even when that value isn't being used on any of the pages. Also, only using the cascading string doesn't appear to reproduce the issue.

Further technical details

.NET Core SDK (reflecting any global.json):
Version: 3.1.300-preview-015048
Commit: 13f19b4682

Runtime Environment:
OS Name: Windows
OS Version: 10.0.18362
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\3.1.300-preview-015048\

Host (useful for support):
Version: 3.1.3
Commit: 4a9f85e9f8

Microsoft Visual Studio Community 2019
Version 16.5.3

@javiercn javiercn added the area-blazor Includes: Blazor, Razor Components label Apr 8, 2020
@javiercn javiercn changed the title [Blazor WASM] Cascading RouteData in Layout causes OnInitializedAsync to be called twice when navigating to another Layout [Blazor] Cascading RouteData in Layout causes OnInitializedAsync to be called twice when navigating to another Layout Apr 8, 2020
@mkArtakMSFT mkArtakMSFT added this to the Next sprint planning milestone Apr 8, 2020
@captainsafia
Copy link
Member

Thanks for posting the issue and providing the repro, @Charnock!

I was able to reproduce the same behavior you saw. There's some funkiness with how the cascading parameter after the processing of the components. The component is initialized before the layout and the re-initialized again after the layout is initialized.

| Switch page | --> | Initialize FetchData |--> | Initialize MainLayout |  --> | Initialize FetchData |

As opposed to

| Switch page | -->  | Initialize MainLayout |  --> | Initialize FetchData |

Marking this as a bug for now. I suspect that we will have to look into the logic in the CascadingValue component a little bit closer.

@captainsafia captainsafia added bug This issue describes a behavior which is not expected - a bug. and removed investigate labels May 6, 2020
@dazinator
Copy link

dazinator commented Jun 30, 2020

Just a hit a similar problem, we were navigating from a page with a non default layout, to one that was missing the layout directive (and so using the default layout), and seeing OnInitAsync called twice, in a blazor wasm application.

We didn't use cascaded values on either page, but we do have a cascaded value surrounding the app.

@ghost
Copy link

ghost commented Jul 23, 2020

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@captainsafia captainsafia removed their assignment Sep 16, 2020
@captainsafia captainsafia added affected-few This issue impacts only small number of customers severity-minor This label is used by an internal tool labels Oct 7, 2020
@javiercn javiercn added the feature-blazor-builtin-components Features related to the built in components we ship or could ship in the future label Apr 19, 2021
@jofford
Copy link

jofford commented Jan 14, 2022

The title is also misleading because it doesn't call OnInitializedAsync twice, it constructs two separate components and calls OnInitialized once on each.

Any updates or workarounds for this? This is a major flaw considering most data is retrieved during OnInitialized and there's no way of knowing if it's the duplicate component.

@jofford
Copy link

jofford commented Jan 17, 2022

Here's a minimal sample to prove duplicate components are being created by Blazor.
https://github.com/jofford/BlazorWasmRouteDataBug

@jofford
Copy link

jofford commented Jan 18, 2022

@mkArtakMSFT mkArtakMSFT modified the milestones: Backlog, BlazorPlanning Nov 5, 2023
@ghost
Copy link

ghost commented Dec 13, 2023

Thanks for contacting us.

We're moving this issue to the .NET 9 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@mkArtakMSFT mkArtakMSFT added the Priority:1 Work that is critical for the release, but we could probably ship without label Jan 11, 2024
@javiercn
Copy link
Member

Tracking in #44781

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affected-few This issue impacts only small number of customers area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. feature-blazor-builtin-components Features related to the built in components we ship or could ship in the future Pillar: Technical Debt Priority:1 Work that is critical for the release, but we could probably ship without severity-minor This label is used by an internal tool
Projects
None yet
Development

No branches or pull requests

6 participants