Skip to main content

DioxusRouterExt

Trait DioxusRouterExt 

Source
pub trait DioxusRouterExt {
    // Required methods
    fn serve_static_assets(self) -> Router<FullstackState>;
    fn serve_dioxus_application<M>(
        self,
        cfg: ServeConfig,
        app: impl ComponentFunction<(), M> + Send + Sync,
    ) -> Router
       where M: 'static;
    fn register_server_functions(self) -> Router<FullstackState>;
    fn serve_api_application<M>(
        self,
        cfg: ServeConfig,
        app: impl ComponentFunction<(), M> + Send + Sync,
    ) -> Router
       where M: 'static,
             Self: Sized;
}
Available on crate feature server only.
Expand description

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

Required Methods§

Source

fn serve_static_assets(self) -> Router<FullstackState>

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

§Example
use dioxus_server::DioxusRouterExt;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    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
        // ...
        .with_state(dioxus_server::FullstackState::headless());
    let listener = tokio::net::TcpListener::bind(addr).await?;
    axum::serve(listener, router).await?;
    Ok(())
}
Source

fn serve_dioxus_application<M>( self, cfg: ServeConfig, app: impl ComponentFunction<(), M> + Send + Sync, ) -> Router
where M: 'static,

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
use dioxus_server::{DioxusRouterExt, ServeConfig};

#[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(), app);
    let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
    axum::serve(listener, router).await.unwrap();
}

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

fn register_server_functions(self) -> Router<FullstackState>

Registers server functions with the default 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_state(dioxus_server::FullstackState::headless());
    let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
    axum::serve(listener, router).await.unwrap();
}
Source

fn serve_api_application<M>( self, cfg: ServeConfig, app: impl ComponentFunction<(), M> + Send + Sync, ) -> Router
where M: 'static, Self: Sized,

Serves a Dioxus application without static assets. Sets up server function routes and rendering endpoints only.

Useful for WebAssembly environments or when static assets are served by another system.

§Example
#[tokio::main]
async fn main() {
    let router = axum::Router::new()
        .serve_api_application(ServeConfig::new(), app)
        .into_make_service();
    // ...
}

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

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.

Implementors§

Source§

impl DioxusRouterExt for Router<FullstackState>

Available on non-WebAssembly only.