Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blazor .Net 8 splash screen

Before .Net 8, I could set a splash screen in the index.html file and tag in the body part. In the new templates there isn't a index.html file anymore.

I can find a lot of info about the old pre .Net 8 structures but not a lot about .Net 8 and the provided templates are very basic.

Does somebody know how to set a splash screen in the new project layout? (I'm using the global wasm option).

Before I had something like this in my index.html file (.net 7 and before)

<body>
<app id="app">
    <div style="width: 100%">
        <div class="p-5 bg-light border rounded-3 marginTop20px marginLeft20px marginRight20px mb-3 jumbotron">
            <h2>Please wait while the application is loading</h2>
        </div>
    </div>
</app>

<div id="blazor-error-ui">
    An unhandled error has occurred.
    <a href="" class="reload">Reload</a>
    <a class="dismiss">🗙</a>
</div>
<script src="_framework/blazor.webassembly.js" asp-append-version="true"></script>

The application is a .Net core hosted project but with the global wasm option.

like image 245
JMA Avatar asked Oct 20 '25 15:10

JMA


1 Answers

You need to detect pre-rendering and show a Splash component before the client side SPA re-renders the page.

  1. Add the Blazr.RenderState.Server Nuget package to the Server project.
  2. Add the Blazr.RenderState services to the server.
builder.AddBlazrRenderStateServerServices();
  1. Add the Blazr.RenderState.WASM Nuget package to the Client project.
  2. Add the Blazr.RenderState services to the client.
builder.AddBlazrRenderStateWASMServices();

With these added, you have access to IBlazrRenderStateService and it's IsPreRender bool property in both server side and client side contexts.

Create a Splash.razor in the client project.

<div class="alert alert-primary m-5">
    BIG SPLASH
</div>

Modify Router to:

  1. Display the Splash component if the page is pre-rendered. Note the return to early exit.
  2. Only do routing when we are in an active mode (in this case the WASM SPA has started).
@using Blazr.RenderState
@inject IBlazrRenderStateService RenderState

@if (RenderState.IsPreRender)
{
    <Splash />
    return;
}

<Router AppAssembly="@typeof(Program).Assembly">
    <Found Context="routeData">
        <RouteView RouteData="@routeData" DefaultLayout="@typeof(Layout.MainLayout)" />
        <FocusOnNavigate RouteData="@routeData" Selector="h1" />
    </Found>
</Router>

This works for any page in the SPA. F5 on counter shows the splash screen.

Note: Blazr.RenderState is mine. You can grab the code if you don't want to use the packages from here: https://github.com/ShaunCurtis/Blazr.RenderState.

like image 135
MrC aka Shaun Curtis Avatar answered Oct 22 '25 04:10

MrC aka Shaun Curtis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!