This is the next part of the ASP.NET Core on .NET 6 series. In this post, I'd like to have a look into preserve prerendered state in Blazor apps.
In Blazor apps can be prerendered on the server to optimize the load time. The app gets rendered immediately in the browser and is available for the user. Unfortunately, the state that is used on while prerendering on the server is lost on the client and needs to be recreated if the page is fully loaded and the UI may flicker, if the state is recreated and the prerendered HTML will be replaced by the HTML that is rendered again on the client.
To solve that, Microsoft adds support to persist the state into the prerendered page using the <preserve-component-state /> tag helper. This helps to set a stage that is identically on the server and on the client.
Actually, I have no idea why this isn't implemented as a default behavior in case the app get's prerendered. It should be done easily and wouldn't break anything, I guess.
Try to preserve prerendered states
I tried it with a new Blazor app and it worked quite well on the FetchData page. The important part is, to add the preserve-component-state tag helper after all used components in the _Host.cshtml. I placed it right before the script reference to the blazor.server.js:
<body>
<component type="typeof(App)" render-mode="ServerPrerendered" />
<div id="blazor-error-ui">
<environment include="Staging,Production">
An error has occurred. This application may no longer respond until reloaded.
</environment>
<environment include="Development">
An unhandled exception has occurred. See browser dev tools for details.
</environment>
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>
<persist-component-state /> <!-- <== relevant tag helper -->
<script src="_framework/blazor.server.js"></script>
</body>
The next snippet is more or less the same as on Microsoft's blog post, except that the forecast variable is missing there and System.Text.Json should be in the usings as well
This reminds me of the ViewState we had in ASP.NET WebForms. Does this make Blazor Server the successor of ASP.NET WebForms? Just kidding.
Actually, it is not really the ViewState, because it not gets sent back to the server. It just helps the client restore the state created on the server initially while it was prerendered.
What's next?
In the next part In going to look into the support for HTTP/3 endpoint TLS configuration in ASP.NET Core.
.netvisual-studiocsharpasp.net
Weitere News:
1 Meinung
0
Do soothing activities, like taking a https://tutuappx.com/Vidmate warm bath, listening to calming music, or making a gratitude list.
Jürgen Gutsch arbeitet bei der Firma ASS.TEC GmbH in Villingen-Schwenningen als Softwareentwickler und Teamleiter und ist dort für die gesamten .NET-Entwicklung verantwortlich. Neben Familie und Beruf ist Jürgen Leiter des ".NET-Stammtisch Konstanz-Kreuzlingen" und betreibt ein Blog auf ASP.NET Zone zum Thema "ASP.NET und mehr..."