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:
- 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.
- 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. - 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:
- Read
PersistingComponentStateSubscription
. If that has a value, return. - Write 'true' to
PersistingComponentStateSubscription
- 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:
- Do all fast initialization.
- IfPreRender, return (exit).
- 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.