Normally when a page is being created & rendered in Blazor version 8, RenderMode.InteractiveServer, OnInitializedAsync()
is called once. But when I do a Reload on Chrome, it is called twice. And all child components in the page are called twice too.
Why is it being called twice? And what can I do to avoid reading from the database to populate the page twice? And very importantly, in a way that will call from the database at least once for any situation?
Every time I think I finally understand the rendering mode, I realize I have it wrong again. So InteractiveServer may call OnInitializedAsync() twice if pre-rendering is on. And I want that lightening fast initial render. And there is no way to know if a call is the pre-render or post-render call. Or possibly the only-render call.
To handle this I have seen three main approaches suggested:
[CascadingParameter] HttpContext? == null
to determine if it's the pre-render.Microsoft does answer this question - Persist prerendered state. So the suggested approach from Microsoft is read from the DB on the pre-render OnInitializedAsync
, save that, and then reuse it for the subsequent OnInitializedAsync
call.
With that said, the solutions listed here by MrC, Logarr, and Rena are all valid approaches that work.