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

Comments

@ajai1109
Copy link

@ajai1109 ajai1109 commented Aug 26, 2019

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 label Aug 26, 2019
@pranavkm

This comment has been minimized.

Copy link
Contributor

@pranavkm pranavkm commented Aug 26, 2019

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.

@pranavkm pranavkm closed this Aug 26, 2019
@ajai1109

This comment has been minimized.

Copy link
Author

@ajai1109 ajai1109 commented Aug 27, 2019

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

This comment has been minimized.

Copy link

@brzezinol brzezinol commented Aug 27, 2019

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

@ajai1109

This comment has been minimized.

Copy link
Author

@ajai1109 ajai1109 commented Aug 27, 2019

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

How to reopen this issue?

@ajai1109

This comment has been minimized.

Copy link
Author

@ajai1109 ajai1109 commented Aug 27, 2019

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

This comment has been minimized.

Copy link
Author

@ajai1109 ajai1109 commented Aug 27, 2019

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

This comment has been minimized.

Copy link
Contributor

@pranavkm pranavkm commented Aug 27, 2019

@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

This comment has been minimized.

Copy link

@brzezinol 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

This comment has been minimized.

Copy link

@sbwalker sbwalker commented Sep 10, 2019

What namespace does RenderMode.Server exist in?

@divinci

This comment has been minimized.

Copy link

@divinci divinci commented Sep 10, 2019

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

@AlaaNassar

This comment has been minimized.

Copy link

@AlaaNassar AlaaNassar commented Sep 21, 2019

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

This comment has been minimized.

Copy link

@MisinformedDNA MisinformedDNA commented Sep 27, 2019

@pranavkm Can you please reopen?

@msftbot msftbot bot 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.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
8 participants
You can’t perform that action at this time.