Skip to content

Feature: support for HTML streaming #491

@zannabianca1997

Description

@zannabianca1997

I found maud quite adapt in generating pieces of HTML, and the output of the html macro can easily be converted to an async stream.

By adding a single control flow @yield marking where to pass the buffer forward toward the client I was able to generate a streaming body.

A brief example of the resulting code:

async fn slow_list() -> StreamingMarkup<impl Stream<Item = Markup> + Send + 'static> {
    streaming_html!(
        (DOCTYPE)
        html {
            head { title { "Slow list" } }
            body {
                ol {
                    @yield
                    @for i in 0..10 {
                        li {
                            ({sleep(Duration::from_secs(1)).await; format!("Slept for {}", i+1)})
                            @yield
                        }
                    }
                }
            }
        }
    )
}

I've opened a PR /pull/490 with the initial implementation and Axum support. This request is still a draft, as it is missing both tests and documentation. I think this feature would be more adapt to a sister/support crate, but I would have to copy all the parsing machinery.

Is this something that could be of interest?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions