Search code examples
asp.net-coreblazorblazor-server-side

RenderMode.InteractiveServer call OnInitializedAsync() twice - how can I avoid all that processing twice?


I have been using

<component type="typeof(HeadOutlet)" render-mode="Server" />

However, the latest version of DevExpress requires:

<Routes @rendermode="@RenderMode.InteractiveServer"/> 

Which I assume I can do as:

<component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />

The new .NET 8 code sample appears to have no _Host.cshtml and defines all this a little different. But I can use the old _Host.cshtml + App.razor approach and don't need to conver to the new Routes.razor + App.razor - correct?

This all works fine for me - but asking to confirm that it should, as opposed to somehow I'm lucky and a change will break my code.

And my big question is as follows. When I switch to ServerPrerendered, it then calls OnInitializedAsync() twice. This is a big hit on the database on a lot of my pages. So it will take twice as long for the pages to render.

Is there any way to avoid this? Or are we stuck with slower pages if ServerPrerendered is used?


Solution

  • But I can use the old _Host.cshtml + App.razor approach and don't need to convert to the new Routes.razor + App.razor - correct?

    Yes

    When I switch to ServerPrerendered, it then calls OnInitializedAsync() twice. This is a big hit on the database on a lot of my pages. So it will take twice as long for the pages to render.

    What render mode do you have set on App? My guess is you aren't.

    See this commentary of mine which explains what's going on and how to detect which mode you are rendering various components in - https://github.com/ShaunCurtis/Blazor.ExploreRendering/blob/master/Documents/Going-For-Broke.md.