Skip to content

Commit

Permalink
Remove SpaRouter (#1784)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn authored Feb 25, 2023
1 parent f726f16 commit 27f05ad
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 231 deletions.
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(

This comment has been minimized.

Copy link
@Firstyear

Firstyear Jun 14, 2023

Contributor

Something that wasnt clear here about how this works is that dist/index.html will be served from the router if say "/hello" isn't found. It's almost like the server dir and the "not found" are kind of seperate in concept, but they are merged in the way they are presented to the router. Is there a way to improve the docs here?

because the way that I first read this it seemed like only if a url under /dist/ was not found, then dist/index would be served.

This comment has been minimized.

Copy link
@davidpdrsn

davidpdrsn Jun 14, 2023

Author Member

ServeDir does serve index.html on not found, if it exists. This is just to showcase how to customize that.

This comment has been minimized.

Copy link
@Firstyear

Firstyear Jun 14, 2023

Contributor

My point is that it wasn't obvious that this was the behaviour.

I read this as "serve /assets, if a file in /assets/ is not found, substitute it with /assets/index.html"

When the actual behaviour is "serve /assets, if a file in / is not found, substitue with /index.html"

There is a big difference in these interpretations, and when I first saw this I was confused how this pattern could replace sparouter. So I think better documentation/comments or clarity around what this actually would would be great :)

This comment has been minimized.

Copy link
@davidpdrsn

davidpdrsn Jun 15, 2023

Author Member

You’re welcome to submit a PR.

"/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

0 comments on commit 27f05ad

Please sign in to comment.