-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Blazor: OnInitializedAsync with prerendering #15266
Comments
@JDBarndt thanks for contacting us. Unfortunately the doc is obsolete. I've filed an issue to get it fixed. We understand the scenario and will be looking into ways to improve the situation in the future, whether it is with specific guidance or enhancements to the framework. The main issue arises because during prerendering the renderer waits until the application has completed the first render to produce the initial HTML, while the renderer tries to produce an initial render and update the UI as soon as possible, resulting normally in a render before the data is present and a render after the data is present. This is a trade-off between the flash on the UI and making the application interactive. There are ways to design around it, like passing the query results to the component so that upon rendering it again it simply re-renders the cached results. We could in the future investigate if we can do something to make the initial render on the client wait until the first meaningful content paint at the cost of delaying the app becoming interactive. I've file this to keep track of the docs issue dotnet/AspNetCore.Docs#15271 |
actually I'm not sure that the following is true:
from logging the output the following happens:
Actually It's also really simple to debug. Just remove the <script> tag. and the OnInitialized, will only be called once with -- this is somehow stupid, since the websocket connection is stateful. I would understand if everything with blazor is not stateful. unfortunatly the re-rendering is kinda something that makes no sense and there is also no way to work around it. What should happen (if Blazor wants to be useful):
btw. before 3.1 hits blazor is already kinda broken, with 3.1 this issue gets relaxed, by the fact that I can actually add parameters to the component which is a work around. but not a solution. There are many JS/Typescript frameworks with prerendering and none of them has the same issue that blazor has. most of them are stateless, thus you need to actually put a variable with the complete state into your html. Blazor is stateful, it should not have had this problem in the first place. Sorry if my tone is not correct, since I'm really sad because Blazor is such a great idea and even if the end design decision are good (Html.RenderComponentAsync, RenderMode.ServerPrerendered) they have some really bad flaws (this, #14433) that will hinder adoption. Also this is not solved by adding something to the docs. It would be even more cringe if doing that. |
When the app is being prerendered it gets initialized and torn down after producing the HTML and then the client starts up a new application. You can validate this by implementing IDisposable on your root component and putting a break point on it, and you'll see how it gets called after the content has been prerendered. You don't get a websocket connection until after the initial render of the page has happened and the SignalR connection gets initialized. Is at that point where the app gets started again and becomes stateful. The goal of prerendering is not to reduce service calls, it is to render content for the user faster. If you want to reduce expensive service calls, use a cache.
Things are not that simple and every solution has trade-offs. We used to do it that way and we switched to the current approach. You can read the details on some of the challenges it involves here: |
well if it is that challenging, to do it with a stateful approach, why not at least try to go the redux/react way. Stateful Prerender and preserving the state inside a:
in 99.9% of scenarios people would prefer that over double initialization or something that is extremly hard (caching), it could even be configurable. |
On server-side blazor it is simpler and better to use the in memory cache for that.
As an example below is a modified WeatherService that shows that it causes no flickering.
|
For server-side blazor the recommendation is to pass in an identifier that can be used to retrieve the state you want to preserve as a parameter and to use that identifier within your services to cache the state in memory. |
The recommendation, is extremly complex, does not work automatically and also is extremly annoying when combined with authentication. It would be way easier to use the script tag approach (which also works with multiple Servers),
also caching is always the worst solution and will most often lead to more problems than the previous renderer had.
Not sure why it is even a suggestion...
Von meinem iPhone gesendet
Am 23.10.2019 um 17:15 schrieb Javier Calvarro Nelson <notifications@github.com>:
Closed #15266<#15266>.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub<#15266>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAMQXOLRIDJOWJ2ZB7FKM33QQBTBBANCNFSM4JDSKTLA>.
|
I have been using this to detect pre-rendering:
In my _Host.cshtml file:
|
@Grauenwolf How to use this to avoid flashing UI when using |
I haven't figured that out yet.
Jonathan Allen
619-933-8527
Scholars of Alcala
scholarsofalcala.org
…________________________________
From: Karluzo <notifications@github.com>
Sent: Friday, December 13, 2019 1:22:17 AM
To: aspnet/AspNetCore <AspNetCore@noreply.github.com>
Cc: Jonathan Allen <grauenwolf@gmail.com>; Mention <mention@noreply.github.com>
Subject: Re: [aspnet/AspNetCore] Blazor: OnInitializedAsync with prerendering (#15266)
@Grauenwolf<https://github.com/Grauenwolf> How to use this to avoid flashing UI when using RenderMode.ServerPrerendered?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#15266>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ACRCUTPDTFFDR7OBCRSDGHTQYNH4TANCNFSM4JDSKTLA>.
|
This was already mentioned in #13607, #14977, and #13448. OnInitializedAsync will fire twice, once during pre-rendering and once after the app bootstraps. This is apparently by design.
Is there any recommendation on how to avoid requesting the data for the page/component more than once?
An example of why this could be problematic is with the Blazor demo's weather forecast page:
FetchData.razor:
When loading into this page with prerendering, you'll see the initial grid loaded with weather data. Then, the app bootstraps and the component re-initializes a second time and sends an updated grid back to the client, replacing the initial grid with a new one. That's easily visible to the end-user because
GetForecastAsync
returns randomized data.That particular data set is generated in-memory and has no meaningful performance impact, but if it instead was the result of a database, external API, or other call, you certainly wouldn't want to do that more than once. Obviously caching of some kind can and likely would be a solution, but I'm wondering if there is some other way within Blazor to avoid multiple requests.
I would almost expect that between prerendering and the app bootstrapping, the initial component state is "shared" so that it knows it doesn't have to regenerate, especially after reading https://docs.microsoft.com/en-us/aspnet/core/blazor/hosting-models?view=aspnetcore-3.0#stateful-reconnection-after-prerendering:
Must be missing something here? Thanks!
The text was updated successfully, but these errors were encountered: