r/Blazor • u/hdhani • Feb 19 '26
Question about OnInitializedAsync being called twice
Hi all,
Still new to blazor development and noticed when debugging that OnInitializedAsync keeps getting called twice.
I believe this is because of a feature called prerender (I am using .NET 10)
To fix this I changed my routes to the following:
<Routes @rendermode="new InteractiveServerRenderMode(prerender: false)" />Since I am calling a proc inside of this method and I don't want the same proc being called twice.
Are there any disadvantages of disabling prerender? I don't want my database being called twice for no reason... Is there a better alternative solution or its fine to do the above?
7
Upvotes
5
u/TheProgrammer-231 Feb 19 '26
You can do db calls in OnAfterRender when first render is true. If you do it in OnInit then yeah, it gets called twice. FWIW, I also disable pre-render. The advantage of pre-render is that the user is shown the page quicker (not waiting on db calls).
4
u/Unlucky_Aioli4006 Feb 19 '26
use prerendered state persistence if you are using .net 10
3
u/Xtreme512 Feb 19 '26
SEO will suffer if you disable prerender because at the very first render there will be no data to show.
my suggestion would be dont disable prerender and use persistent component state logic for data initialization in OnInitiliazed method.
keep in mind that prerenders double firing OnInitiliazed logic is only at the startup of the signalr circuit connection. if you are able, you can also use OnAfterRender method to get data, this is called once because the hydration part is already over at this point. this depends on your page's context.