Function leptos_actix::render_app_to_stream

source ·
pub fn render_app_to_stream<IV>(
    options: LeptosOptions,
    app_fn: impl Fn() -> IV + Clone + 'static,
    method: Method
) -> Route
where IV: IntoView,
Expand description

Returns an Actix struct@Route that listens for a GET request and tries to route it using leptos_router, serving an HTML stream of your application. The stream will include fallback content for any <Suspense/> nodes, and be immediately interactive, but requires some client-side JavaScript.

The provides a MetaContext and a RouterIntegrationContext to app’s context before rendering it, and includes any meta tags injected using leptos_meta.

The HTML stream is rendered using render_to_stream, and includes everything described in the documentation for that function.

This can then be set up at an appropriate route in your application:

use actix_web::{App, HttpServer};
use leptos::*;
use leptos_router::Method;
use std::{env, net::SocketAddr};

#[component]
fn MyApp() -> impl IntoView {
    view! { <main>"Hello, world!"</main> }
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let conf = get_configuration(Some("Cargo.toml")).await.unwrap();
    let addr = conf.leptos_options.site_addr.clone();
    HttpServer::new(move || {
        let leptos_options = &conf.leptos_options;

        App::new()
            // {tail:.*} passes the remainder of the URL as the route
            // the actual routing will be handled by `leptos_router`
            .route(
                "/{tail:.*}",
                leptos_actix::render_app_to_stream(
                    leptos_options.to_owned(),
                    || view! { <MyApp/> },
                    Method::Get,
                ),
            )
    })
    .bind(&addr)?
    .run()
    .await
}

§Provided Context Types

This function always provides context values including the following types: