Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4004

Blazor - why do anything on the 2nd call to OnInitializedAsync()?

$
0
0

This is a follow-on question to my question Why is OnInitializedAsync() called twice?

What I have been doing to date, based on Microsoft's guidance that everything should be requested in the pre-render call, is:

  1. Anything that might be cached I call. If it is not in the cache, the pre-render pass will read it in to the cache. Then the second pass will get it from the cache.
  2. Anything small I first look for in the PersistingComponentStateSubscription. If there, I use that. If not there, I read it, save it in the persisted store, and use it.
  3. For anything else, I only call it if it is not a Pre-Render call (HttpContext is not null).

But thinking about it today, it strikes me that we want to call all initialization in the pre-render pass. And then do nothing on the second pass. Which would be the following:

  1. Read PersistingComponentStateSubscription. If that has a value, return.
  2. Write 'true' to PersistingComponentStateSubscription
  3. Perform all initialization, including reading everything needed from the DB even if that will take 10+ seconds.

Is there any downside to the above? I ask, because why have 2 passes if doing this meets the Microsoft guidance to get everything in the pre-render pass?

The flip approach to the above is to do:

  1. Do all fast initialization.
  2. IfPreRender, return (exit).
  3. Do all slow initialization.

But following that means all the slow initialization will be started a bit later. That will mean it will take longer to fully initialize the page. And as Microsoft's guidance implies that a slow pre-render initialization will not slow down the rendering of the page skeleton, I don't see any advantage to this approach.


Viewing all articles
Browse latest Browse all 4004

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>