question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Blazor: OnInitializedAsync with prerendering

See original GitHub issue

This was already mentioned in https://github.com/aspnet/AspNetCore/issues/13607, https://github.com/aspnet/AspNetCore/issues/14977, and https://github.com/aspnet/AspNetCore/issues/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:

protected override async Task OnInitializedAsync()
{
	forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
}

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:

The client reconnects to the server with the same state that was used to prerender the app. If the app’s state is still in memory, the component state isn’t rerendered after the SignalR connection is established.

Must be missing something here? Thanks!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
schmitchcommented, Oct 24, 2019

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 #15266https://github.com/aspnet/AspNetCore/issues/15266.

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/aspnet/AspNetCore/issues/15266?email_source=notifications&email_token=AAMQXOOV4JS2LZO3Z7CPKPTQQBTBBA5CNFSM4JDSKTLKYY3PNVWWK3TUL52HS4DFWZEXG43VMVCXMZLOORHG65DJMZUWGYLUNFXW5KTDN5WW2ZLOORPWSZGOUMVJV5Y#event-2737478391, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAMQXOLRIDJOWJ2ZB7FKM33QQBTBBANCNFSM4JDSKTLA.

2reactions
javiercncommented, Oct 23, 2019

On server-side blazor it is simpler and better to use the in memory cache for that.

  • It avoids rountripping the state.
  • It can be combined with a react/redux approach where you simply pass an id for the store and put the store in a memory cache with the id, which is then used to retrieve the store when the app reconnects and to produce an identical render to the one it was prerendered.

As an example below is a modified WeatherService that shows that it causes no flickering.

        public WeatherForecastService(IMemoryCache memoryCache)
        {
            MemoryCache = memoryCache;
        }
        
        public IMemoryCache MemoryCache { get; }

        public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
        {
            return MemoryCache.GetOrCreateAsync(startDate, async e =>
             {
                 e.SetOptions(new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(30) });
                 var rng = new Random();
                 await Task.Delay(TimeSpan.FromSeconds(10));
                 return Enumerable.Range(1, 5).Select(index => new WeatherForecast
                 {
                     Date = startDate.AddDays(index),
                     TemperatureC = rng.Next(-20, 55),
                     Summary = Summaries[rng.Next(Summaries.Length)]
                 }).ToArray();
             });
        }

        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };
Read more comments on GitHub >

github_iconTop Results From Across the Web

Blazor Server Prerendered: Where to load data and update ...
I want to load data from an API and display a loading animation. When I load in OnInitializedAsync() the animation is shown but...
Read more >
ASP.NET Core Razor component lifecycle
Blazor apps that prerender their content on the server call OnInitializedAsync twice: Once when the component is initially rendered ...
Read more >
Enabling prerendering for Blazor WebAssembly apps
In this post I describe how to host a Blazor WebAssembly app in an ASP.NET Core app and how to enable WebAssembly prerendering....
Read more >
Prerendering Blazor Apps - How does it work / tips and tricks
Blazor comes with the option to prerender your webpage on the server. ... The first time OnInitializedAsync is called happens on the server....
Read more >
Does .NET 6 fix Blazor Prerendering?
Prerendering eradicates Blazor WASM's initial load time and .NET 6 addresses its one key limitation.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found