pub struct SpaRouter<B = Body, T = (), F = fn(_: Error) -> Ready<StatusCode>> { /* private fields */ }
Expand description

Router for single page applications.

SpaRouter gives a routing setup commonly used for single page applications.

Example

use axum_extra::routing::SpaRouter;
use axum::{Router, routing::get};

let spa = SpaRouter::new("/assets", "dist");

let app = Router::new()
    // `SpaRouter` implements `Into<Router>` so it works with `merge`
    .merge(spa)
    // we can still add other routes
    .route("/api/foo", get(api_foo));

async fn api_foo() {}

With this setup we get this behavior:

  • GET / will serve index.html
  • GET /assets/app.js will serve dist/app.js assuming that file exists
  • GET /assets/doesnt_exist will respond with 404 Not Found assuming no such file exists
  • GET /some/other/path will serve index.html since there isn’t another route for it
  • GET /api/foo will serve the api_foo handler function

Implementations

Available on crate feature spa only.

Create a new SpaRouter.

Assets will be served at GET /{serve_assets_at} from the directory at assets_dir.

The index file defaults to assets_dir.join("index.html").

Available on crate feature spa only.

Set the path to the index file.

path must be relative to assets_dir passed to SpaRouter::new.

Example
use axum_extra::routing::SpaRouter;
use axum::Router;

let spa = SpaRouter::new("/assets", "dist")
    .index_file("another_file.html");

let app = Router::new().merge(spa);
Available on crate feature spa only.

Change the function used to handle unknown IO errors.

SpaRouter automatically maps missing files and permission denied to 404 Not Found. The callback given here will be used for other IO errors.

See axum::error_handling::HandleErrorLayer for more details.

Example
use std::io;
use axum_extra::routing::SpaRouter;
use axum::{Router, http::{Method, Uri}};

let spa = SpaRouter::new("/assets", "dist").handle_error(handle_error);

async fn handle_error(method: Method, uri: Uri, err: io::Error) -> String {
    format!("{} {} failed with {}", method, uri, err)
}

let app = Router::new().merge(spa);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Converts to this type from the input type.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more