Course contentsShow
← C#
Lesson 1436 of 1,90812. Modern Frontend with Razor and BlazorFree lesson

Streaming Data Rendering

Improve perceived performance by streaming UI segments as data becomes available.

In this lesson, you will learn how to improve perceived performance by streaming UI segments to the client as data becomes available, rather than waiting for an entire page to load.

The Concept of Chunked UI Delivery

When dealing with complex, data-heavy dashboards, waiting for a single, monolithic server-side operation to complete before rendering the UI creates a "blank screen" penalty. Even with efficient backend calls, the total time to first meaningful paint can suffer. Streaming Data Rendering allows the server to send the initial layout immediately and then "push" individual components or data blocks as they finish computing.

In the Blazor ecosystem, this is achieved by leveraging IAsyncEnumerable<T> or streaming task patterns. Instead of awaiting a single task that fetches everything, you initialize your state with partial data and let the component lifecycle handle the incoming stream.

Illustrative Pattern

Consider a dashboard that needs to fetch data from three separate legacy services. Instead of blocking, we can use a Stream to update the state incrementally:

protected override async Task OnInitializedAsync()
{
    // The UI renders immediately with an empty list
    await foreach (var item in DataService.GetStreamedItemsAsync())
    {
        _items.Add(item);
        // Trigger a re-render to push the new segment to the browser
        StateHasChanged(); 
    }
}

By calling StateHasChanged() within the await foreach loop, the browser receives the markup for each item as soon as it arrives. This provides immediate visual feedback to the user, who sees the list growing in real-time rather than staring at a loading spinner for five seconds.

Why It Matters

This approach transforms the user's perception of speed. By rendering a skeleton UI or the "top half" of a page first and filling in the gaps via streaming, you eliminate the "all or nothing" rendering trap. This is particularly vital in applications where backend latency is inconsistent or dependent on slow external database queries. It turns a long wait into a series of small, manageable visual updates.

Key Takeaway: Streaming Data Rendering optimizes perceived performance by incrementally pushing UI fragments to the client as asynchronous data arrives, replacing monolithic loading delays with an immediate, progressive rendering experience.