Search code examples
c#asp.net.netblazorblazor-webassembly

Improve Performance of initial page load time with Blazor Webassembly


.Net Blazor WebAssembly sounds really great. It can run fully client-side, meaning there is no need for a dynamic server and one can even host it on a CDN. Even better, it is possible to turn it into an installable PWA. So I looked at some demos and was horrified to find out, that loading an example page on synfusion.com approximately took me 15 seconds with a decent internet connection.
Here is the example: https://blazor.syncfusion.com/showcase/wasm/health-tracker/

I do absolutely understand, that the entire .Net Runtime for Webassembly needs to be downloaded and that this naturally takes some time, and I have already measured, that the time on subsequent loads, when the runtime for the page is already cached, is "only" about 3 to 5 seconds.
This however is still a lot, and in my opinion unusable in a real life secnario. Which user is going to wait for a website that takes 15 seconds to load?

So my question is, there surely must be a built-in way to improve startup time. Maybe one could only load the parts of the .Net Runtime, that are necessary for the Home Page, or whichever page the user requested, to work. I understand thoguh, that this is probably difficult, as the .Net Runtime is most likely very interconnected, and most parts will rely on other parts. I could imagine having a traditional Html/Js/CSS Page served first, until the Webassembly runtime is downloaded. Maybe one can at least display the not yet interactive UI, to give the user a sense of speed? Also, why are subsequent loads taking more than 3 seconds? The runtime should be cached, does it really take that long, to retrieve it from the cache?

If anyone knows how to improve startup time in a .Net Blazor WASM Application, I would be glad to hear a solution, because the default startup time is unbearably long, and at least for me not applicable in any real world scenario, what really is a huge shame.

PS:
I of course have also tried to built my own page and measured the initial startup time and the cached one, which both are faster than the syncfusion ones, but of course the application is also way smaller in size, when compiled and built.

Ah, yeah, and for anyone asking for a minimal reproducible example, take the Web App template in Visual Studio, configured for Interactive render mode = WebAssembly and tick "Include sample pages".


Solution

  • I figured it out: .Net 8 solved the problem by introducing a new rendermode called Auto. Just adding the @rendermode Auto directive improves the startup time heavily. This essentially means, that on the initial page load @rendermode InteractiveServer and on every susbequent load @rendermode InteractiveClient is used. On the initial load the server serves the request, and the required wasm is downloaded in the background, and on every forthfollowing request the cached webassembly is used and therefore a connection to the server is not even established. This gives the best of both worlds, the only downside being, that two projects are needed - on for the server and one for the client.