Net8BlazorAuto icon indicating copy to clipboard operation
Net8BlazorAuto copied to clipboard

Always triggers the site to run on Server Side, then switch to WebAssembly

Open HarrisonWD opened this issue 1 year ago • 0 comments

So I've worked on a app where I was getting issues with a property always returning null, even though I can see this property getting data from an API. I knew this because the front end would change depending on the value of this nullable property.

When I typed in a URL in the browser to the page and hit enter, for a brief moment the data would be there, but soon after it would disappear and be null again after doing some digging, it was because the site would be immediately rendered as Server Side and without prompt, switch to Web Assembly. I say "Without Prompt" because I haven't navigated to another part of the website and it does a sneaky switch in-between navigating to different components. It would just immediately switch no matter if I've just built the website and started debugging or if I've been debugging for a while.

Here's a .gif better showing the issue. All I've done to this new app is change the Counter.razor page to show the runtime.

@inject NavigationManager NavigationManager;

@page "/counter"

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

<p>@Message</p>

<button class="btn btn-primary" @onclick="NavigateToWeather">Weather</button>

@code {
    private int currentCount = 0;

    private string Message = "";

    private void IncrementCount()
    {
        currentCount++;
    }

    protected override async Task OnInitializedAsync()
    {
        await GetRuntime();
    }

    private async Task GetRuntime()
    {
        var isWebAssembly = OperatingSystem.IsBrowser();

        if (isWebAssembly)
            Message = "The app is running on WebAssembly";
        else
            Message = "The app is running on sever-side";

        Console.WriteLine(Message);
    }

    private void NavigateToWeather()
    {
        NavigationManager.NavigateTo("weather");
    }
}

RenderTests

HarrisonWD avatar Aug 15 '24 14:08 HarrisonWD