Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove SpaRouter #1784

Merged
merged 1 commit into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion axum-extra/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,33 @@ and this project adheres to [Semantic Versioning].

# Unreleased

- **breaking:** `SpaRouter::handle_error` has been removed ([#1783])
- **breaking:** `SpaRouter` has been removed. Use `ServeDir` and `ServeFile`
from `tower-http` instead:

```rust
// before
Router::new().merge(SpaRouter::new("/assets", "dist"));

// with ServeDir
Router::new().nest_service("/assets", ServeDir::new("dist"));

// before with `index_file`
Router::new().merge(SpaRouter::new("/assets", "dist").index_file("index.html"));

// with ServeDir + ServeFile
Router::new().nest_service(
"/assets",
ServeDir::new("dist").not_found_service(ServeFile::new("dist/index.html")),
);
```

See the [static-file-server-example] for more examples.

- **breaking:** Change casing of `ProtoBuf` to `Protobuf` ([#1595])

[#1783]: https://github.com/tokio-rs/axum/pull/1783
[#1595]: https://github.com/tokio-rs/axum/pull/1595
[static-file-server-example]: https://github.com/tokio-rs/axum/blob/main/examples/static-file-server/src/main.rs

# 0.5.0 (12. February, 2022)

Expand Down
6 changes: 0 additions & 6 deletions axum-extra/src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ use tower_service::Service;

mod resource;

#[cfg(feature = "spa")]
mod spa;

#[cfg(feature = "typed-routing")]
mod typed;

Expand All @@ -28,9 +25,6 @@ pub use axum_macros::TypedPath;
#[cfg(feature = "typed-routing")]
pub use self::typed::{SecondElementIs, TypedPath};

#[cfg(feature = "spa")]
pub use self::spa::SpaRouter;

/// Extension trait that adds additional methods to [`Router`].
pub trait RouterExt<S, B>: sealed::Sealed {
/// Add a typed `GET` route to the router.
Expand Down
202 changes: 0 additions & 202 deletions axum-extra/src/routing/spa.rs

This file was deleted.

25 changes: 3 additions & 22 deletions examples/static-file-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use axum::{
routing::get,
Router,
};
use axum_extra::routing::SpaRouter;
use std::net::SocketAddr;
use tower::ServiceExt;
use tower_http::{
Expand All @@ -31,7 +30,6 @@ async fn main() {
.init();

tokio::join!(
serve(using_spa_router(), 3000),
serve(using_serve_dir(), 3001),
serve(using_serve_dir_with_assets_fallback(), 3002),
serve(using_serve_dir_only_from_root_via_fallback(), 3003),
Expand All @@ -41,30 +39,13 @@ async fn main() {
);
}

fn using_spa_router() -> Router {
// `SpaRouter` is the easiest way to serve assets at a nested route like `/assets`
//
// Requests starting with `/assets` will be served from files in the current directory.
// Requests to unknown routes will get `index.html`.
Router::new()
.route("/foo", get(|| async { "Hi from /foo" }))
.merge(SpaRouter::new("/assets", "assets").index_file("index.html"))
}

fn using_serve_dir() -> Router {
// `SpaRouter` is just a convenient wrapper around `ServeDir`
//
// You can use `ServeDir` directly to further customize your setup
let serve_dir = ServeDir::new("assets");

Router::new()
.route("/foo", get(|| async { "Hi from /foo" }))
.nest_service("/assets", serve_dir.clone())
.fallback_service(serve_dir)
// serve the file in the "assets" directory under `/assets`
Router::new().nest_service("/assets", ServeDir::new("assets"))
}

fn using_serve_dir_with_assets_fallback() -> Router {
// for example `ServeDir` allows setting a fallback if an asset is not found
// `ServeDir` allows setting a fallback if an asset is not found
// so with this `GET /assets/doesnt-exist.jpg` will return `index.html`
// rather than a 404
let serve_dir = ServeDir::new("assets").not_found_service(ServeFile::new("assets/index.html"));
Expand Down