From 7904e0c3950dbe6a90e9c991e9ec40ebafd1a312 Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Mon, 21 Oct 2024 09:14:36 -0400 Subject: [PATCH] fix: unregister server functions whose paths are in excluded routes (closes #2735) (#3138) --- integrations/actix/src/lib.rs | 8 +++++++- integrations/axum/src/lib.rs | 9 ++++++++- server_fn/src/lib.rs | 22 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/integrations/actix/src/lib.rs b/integrations/actix/src/lib.rs index 6096b4ac34..c2d06cc268 100644 --- a/integrations/actix/src/lib.rs +++ b/integrations/actix/src/lib.rs @@ -41,7 +41,8 @@ use once_cell::sync::Lazy; use parking_lot::RwLock; use send_wrapper::SendWrapper; use server_fn::{ - redirect::REDIRECT_HEADER, request::actix::ActixRequest, ServerFnError, + actix::unregister_server_fns, redirect::REDIRECT_HEADER, + request::actix::ActixRequest, ServerFnError, }; use std::{ fmt::{Debug, Display}, @@ -1004,6 +1005,11 @@ where { let _ = any_spawner::Executor::init_tokio(); + // remove any server fns that match excluded paths + if let Some(excluded) = &excluded_routes { + unregister_server_fns(excluded); + } + let owner = Owner::new_root(Some(Arc::new(SsrSharedContext::new()))); let (mock_meta, _) = ServerMetaContext::new(); let routes = owner diff --git a/integrations/axum/src/lib.rs b/integrations/axum/src/lib.rs index b15c00919e..74cf7c82a4 100644 --- a/integrations/axum/src/lib.rs +++ b/integrations/axum/src/lib.rs @@ -71,7 +71,9 @@ use leptos_router::{ #[cfg(feature = "default")] use once_cell::sync::Lazy; use parking_lot::RwLock; -use server_fn::{redirect::REDIRECT_HEADER, ServerFnError}; +use server_fn::{ + axum::unregister_server_fns, redirect::REDIRECT_HEADER, ServerFnError, +}; #[cfg(feature = "default")] use std::path::Path; use std::{fmt::Debug, io, pin::Pin, sync::Arc}; @@ -1338,6 +1340,11 @@ where init_executor(); let owner = Owner::new_root(Some(Arc::new(SsrSharedContext::new()))); + // remove any server fns that match excluded paths + if let Some(excluded) = &excluded_routes { + unregister_server_fns(excluded); + } + let routes = owner .with(|| { // stub out a path for now diff --git a/server_fn/src/lib.rs b/server_fn/src/lib.rs index 53301e6d7f..a3e67ad459 100644 --- a/server_fn/src/lib.rs +++ b/server_fn/src/lib.rs @@ -500,6 +500,17 @@ pub mod axum { .map(|item| (item.path(), item.method())) } + /// Removes any server functions with an included path from the map of + /// registered server functions. + /// + /// Calling this will mean that these server functions are not found unless you provide + /// alternate handlers for them in your application. + pub fn unregister_server_fns(paths: &[String]) { + if !paths.is_empty() { + REGISTERED_SERVER_FUNCTIONS.retain(|(p, _), _| !paths.contains(p)); + } + } + /// An Axum handler that responds to a server function request. pub async fn handle_server_fn(req: Request) -> Response { let path = req.uri().path(); @@ -588,6 +599,17 @@ pub mod actix { .map(|item| (item.path(), item.method())) } + /// Removes any server functions with an included path from the map of + /// registered server functions. + /// + /// Calling this will mean that these server functions are not found unless you provide + /// alternate handlers for them in your application. + pub fn unregister_server_fns(paths: &[String]) { + if !paths.is_empty() { + REGISTERED_SERVER_FUNCTIONS.retain(|(p, _), _| !paths.contains(p)); + } + } + /// An Actix handler that responds to a server function request. pub async fn handle_server_fn( req: HttpRequest,