'Blazor Webassembly Prerendering: How to avoid HeadContent render two times

I have a Blazor WASM prerendered .NET6 application.

In Index.razor on the Hosted server project, I have a component that loads css files dynamically.

The problem is that the files are rendered two times causing the page to lose and reapply the css for a moment, very noticeable with slow connections.

<HeadContent>
  @foreach (var style in Template.CssRepository)
     {
       <link rel="stylesheet" href="@style">
     }
</HeadContent>

I would like the styles to be prerendered an not reloaded again when the app is "ready".

The Template object state is already persisted using ApplicationState.TryTakeFromJson<>, but HeadContent reload itself recalling all Css files.

I tried using OnAfterRender method but when it fires the css files have already been loaded twice.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source