Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting server pre-rendering in Blazor server app

Is there any way to detect the pre-rendering is going on in a Blazor component from the OnInitializedAsync life cycle method? I know the component workflow would call OnInitializedAsync called two times, the first time for the pre-rendering and the second time for actual rendering. Finally, called the OnAfterRenderAsync method in a single time for actual rendering.

But, I need to detect the pre-rendering in OnInitializedAsync. So that I could make some changes in pre-rendering and prevent it in actual rendering vice versa.

I checked the below GitHub issue, but it doesn't have a valid solution. I hope, it should be addressed in an API like IsPrerendering.

https://github.com/dotnet/aspnetcore/issues/17282

Thanks in advance.

like image 736
Son04 Avatar asked Jan 26 '26 00:01

Son04


1 Answers

In .Net 9, there is a new API called RendererInfo that can detect render mode at runtime.

@if (RendererInfo.IsInteractive)
{
    <button class="btn btn-primary" @onclick="IncrementCount">Counter</button>
}
else
{
    <p>Loading...</p>
}

Do not use IJSRuntime for determining the render mode, it is not reliable as mentioned here.

like image 82
Shervin Ivari Avatar answered Jan 28 '26 12:01

Shervin Ivari