Ducky Docs
Blazor pages

Render modes and the load rule

Ducky.Blazor runs the same store in every Blazor host. What changes between hosts is how long a store lives, which storage it can reach, and whether a prerendered page hands its state over.

dotnet add package Ducky.Blazor

Register it inside AddDucky with AddBlazor(). Persist<T>(), Prerender<T>() and UseDevTools() call it for you, and so does the generated registration of a slice marked [Persist] or [Prerender].

Components

Inherit DuckyComponent (or DuckyComponent<TState>, whose State is the whole slice, and DuckyLayout for layouts), call Select(...) in OnInitialized, and dispatch from event handlers with Dispatch(...). A store change re-renders the component only when a selected value changed. Select after the first render throws (DUCKY352), and DUCKY009 catches a Select in markup. <DuckySelect Selector="..."> renders a fragment from one selection, and StoreSelectionExtensions.Select serves a component that must keep another base class. Ducky components work without AddBlazor().

The render-mode matrix

Host Store Browser storage (Local, Session) Server storage (IDistributedCache) Prerender seed Cross-tab DevTools
WebAssembly standalone one per app read at init; each change written on the next turn not used (Info log once) none yes when UseDevTools()
Static SSR page one per request not used: status NotStarted read at init; written through; the last writes leave with the request scope, after the response (Static SSR) none no no
Prerender pass of an interactive page one per request, thrown away not used as static SSR; its flush can land after the circuit store read or wrote the key written from the idle store no no
Interactive Server (circuit) one per circuit read at init; written 250 ms after a change; retried after a disconnect; lost only if the circuit ends before a write succeeds read at init; written through; flushed when the circuit ends taken at init, before the first render yes yes
Interactive WebAssembly one per app as WebAssembly standalone not used (restored from the seed only when the slice is also Prerender<T>) taken at init, or at the first Ducky component yes yes
Interactive Auto one circuit store and one WebAssembly store, both live in one document after an enhanced navigation both use the same keys, and each write reaches the other store circuit side as Interactive Server; WebAssembly side not used whichever side starts yes, and between the two stores yes

On the server the store is scoped to the circuit or the request. Code that runs in another DI scope (an IHttpClientFactory DelegatingHandler, a hosted service, a SignalR hub) resolves a different, empty store; reach the circuit's services through a circuit accessor.

In the browser the store starts from a hosted service that AddDucky registers (BlazorOptions.InitializeOnStart, on by default), so a WebAssembly app needs no <DuckyInitializer>; the prerender guide says when it is still needed.

In the browser there is one store per app, and its effects resolve scoped services from the store's own scope, so a scoped service injected into an effect (the template's scoped HttpClient, for example) is a different instance from the one components get.

A circuit that .NET 10 pauses and resumes is a fresh store: its Prerender<T> slices come back from the pause state, its persisted slices from storage, and every other slice starts from its initial state. Circuit pause and resume says which one wins for a slice that is both, and when a change not yet written survives the pause.

One load rule in every mode

Load a slice in an effect on StoreInitialized, and return early when the slice was restored. StoreInitialized comes after every hydration read the host can perform, so a slice that storage or the prerender seed restored is already in the state:

RenderModesGuideTests.csfrom a tested file
public sealed record CatalogState(IReadOnlyList<string>? Products);

public sealed record CatalogLoaded(IReadOnlyList<string> Products);

public sealed class CatalogSlice : Slice<CatalogState>
{
    public CatalogSlice() => On<CatalogLoaded>((_, action) => new CatalogState(action.Products));

    public override string Key => "catalog";

    protected override CatalogState Initial => new(Products: null);
}

public interface ICatalogApi
{
    Task<IReadOnlyList<string>> GetProductsAsync(CancellationToken cancellationToken);
}
RenderModesGuideTests.csfrom a tested file
public sealed class LoadCatalog(ICatalogApi api) : Effect<StoreInitialized>
{
    public override async Task Handle(StoreInitialized action, EffectContext context, CancellationToken cancellationToken)
    {
        // Storage or the prerender seed already restored it: don't load it again.
        if (context.State.WasRestored<CatalogState>())
        {
            return;
        }

        context.Dispatch(new CatalogLoaded(await api.GetProductsAsync(cancellationToken)));
    }
}

To load the slice once per page rather than once in the prerender pass and again in the interactive store, also mark it Prerender<T>, with an include predicate that seeds only a loaded slice:

RenderModesGuideTests.csfrom a tested file
public static DuckyBuilder AddCatalog(DuckyBuilder ducky) => ducky
    .UseJson(DocsJson.Default)
    .AddSlice<CatalogSlice>()
    .AddEffect<LoadCatalog>()
    .Persist<CatalogSlice>() // restored from localStorage...
    .Prerender<CatalogSlice, CatalogState>(static state => state.Products is not null); // ...or from the prerendered page, once loaded
  • The rule is the same on Server, WebAssembly and Auto: there is no "wait for HydrationCompleted" variant at startup. A user switch during a circuit is different: there a per-user load reacts to HydrationCompleted (After a user switch).
  • Keep the predicate: without it, a load that fails during prerender seeds whatever state the failure left, the slice reads as restored, and the load rule never loads it in the interactive store (Prerender).
  • Load a Prerender<T> slice with an Effect<T>, never a reactive effect: the seed waits for running effects, not for reactive pipelines.

Next: Persistence, Prerender, Cross-tab sync, Redux DevTools.