Replies: 1 comment 1 reply
-
In case anyone else has the same problem (static SSR that's not meant to be hydrated needing to run async code before render), here's a simple way to bypass it: simply inherit from the class listed below (instead of the implied /// <summary>
/// Overrides to allow for asynchronous actions to be performed in fully SSR pages before the response gets written
/// </summary>
public class AsyncComponentBase : ComponentBase
{
private bool _initialized;
public override Task SetParametersAsync(ParameterView parameters)
{
parameters.SetParameterProperties(this);
if (_initialized) return CallOnParametersSetAsync();
_initialized = true;
return RunInitAndSetParametersAsync();
}
private async Task RunInitAndSetParametersAsync()
{
OnInitialized();
await OnInitializedAsync();
await CallOnParametersSetAsync();
}
private async Task CallOnParametersSetAsync()
{
OnParametersSet();
await OnParametersSetAsync();
StateHasChanged();
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
To test pure Blazor SSR, I removed Blazor JS from the layout and modified the
Weather
component to remove the null check for theforecasts
array in the markup.When I ran the project, I encountered a Null Reference Exception (NRE). This is expected, as in the server or WASM model, it appears that the markup is being evaluated before the
OnInitializedAsync
task is completed.I can fix this by simply checking if forecasts is not null, which works. However, while the response is not streamed, it takes some time for the rendered HTML to appear in my browser.
My question is: while the HTML is being evaluated on the server before being sent to the browser, how does the server know when to send the response? How does it know it should wait for the
OnInitializedAsync
method to finish?If I try to update the component state, for example via an event, it doesn't work because it only sends the HTML once, so the interaction won't work. However, while it waits for OnInitializedAsync to finish, why doesn't it wait for other state changes? Obviously, it is impossible to predict future state changes, so how does Blazor SSR know when to send the data to the browser?
Beta Was this translation helpful? Give feedback.
All reactions