Skip to content

Add generic state support to dioxus::server::router#5561

Open
prodbyola wants to merge 3 commits into
DioxusLabs:mainfrom
prodbyola:main
Open

Add generic state support to dioxus::server::router#5561
prodbyola wants to merge 3 commits into
DioxusLabs:mainfrom
prodbyola:main

Conversation

@prodbyola
Copy link
Copy Markdown

@prodbyola prodbyola commented May 14, 2026

Summary

This PR updates dioxus::server::router to return an axum::Router<S> with a generic state type instead of just axum::Router which takes () by default. This allows consumers to attach shared application state using Axum’s with_state pattern.

Motivation

Currently, there is no straightforward way to pass shared state (e.g. database pools, configuration, or services) into handlers when using dioxus::server::router. Since Dioxus server integration is built on top of Axum, enabling generic state aligns with standard Axum patterns and improves interoperability.

Changes

Updated router signature to:

pub fn router<S>(app: fn() -> Element) -> Router<S>
where
    S: Clone + Send + Sync + 'static

Preserved existing behavior while allowing callers to attach state via .with_state(...)

Example usage

dioxus::serve(|| async {
    let state = AppState::create().await.unwrap();

    Ok(
        dioxus::server::router(app)
            .with_state(state)
    )
});

Benefits

Enables clean dependency injection for server functions
Aligns with Axum’s standard state management model
Removes need for global state or workarounds like thread_local!
Improves ergonomics for real-world apps (e.g. SQLx, Redis, config)

Notes

No breaking changes for existing users who don’t use state
Type inference works seamlessly in most cases

Happy to adjust

@prodbyola prodbyola requested a review from a team as a code owner May 14, 2026 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant