0.6.2
leptos-rs/leptos0.6.2Jan 26, 2024by gbj
AI Summary
This release mirrors v0.6.3, focusing on the rewritten server function system and Axum 0.7 support, including new encodings, streaming responses, and custom error types.
Key Highlights
- Major rewrite of the server function system
- Support for Axum 0.7 (requires migration from Axum 0.6)
- New built-in encodings: rkyv, multipart forms/file uploads
- Support for streaming responses from server functions
- Custom error types and `#[middleware]` macro support
Breaking Changes
- The `extract` function has been removed in Actix and Axum, replaced with a new one
- The `RequestParts` type has been removed; use `http::request::Parts` directly
- Introduced `ServerFnError::new()` helper to fix type inference issues
- Explicit `.handle_server_fns()` calls in main.rs are no longer needed (handled in `.leptos_routes()`)
New Features
- Server function rewrite
- Axum 0.7 support
- Streaming responses
- Custom encodings (rkyv, multipart)
- Middleware macro
Full Release Notes
This is release for our new server functions rewrite and Axum 0.7 support.
This should be a relatively feature-rich release, with limited breaking changes.
## Migration
### Actix
- You can remove any explicit `.handle_server_fns()` call in your `main.rs`, as server functions are now handled in `.leptos_routes()`
- The current `extract` function has been removed, and replaced with a new `extract` that has the same API as the current [`extractor`](https://docs.rs/leptos_actix/latest/leptos_actix/fn.extractor.html). I think this API is strictly better, but please share feedback if you disagree.
### Axum
- This release supports Axum 0.7, so you'll need to migrate from Axum 0.6. The easiest way to do this is probably to consult [the diff on one of the examples](https://github.com/leptos-rs/leptos/pull/2158/files#diff-ceceec2ecd620c6891344b9407c391c6a9ca3b2d2f101881cd09ddd7f5a31981). (Note that along with Axum 0.7, you'll need to update to `http` 1.0 and `tower_http` 0.5.)
- You can remove any explicit `.handle_server_fns()` call in your `main.rs`, as server functions are now handled in `.leptos_routes()`
- The current `extract` function has been removed, and replaced with a new `extract` that has the same API as the current [`extractor`](https://docs.rs/leptos_axum/latest/leptos_axum/fn.extractor.html). I think this API is strictly better, but please share feedback if you disagree.
- `RequestParts` has been removed, as `http::request::Parts` now implements `Clone`: any `use_context::<RequestParts>()` should be updated to use `Parts` directly instead.
### `ServerFnError::new()`
The addition of custom error types means that constructing `ServerFnError` inside server functions can cause type inference errors:
```rust
let oops = Err(ServerFnError::ServerError("No server state".to_string()));
return oops; // this is fine
oops? // this is not: cannot infer type of the type parameter `E` declared on the enum `ServerFnError`
```
As a result, we've added a helper `ServerFnError::new` which simply constructs a `ServerFnError::<NoCustomError>::ServerError`:
```rust
let oops = Err(ServerFnError::new("No server state"));
return oops; // this is fine
oops? // this is also fine now
```
This has the benefit of being more concise than the earlier pattern in any case.
## Features
A rewritten server function system that is backwards-compatible, but reduces binary size and increases flexibility, specifically by allowing
- automatic setup of server fn handlers with `.leptos_routes()` from the integrations
- a variety of additional built-in encodings (rkyv, multipart forms/file uploads) in addition to the current set (GET URL, POST URL, CBOR, Rkyv) (#1868, #1989)
- support for streaming responses from server functions (#1284)
- ability to create custom user encodings for input and output by deriving `IntoReq`, `FromReq`, `IntoRes`, and/or `FromRes` traits
- ability to mix and match encodings easily: This server function should be a JSON POST request and a ByteStream response, this one should be GET URL request and an rkyv response, etc.; any combination of the encodings above is supported
- custom error types (#1657)
- a `#[middleware]` macro to add per-server-function middleware from the Tower or Actix ecosystems (#1461)
Note: The additional included encodings (`serde_lite`, `rkyv`, multipart form data) are all enabled by additive features on the `server_fn` crate. If you want to use them you can just add that crate as a dependency and enable the required features.
**Example**: You can find a comprehensive example of these new features in the new [`server_fns_axum`](https://github.com/leptos-rs/leptos/blob/leptos_v0.6/examples/server_fns_axum/src/app.rs) example.
**Full Changelog**: https://github.com/leptos-rs/leptos/compare/v0.5.7...0.6.2