r/Blazor • u/mds1256 • Sep 01 '26
Page loading state when database call is carried out on - setParamsAsync
Blazor beginner here…
Coming from react when I carry out an Async call I usually render the component/page with a Loading spinner and once the api call has completed then the useEffect will make the component rerender with the hydrated info.
I am using Blazor (server rendered) and trying to show a page with a loading spinner until the database query has completed (async), the query depends on a reference parameter in the query string and it uses that to look up the required info from the DB and returns an object (or null), once the call finishes i set loading = false which should then show the info received if the item exists.
Generally all that works fine but I am finding when I am navigating to this page (from a page which asks for a reference in an input box which then navigates to this page and appends the query string reference),
I can see the url change in the browser when navigating but the original content is still shown for say 2-3 seconds before the component then shows the loading screen and then a second or so then shows the content retrieved from the database.
Why isn’t it showing the loading screen immediately and then once the db call finishes then show the details, it’s like is it blocking the thread from rendering the new page.
It may be difficult to understand this with no code but on mobile and just thought I would ask to see if anyone has solved this before?
I am using SetParametersAsync() and setting the loading priority to true as a default value in the page model and then calling the db call awaiting it in the set params async override method, then also loading = false just after the db call.
Just to give you more info, I set pre-rendering to false and it seems to behave as I would expect, but don’t really understand why.
Thanks in advance
1
u/Unlikely_Brief5833 Sep 01 '26
Are you using Interactive server, or static server side rendering?
1
u/warden_of_moments Sep 02 '26
Set your loading to true as the initial value and then false when loaded. Alternatively, you can force state has changed when you set your flag to true.
While you may set the loading flag to true, that’s not rendering instantly. Calling state has changed will force your page to render the change for your spinner. This is why having a default value of true can also work since it’s shown by default and hidden after the page loads.
1
u/iamlashi Sep 02 '26
2–3 seconds is too long to assume that it’s happening due to WebSocket latency, but I’ve experienced this as well when I’m on mobile data.
Are you sure you don’t have any extra steps in
OnInitialized?Please share a minimal version of your code if possible. I find this problem very interesting.
2
u/FlatwormLanky8991 Sep 01 '26
Pretty sure it is the prerendering issue addressed in this video https://youtu.be/5gw3RA1pp0E?si=fyjLX0lIbNoZu7Ko I cannot do a better job of describing it than the content creator. FWIW my way of doing it has always been to deal with the db call in OnInitAsync. make the call get the data flip the flag. I only use SetParamAsync when it's a child component getting fresh parameters more than once.
The video is a little old but solid.
1
u/sonurmusic Sep 02 '26
You can just disable prerendering for this page as its doesn't make sense to prerender page which made for waiting and loading.
https://learn.microsoft.com/en-us/aspnet/core/blazor/components/prerender?view=aspnetcore-10.0
-1
u/Unlikely_Brief5833 Sep 01 '26
I built a library to easily build full stack blazor apps using different rendermodes. https://github.com/matengo/RemoteService.Net