From 104c09f3bf3a3d42fd050fa7fd4177804ed2dc93 Mon Sep 17 00:00:00 2001 From: sify21 Date: Wed, 20 Mar 2024 00:37:42 +0800 Subject: [PATCH] register server_fn first to allow for wildcard Route path (#2435) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It's normal to have a `NotFound` page with a wildcard path like this ``` ... ``` In `ssr` mode, most servers do a `first match win` approach, so we should register server functions before view routes, or else a wildcard route would block all api requests. https://discord.com/channels/1031524867910148188/1218508054442545185 Signed-off-by: 司芳源 --- integrations/actix/src/lib.rs | 32 +++++++++++++---------- integrations/axum/src/lib.rs | 48 +++++++++++++++++------------------ 2 files changed, 42 insertions(+), 38 deletions(-) diff --git a/integrations/actix/src/lib.rs b/integrations/actix/src/lib.rs index c606422969..682616d74c 100644 --- a/integrations/actix/src/lib.rs +++ b/integrations/actix/src/lib.rs @@ -1210,6 +1210,15 @@ where IV: IntoView + 'static, { let mut router = self; + + // register server functions first to allow for wildcard route in Leptos's Router + for (path, _) in server_fn::actix::server_fn_paths() { + let additional_context = additional_context.clone(); + let handler = handle_server_fns_with_context(additional_context); + router = router.route(path, handler); + } + + // register routes defined in Leptos's Router for listing in paths.iter() { let path = listing.path(); let mode = listing.mode(); @@ -1272,13 +1281,6 @@ where } } - // register server functions - for (path, _) in server_fn::actix::server_fn_paths() { - let additional_context = additional_context.clone(); - let handler = handle_server_fns_with_context(additional_context); - router = router.route(path, handler); - } - router } } @@ -1311,6 +1313,15 @@ impl LeptosRoutes for &mut ServiceConfig { IV: IntoView + 'static, { let mut router = self; + + // register server functions first to allow for wildcard route in Leptos's Router + for (path, _) in server_fn::actix::server_fn_paths() { + let additional_context = additional_context.clone(); + let handler = handle_server_fns_with_context(additional_context); + router = router.route(path, handler); + } + + // register routes defined in Leptos's Router for listing in paths.iter() { let path = listing.path(); let mode = listing.mode(); @@ -1355,13 +1366,6 @@ impl LeptosRoutes for &mut ServiceConfig { } } - // register server functions - for (path, _) in server_fn::actix::server_fn_paths() { - let additional_context = additional_context.clone(); - let handler = handle_server_fns_with_context(additional_context); - router = router.route(path, handler); - } - router } } diff --git a/integrations/axum/src/lib.rs b/integrations/axum/src/lib.rs index 76da90958a..df6301b898 100644 --- a/integrations/axum/src/lib.rs +++ b/integrations/axum/src/lib.rs @@ -1624,6 +1624,30 @@ where let mut router = self; + // register server functions first to allow for wildcard router path + for (path, method) in server_fn::axum::server_fn_paths() { + let cx_with_state = cx_with_state.clone(); + let handler = move |req: Request| async move { + handle_server_fns_with_context(cx_with_state, req).await + }; + router = router.route( + path, + match method { + Method::GET => get(handler), + Method::POST => post(handler), + Method::PUT => put(handler), + Method::DELETE => delete(handler), + Method::PATCH => patch(handler), + _ => { + panic!( + "Unsupported server function HTTP method: \ + {method:?}" + ); + } + }, + ); + } + // register router paths for listing in paths.iter() { let path = listing.path(); @@ -1722,30 +1746,6 @@ where } } - // register server functions - for (path, method) in server_fn::axum::server_fn_paths() { - let cx_with_state = cx_with_state.clone(); - let handler = move |req: Request| async move { - handle_server_fns_with_context(cx_with_state, req).await - }; - router = router.route( - path, - match method { - Method::GET => get(handler), - Method::POST => post(handler), - Method::PUT => put(handler), - Method::DELETE => delete(handler), - Method::PATCH => patch(handler), - _ => { - panic!( - "Unsupported server function HTTP method: \ - {method:?}" - ); - } - }, - ); - } - router }