Skip to content

Commit

Permalink
fix: provide custom state in file_and_error_handler (#3408)
Browse files Browse the repository at this point in the history
The `file_and_error_handler` provided by leptos_axum does not provide
the app's custom state in the context. This means that when the fallback
handler tries to render the app, the app will not be able to access the
custom state (and will panic if it uses `expect_context`).

The handle defined by `file_and_error_handler` has access to the custom
state from Axum, so we can pass the custom state from Axum into the
Leptos handler using the `additional_context` parameter of
`handle_response_inner`.
  • Loading branch information
spencewenski authored Dec 27, 2024
1 parent 1c30a4c commit 78d0dbd
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions integrations/axum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1994,20 +1994,22 @@ pub fn file_and_error_handler<S, IV>(
+ 'static
where
IV: IntoView + 'static,
S: Send + 'static,
S: Send + Sync + Clone + 'static,
LeptosOptions: FromRef<S>,
{
move |uri: Uri, State(options): State<S>, req: Request<Body>| {
move |uri: Uri, State(state): State<S>, req: Request<Body>| {
Box::pin(async move {
let options = LeptosOptions::from_ref(&options);
let options = LeptosOptions::from_ref(&state);
let res = get_static_file(uri, &options.site_root, req.headers());
let res = res.await.unwrap();

if res.status() == StatusCode::OK {
res.into_response()
} else {
let mut res = handle_response_inner(
|| {},
move || {
provide_context(state.clone());
},
move || shell(options),
req,
|app, chunks| {
Expand Down

0 comments on commit 78d0dbd

Please sign in to comment.