Trait DioxusRouterExt

Source
pub trait DioxusRouterExt<S> {
    // Required methods
    fn register_server_functions_with_context(
        self,
        context_providers: Arc<Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync + 'static>>>,
    ) -> Self;
    fn serve_static_assets(self) -> Self
       where Self: Sized;
    fn serve_dioxus_application<Cfg, Error>(
        self,
        cfg: Cfg,
        app: fn() -> Element,
    ) -> Self
       where Cfg: TryInto<ServeConfig, Error = Error>,
             Error: Error,
             Self: Sized;

    // Provided method
    fn register_server_functions(self) -> Self
       where Self: Sized { ... }
}
Available on crate feature axum only.
Expand description

A extension trait with utilities for integrating Dioxus with your Axum router.

Required Methods§

Source

fn register_server_functions_with_context( self, context_providers: Arc<Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync + 'static>>>, ) -> Self

Registers server functions with some additional context to insert into the DioxusServerContext for that handler.

§Example
#[tokio::main]
async fn main() {
    let addr = dioxus::cli_config::fullstack_address_or_localhost();
    let router = axum::Router::new()
        // Register server functions routes with the default handler
        .register_server_functions_with_context(Arc::new(vec![Box::new(|| Box::new(1234567890u32))]))
        .into_make_service();
    let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
    axum::serve(listener, router).await.unwrap();
}
Source

fn serve_static_assets(self) -> Self
where Self: Sized,

Serves the static WASM for your Dioxus application (except the generated index.html).

§Example
#[tokio::main]
async fn main() {
    let addr = dioxus::cli_config::fullstack_address_or_localhost();
    let router = axum::Router::new()
        // Server side render the application, serve static assets, and register server functions
        .serve_static_assets()
        // Server render the application
        // ...
        .into_make_service();
    let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
    axum::serve(listener, router).await.unwrap();
}
Source

fn serve_dioxus_application<Cfg, Error>( self, cfg: Cfg, app: fn() -> Element, ) -> Self
where Cfg: TryInto<ServeConfig, Error = Error>, Error: Error, Self: Sized,

Serves the Dioxus application. This will serve a complete server side rendered application. This will serve static assets, server render the application, register server functions, and integrate with hot reloading.

§Example
#[tokio::main]
async fn main() {
    let addr = dioxus::cli_config::fullstack_address_or_localhost();
    let router = axum::Router::new()
        // Server side render the application, serve static assets, and register server functions
        .serve_dioxus_application(ServeConfig::new().unwrap(), app)
        .into_make_service();
    let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
    axum::serve(listener, router).await.unwrap();
}

fn app() -> Element {
    rsx! { "Hello World" }
}

Provided Methods§

Source

fn register_server_functions(self) -> Self
where Self: Sized,

Registers server functions with the default handler. This handler function will pass an empty DioxusServerContext to your server functions.

§Example
#[tokio::main]
async fn main() {
    let addr = dioxus::cli_config::fullstack_address_or_localhost();
    let router = axum::Router::new()
        // Register server functions routes with the default handler
        .register_server_functions()
        .into_make_service();
    let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
    axum::serve(listener, router).await.unwrap();
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<S> DioxusRouterExt<S> for Router<S>
where S: Send + Sync + Clone + 'static,

Source§

fn register_server_functions_with_context( self, context_providers: Arc<Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync + 'static>>>, ) -> Self

Source§

fn serve_static_assets(self) -> Self

Source§

fn serve_dioxus_application<Cfg, Error>( self, cfg: Cfg, app: fn() -> Element, ) -> Self
where Cfg: TryInto<ServeConfig, Error = Error>, Error: Error,

Implementors§