mrpmorris / Fluxor

Fluxor is a zero boilerplate Flux/Redux library for Microsoft .NET and Blazor.
MIT License
1.22k stars 139 forks source link

.NET 8 server-side rendering doesn't work with Fluxor #451

Closed guythetechie closed 8 months ago

guythetechie commented 9 months ago

I created a project using the default .NET 8 Blazor Web App template. I then modified the counter page to use Fluxor for incrementing. Unfortunately, incrementing by clicking on button does not work.

Because the default template uses server-side rendering, ComponentBase.OnAfterRender is not called and the Fluxor IStore doesn't get initialized. image

To fix it, I simply injected and initialized IStore in my component.

    [Inject]
    private IStore Store { get; set; } = default!;

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        await Store.InitializeAsync();
    }

Just wanted to raise this as Blazor in .NET 8 defaults to server-side rendering. There's a good chance most .NET 8 Fluxor projects will run into this.

Here are sample repos that illustrate the issue:

mrpmorris commented 8 months ago

It seems BSSR doesn't execute AfterRenderAsync, so the StoreInitializer component doesn't automatically initialize.

I suppose you'd have to resolve IStore in your app boot up and call InitializeAsync in there?

guythetechie commented 8 months ago

I suppose you'd have to resolve IStore in your app boot up and call InitializeAsync in there?

That's right. It's a trivial fix if you know what the problem is. That said, anyone using Fluxor + BSSR will run into the issue, so might be worth calling this out somewhere. Not sure how easy it is to address directly in the library.

mrpmorris commented 8 months ago

I've not really tried BSSR, I will get around to it :)

michaelvolz commented 7 months ago

just fyi: OnInitializedAsync does not seem to work in the latest build, but with OnAfterRenderAsync it works fine. Thanks for the tip!

   protected override async Task OnAfterRenderAsync(bool firstRender)
   {
       if (firstRender)
       {
           await Store.InitializeAsync();
       }
   }
mrpmorris commented 7 months ago

OnAfterRender* doesn't execute in SSR.

michaelvolz commented 7 months ago

I am using it on my machine. Works perfectly. Server/WebAssembly and Auto.