Explain the Double Render problem during prerendering: why does OnInitializedAsync run twice, and how do you prevent side effects or flickering?
Last updated
With prerendering, a component renders once on the server (static HTML) and again on the client with a fresh instance, so
OnInitializedAsyncruns twice. This causes duplicate side effects and flicker as data is refetched.Use
PersistentComponentStateto carry the server-fetched data to the client so it reuses it instead of refetching.Guard non-idempotent side effects with
RendererInfo.IsInteractiveor move them toOnAfterRenderAsync(firstRender)(client-only). As a last resort, setprerender: false.
- https://www.youtube.com/watch?v=qQ91rdMgIMM
- https://juliocasal.com/blog/dealing-with-blazor-prerendering
- https://alexb72.medium.com/prerendering-in-blazor-how-to-persist-state-using-persistingcomponentstate-6a1ead059aa0
- https://learn.microsoft.com/en-us/aspnet/core/blazor/state-management/prerendered-state-persistence?view=aspnetcore-10.0
- https://jonhilton.net/persist-state-between-renders-net8/
- https://learn.microsoft.com/en-us/answers/questions/1621494/why-is-oninitializedasync-called-twice-on-a-page-r
- https://supportcenter.devexpress.com/ticket/details/t1217363/a-new-project-created-with-wizard-calls-the-oninitializedasync-event-twice
- https://blazor-university.com/components/component-lifecycles/