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

In Blazor component (razor page) OnInitializedAsync method fires twice. #13448

Closed
ajai1109 opened this issue Aug 26, 2019 · 12 comments
Closed

In Blazor component (razor page) OnInitializedAsync method fires twice. #13448

ajai1109 opened this issue Aug 26, 2019 · 12 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@ajai1109
Copy link

If we NavigateTo Blazor component (razor page) OnInitializedAsync method fires twice. But in another Blazor project, it is not executing twice!

@Eilon Eilon added the area-blazor Includes: Blazor, Razor Components label Aug 26, 2019
@pranavkm
Copy link
Contributor

This is likely due to pre-rendering. If you think that's not the case, feel free to reopen and we'd be happy to investigate.

@ajai1109
Copy link
Author

OnInitializedAsync execution happening twice (the entire process will execute again) and this issue were not happening on projects those created in the earlier preview version.
@* Remove the following line of code to disable prerendering @
@
@(await Html.RenderComponentAsync())*@ I have removed the code in Host.cshtml but the navigations and screen appearing is not like earlier with the code. Could you please reopen investigate on this.

@brzezinol
Copy link

I can confirm, component OnInitializedAsync is called twice on page load.
Tested on new server side project.

@ajai1109
Copy link
Author

I can confirm, component OnInitializedAsync is called twice on page load.
Tested on new server side project.

How to reopen this issue?

@ajai1109
Copy link
Author

This is likely due to pre-rendering. If you think that's not the case, feel free to reopen and we'd be happy to investigate.

OnInitializedAsync execution happening twice (the entire process will execute again) and this issue were not happening on projects those created in the earlier preview version.
@* Remove the following line of code to disable prerendering @
@@(await Html.RenderComponentAsync())*@ I have removed the code in Host.cshtml but the navigations and screen appearing is not like earlier with the code. Could you please reopen investigate on this.

@ajai1109
Copy link
Author

This is likely due to pre-rendering. If you think that's not the case, feel free to reopen and we'd be happy to investigate.

Is there any way to prevent methods multiple executions on OnInitializedAsync

@pranavkm
Copy link
Contributor

@ajai1109 \ @brzezinol the default server-side template has pre-rendering enabled. This would result in multiple calls to OnInitializedAsync. You can update _Host.cshtml to not perform pre-rendering:

// _Host.cshtml
    <app>
-        @(await Html.RenderComponentAsync<App>(RenderMode.ServerPrerendered))
+        @(await Html.RenderComponentAsync<App>(RenderMode.Server))
    </app>

If that's not what's causing the call to appear twice, please share a sample app.

@brzezinol
Copy link

brzezinol commented Aug 27, 2019

Ok, in other hands, how to use pre-rendering properly?
One way is disable it, but somebody created this functionality for some reason, maybe there is some way to properly initialize component in blazor server-side ?
I initialize Timer on Page, so when page is initialized twice I got two event handlers...

`
protected override async Task OnInitializedAsync()
{
Apis = await ApiSourceService.GetApisAsync();

    if(Apis != null && Apis.Count > 0)
    {
        if (refreshTimer != null)
            return;

        refreshTimer = new System.Timers.Timer(10000);
        refreshTimer.Elapsed += OnTimedEvent;
        refreshTimer.Start();
    }
}`

How to handle code above with pre-rendering.
I know that it is bad solution because of handler will lost on leave page, but it is only for question.

I cannot disable prerendering RenderMode.Server is not known...

@sbwalker
Copy link

What namespace does RenderMode.Server exist in?

@divinci
Copy link

divinci commented Sep 10, 2019

What a silly idea to call the main entry point of a page twice?

@AlaaNassar
Copy link

i confirm this issue happens in client project i use latest preview 9 version
for me it calls the request to the api 2 times,

there's no @(await Html.RenderComponentAsync<App>(RenderMode.ServerPrerendered)) at client side code

@MisinformedDNA
Copy link
Contributor

@pranavkm Can you please reopen?

@ghost ghost locked as resolved and limited conversation to collaborators Dec 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

8 participants